Concept
How can method references and Comparator methods make ordering more concise?
ComputerScienceOne / Java 8: Lambda Expressions
"We can make this even more terse using method references, another new feature in Java 8.\n\n//using getters as key extractors\n\nmyList.sort(\n\nComparator.comparing(Student::getLastName)\n\n.thenComparing(Student::getFirstName)\n\n.thenComparing(Student::getGpa));\n\nThere are several other convenient methods provided by the updated Comparator interface. For example, the reversed() member method returns a new Comparator that defines the reversed order. The static methods, nullsFirst() and nullsLast() can be used to modify a Comparator to order null values."
Related Ideas
- How can lambda expressions simplify sorting a list?ComputerScienceOne · Java 8: Lambda Expressions
- How can Comparator order Student objects by different fields?ComputerScienceOne · Comparators
- How can a Java Comparator handle null values?ComputerScienceOne · Importance of javaequals() and javahashCode() Methods
- How can a Comparator sort Student objects by GPA?ComputerScienceOne · Searching
- How can anonymous classes define custom Java comparators?ComputerScienceOne · Comparators
- What is the difference between Comparable and Comparator in Java?ComputerScienceOne · Comparators
- How does Java sort arrays and lists with comparators?ComputerScienceOne · Sorted Collections
- How does a Comparator determine the order of two elements?ComputerScienceOne · Comparators