Profile Picturejavinpaul

How to compare Java objects on multiple fields?

Hello folks,

It's been quite some time I share a technical post with you, so today, I am sharing some advanced examples of Comparable and Comparator in Java to compare objects on multiple fields like name, address, age, and more. 

Both Comparator and Comparables are used to compare objects but they are not easy to implements, especially when you want them to use in the real world as you may need to sort on multiple fields, maybe some fields will be required to sort in reverse order and so on. 

It was tricky before Java 8  but after Java 8 it's a lot easier because now you can chain comparators using thenComparing() and you have access to more powerful methods like to sort in reverse order and more. I have discussed all of them in this article, if you haven't read it yet, you can read it now to revise your Comparator concepts. 

Here is the link to the article -  6 Advanced Comparator and Comparable Examples in Java


Happy Coding

Javin

Read more - How to compare Java objects on multiple fields?

🎁 Top 5 Dev Deals for You This Week

🔔 30% Discount on My New Book 250+ Spring Framework Practice Questions

🎁 Best Coursera Certifications in 2022 [with $100 OFF on Coursera Plus]

🔔 [Last Reminder] 50% Discount on My New Book Ends in 4 hours

🔔 [24 Hours Left] 50% Discount on My New Book - 250+ Spring Framework Practice Questions

See all posts from javinpaul

Powered by