comparator

comparator with null values

我是研究僧i 提交于 2019-11-26 19:17:15
问题 We have some code wich sorts a list of addresses based on the distance between their coordinates. this is done through collections.sort with a custom comparator. However from time to time an adress without coordinates is in the list causing a NullPointerException. My initial idea to fix this was to have the comparator return 0 as dististance for addresses where at least one of the coordinates is null. I fear this might lead to corruption of the order the 'valid' elements in the list. so is

Java TreeMap Comparator

大城市里の小女人 提交于 2019-11-26 17:43:22
I need a comparator for a TreeMap. Should I write this anonymously in the constructor for my TreeMap? How else could I write my comparator. Currently, Java does not like my code (can I do this anonymously?): SortedMap<String, Double> myMap = new TreeMap<String, Double>(new Comparator<Entry<String, Double>>() { public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { return o1.getValue().compareTo(o2.getValue()); } }); Can I do the above anonymously? How else could I do this? I want to sort myMap by the Value not the Key You can not sort TreeMap on values. A Red-Black tree based

Java generics: Collections.max() signature and Comparator

两盒软妹~` 提交于 2019-11-26 15:38:26
问题 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> ? 回答1:

Comparator.comparing(…) of a nested field

人盡茶涼 提交于 2019-11-26 14:37:26
问题 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

Java Comparator class to sort arrays

╄→гoц情女王★ 提交于 2019-11-26 13:21:48
Say, we have the following 2-dimensional array: int camels[][] = new int[n][2]; How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order using Arrays.sort(camels, comparator) ? The compare function for reference is: @Override public int compare(int[] a, int [] b) { return b[0] - a[0]; } aioobe [...] 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[][]

Very confused by Java 8 Comparator type inference

久未见 提交于 2019-11-26 12:54:43
I've been looking at the difference between Collections.sort and list.sort , specifically regarding using the Comparator static methods and whether param types are required in the lambda expressions. Before we start, I know I could use method references, e.g. Song::getTitle to overcome my problems, but my query here is not so much something I want to fix but something I want an answer to, i.e. why is the Java compiler handling it in this way. These are my finding. Suppose we have an ArrayList of type Song , with some songs added, there are 3 standard get methods: ArrayList<Song> playlist1 =

Android-java- How to sort a list of objects by a certain value within the object

时光怂恿深爱的人放手 提交于 2019-11-26 12:45:33
Im trying to sort through an arraylist of objects by a particular value within the object. What would be the best approach to do such a thing. Should I use Collections.sort() with some kind of comparator? Im trying to sort a list of objects by a float value they hold in one of the variables. EDIT: This is what I have so far: public class CustomComparator implements Comparator<Marker> { @Override public int compare(Mark o1, Mark o2) { return o1.getDistance().compareTo(o2.getDistance()); } } the error states: Cannot invoke compareTo(double) on the primitive type double. Is it because a

What does comparison being consistent with equals mean ? What can possibly happen if my class doesn&#39;t follow this principle?

烂漫一生 提交于 2019-11-26 12:43:38
问题 From the JavaDoc of TreeMap : Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are

null-safe mapping Comparator using default implementations

不打扰是莪最后的温柔 提交于 2019-11-26 12:27:21
问题 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>

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

a 夏天 提交于 2019-11-26 11:30:06
问题 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\",\