null-safe mapping Comparator using default implementations
Found a possible solution: Comparator.comparing(ToSort::getSortBy, Comparator.nullsFirst(Comparator.naturalOrder()) )
Found a possible solution: Comparator.comparing(ToSort::getSortBy, Comparator.nullsFirst(Comparator.naturalOrder()) )
You can not sort TreeMap on values. A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used You will need to provide comparator for Comparator<? super K> so your comparator should compare … Read more
Java needs to know a type of all variables. In many lambdas it can infer a type, but in your first code snippet, it cannot guess the type of s. I think the standard way to solve that problem would be to declare it explicitly: Comparator<String> c = Comparator.comparing((String s) -> s.split(“\\s+”)[0]) .thenComparingInt(s -> Integer.parseInt(s.split(“\\s+”)[1])); … Read more
You can use Comparator.reverseOrder() to have a comparator giving the reverse of the natural ordering. If you want to reverse the ordering of an existing comparator, you can use Comparator.reversed(). Sample code: Stream.of(1, 4, 2, 5) .sorted(Comparator.reverseOrder()); // stream is now [5, 4, 2, 1] Stream.of(“foo”, “test”, “a”) .sorted(Comparator.comparingInt(String::length).reversed()); // stream is now [test, foo, … Read more
Follow this code to sort any ArrayList Collections.sort(myList, new Comparator<EmployeeClass>(){ public int compare(EmployeeClass obj1, EmployeeClass obj2) { // ## Ascending order return obj1.firstName.compareToIgnoreCase(obj2.firstName); // To compare string values // return Integer.valueOf(obj1.empId).compareTo(Integer.valueOf(obj2.empId)); // To compare integer values // ## Descending order // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values // return Integer.valueOf(obj2.empId).compareTo(Integer.valueOf(obj1.empId)); // To compare integer … Read more
[…] How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order […] Here’s a complete example using Java 8: import java.util.*; public class Test { public static void main(String args[]) { int[][] twoDim = { {1, 2}, {3, 7}, {8, 9}, {4, 2}, {5, 3} }; Arrays.sort(twoDim, … Read more
Guava is more elegant: return ComparisonChain.start() .compare(d1.weight, d2.weight) .compare(d1.age, d2.age) .compare(d1.name, d2.name) .result(); Apache commons-lang has a similar construct, CompareToBuilder.
First, all the examples you say cause errors compile fine with the reference implementation (javac from JDK 8.) They also work fine in IntelliJ, so its quite possible the errors you’re seeing are Eclipse-specific. Your underlying question seems to be: “why does it stop working when I start chaining.” The reason is, while lambda expressions … Read more
You need to poll the items from the PriorityQueue one by one. toString doesn’t do that. So instead of your System.out.println(queue); do this: while(!queue.isEmpty()) { System.out.println(queue.poll()); } The reason is that the PriorityQueue is never completely sorted internally, lookup how a heap works for more detail. Polling items from it fixes the heap during the … Read more
In java the “natural” order meaning is “lexicographical” order, so there is no implementation in the core like the one you’re looking for. There are open source implementations. Here’s one: NaturalOrderComparator.java Make sure you read the: Cougaar Open Source License I hope this helps!