comparator

Java stream sort 2 variables ascending/desending

放肆的年华 提交于 2019-11-27 12:59:59
问题 I want to sort seq1 ascending and seq2 descending so I do this: list = list.stream().sorted(comparing(AClass::getSeq1).thenComparing( AClass::getSeq2).reversed()).collect(toList()); But the result come out as both seq1 and seq2 are sorted in descending order. I can do this to make seq1 ascending and seq2 descending: sorted(comparing(AClass::getSeq1) .reversed().thenComparing(AClass::getSeq2).reversed() What is really the correct way to do this? 回答1: In your first example, reversed is applied

Java generics: Collections.max() signature and Comparator

寵の児 提交于 2019-11-27 11:35:37
I understand the get and put principle for collections: if a method takes in a collection that it will write a type T to, the parameter has to be Collection<? super T> , whereas if it will read a type T from, the parameter has to be Collection<? extends T> . But could someone please explain the Collections.max() signature: public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) In particular why is it Comparator<? super T> instead of Comparator<? extends T> ? Josh Bloch's mnemonic PECS is useful here. It stands for: Producer extends , Consumer super This means that

Comparator.comparing(…) of a nested field

陌路散爱 提交于 2019-11-27 09:13:25
Suppose I have a domain model like this: class Lecture { Course course; ... // getters } class Course { Teacher teacher; int studentSize; ... // getters } class Teacher { int age; ... // getters } Now I can create a Teacher Comparator like this: return Comparator .comparing(Teacher::getAge); But how do I compare Lecture's on nested fields, like this? return Comparator .comparing(Lecture::getCourse::getTeacher:getAge) .thenComparing(Lecture::getCourse::getStudentSize); I can't add a method Lecture.getTeacherAge() on the model. Eran You can't nest method references. You can use lambda

Java - Sort Strings like Windows Explorer

╄→尐↘猪︶ㄣ 提交于 2019-11-27 09:10:26
I am trying to use code suggested by Sander Pham on another question. I need my java ArrayList of string names to be sorted like Windows Explorer does. His code worked for everything but for one issue. I would have liked to comment onto that question, but I need more reputation points to comment. Anyways... He suggested to use a custom comparator implemented class and use that to compare the string names. Here is the code of that class: class IntuitiveStringComparator implements Comparator<String> { private String str1, str2; private int pos1, pos2, len1, len2; public int compare(String s1,

Comparator with double type

旧时模样 提交于 2019-11-27 08:12:39
I have written the following code: public class NewClass2 implements Comparator<Point> { public int compare(Point p1, Point p2) { return (int)(p1.getY() - p2.getY()); } } If I let's say have two double numbers, 3.2 - 3.1 , the difference should be 0.1 . When I cast the number to an int, however, the difference ends up as 0 , which is not correct. I therefore need compare() to return a double, not an int. The problem is, my getX field is a double. How can I solve this problem? You don't need to return double . The Comparator interface is used to establish an ordering for the elements being

null-safe mapping Comparator using default implementations

空扰寡人 提交于 2019-11-27 07:56:24
Is there a build-in possibility to create a null-safe mapping comparator in Java 8 without writing a own implementation of Comparator ? When running the following code, it causes a NPE because the keyExtractor argument of Comparator.comparing() may return a null value: public class ToSort { private String sortBy; public ToSort(String sortBy) { this.sortBy = sortBy; } public String getSortBy() { return sortBy; } public static void main(String[] args) { // mapping comparator Comparator<ToSort> comp = Comparator.comparing(ToSort::getSortBy); SortedSet<ToSort> set = new TreeSet<>(comp); ToSort o1

comparing and thenComparing gives compile error

a 夏天 提交于 2019-11-27 07:43:58
问题 I am trying to sort List of employees by name then age using Java8 Comparator , I have created below Comparator but it gives me a compiler error Type mismatch: cannot convert from Comparator<Object> to <unknown> Comparator<String> c = Comparator.comparing(s -> s.split("\\s+")[0]) .thenComparingInt(s -> Integer.parseInt(s.split("\\s+")[1])); //compile error but it works if I explicitly specify the Type Comparator<String> c = Comparator.<String, String> comparing(s -> s.split("\\s+")[0])

why does my compare method throw exception — Comparison method violates its general contract!

不想你离开。 提交于 2019-11-27 05:16:31
Why does this code public class SponsoredComparator implements Comparator<SRE> { public boolean equals(SRE arg0, SRE arg1){ return arg0.getSponsored()==arg1.getSponsored(); } public int compare(SRE object1, SRE object2) { Log.d("SponsoredComparator","object1.getName() == "+ object1.getName()); Log.d("SponsoredComparator","object1.getSponsored() == "+ object1.getSponsored()); Log.d("SponsoredComparator","object2.getName() == "+ object2.getName()); Log.d("SponsoredComparator","object2.getSponsored() == "+ object2.getSponsored()); Log.d("SponsoredComparator","compare return == "+ (object1

Why does the Java Collections Framework offer two different ways to sort?

依然范特西╮ 提交于 2019-11-27 04:32:03
问题 If I have a list of elements I would like to sort, Java offers two ways to go about this. For example, lets say I have a list of Movie objects and I’d like to sort them by title. One way I could do this is by calling the one-argument version of the static java.util.Collections.sort( ) method with my movie list as the single argument. So I would call Collections.sort(myMovieList). In order for this to work, the Movie class would have to be declared to implement the java.lang.Comparable

Reverse a comparator in Java 8

做~自己de王妃 提交于 2019-11-27 04:28:48
I have an ArrayList and want sort it in descending order. I use for it java.util.stream.Stream.sorted(Comparator) method. Here is a description according Java API: Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator . this methods return me a sort with ascending order. Which parameter should I change, just to have the descending order? Tunaki 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() .