Knowledge is power: If you use Comparator.comparing() you just need getters in the Stock class for name and date and a method that calcs the profit (if key 'change' is in the map -> change, else 'last' - 'open' ... thanks Dan).
then you just chain together the Comparators and you're done (use method references as parameter for the comparing(), then comparing methods like Stock::getName, that way you save a lot of anonymous class overhead)
the chain then looks smth like that: Comparator.comparing(... the method ref ...).thenComparing(...)....
Don't forget to reverse the last property. For that there is a overloaded thenComparing method that takes a second argument (keyExtractor and keyComparator -> the keyExtractor is the above mentioned method reference for the keyComparator you can use one of Comparators static methods: reverseOrder()).
The result then looks stunningly simple.
The final challenge was incredibly difficult for me.
Without spoiling anything, here's my advice. Once you've created your comparator and fixed the exception issue:
1: Make sure the the stocks sort alphabetically
2: Make all stock names the same, and ensure that the list populates first with the newest stock Date (Jan 2 2032 should appear before Jan 1 2032)
3: Make all stock names AND dates the same, and ensure that the list populates with the highest profit per day (Change 0.06 is bigger than open 1.00, last 1.05)
Info on calculating profits:
Stocks with change -- They give you the end of day totals, and they tell you whether it was a positive or negative change
Stocks with open and last -- You have to do the legwork here to figure out if the stock went up or down. If it opens at 2 dollars and ends at 1.99, that means the profit is -0.01
Good Luck!
Yeah, get ready for more confusing stuff!
I'm doing my best to try understand things not just copy and pasting, I don't understand why they need to introduce so many new things in some tasks, makes overall learning impossible.
I just hope it's one of those ways of thinking, we make it hard as hell now, so later when you are working it will be easy peasy.. you know, like Navy SEAL training, a period of hell and then you're a top soldier..
At least this is what I hope! That everything has its purpose here.. hopefully... 🤣
P.S. and then get to senior developer in 1 year thanks to all of the knowledge gained here 😆
I have similar insights, It's great for guys whose have predisposition to thinking in that fields. For normal people can be hard and disapointing. Do you have some tips from your succes position? :D
If it's hard for you to motivate yourself, continue with CodeGym. If you can easily motivate yourself, first learn Java basics, maybe read the book for the OCA exam. Then learn Spring Boot, Maven (udemy courses), learn about REST and API's, start buildling Java backend projects (testing with postman for front end) -see Bezkoder, AmigosCode- using microservices style, check job descriptions for JR java and see what else is required and learn that. But for tech interviews you really need to know the basics (OCA book or OCP book) and then have a portfolio on which to discuss you knowledge.
GO TO FULL VERSION