comparator

Java Comparator for Objects with multiple fields [closed]

佐手、 提交于 2019-12-01 02:38:28
I have an Object Collection with 5 fields: id; entityType; entityId; brandId; productId; To sort an ArrayList of Collection I have written the following Comparaor . Comparator<Collection> collectionComparator = new Comparator<Collection>() { @Override public int compare(Collection collection1, Collection collection2) { if(collection1.getId().equals(collection2.getId())) { if(collection1.getEntityType().equals(collection2.getEntityType())) { if(collection1.getEntityId().equals(collection2.getEntityId())) { if(collection1.getBrandId().equals(collection2.getBrandId())) { return collection1

TreeSet constructor with Comparator<?> parameter

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 02:26:58
In Java’s documentation for its class TreeSet one of the constructors is shown to have the following header: TreeSet(Comparator<? super E> c) Can someone help explain why there is a constructor for TreeSet which takes a comparator object as its argument? I have no clue why this is done. The elements in a TreeSet are kept sorted. If you use a constructor that has no Comparator, the natural ordering of the element class (defined by the implementation of Comparable ) would be used to sort the elements of the TreeSet. If you want a different ordering, you supply a Comparator in the constructor.

Curious about the implementation of CaseInsensitiveComparator [duplicate]

做~自己de王妃 提交于 2019-12-01 02:19:13
This question already has an answer here: Understanding logic in CaseInsensitiveComparator 5 answers While I check the implementation of CaseInsensitiveComparator , which is private inner class of String , I found strange thing. private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable { ... public int compare(String s1, String s2) { int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character

Efficiency of the way comparator works

跟風遠走 提交于 2019-11-30 23:45:48
I am trying to use a comparator to help sort a list of objects. I have a question about how exactly the comparator works and what it would be doing exactly in the following example: private static Comparator<Student> comparator() { return (Student a, Student b) -> { return Integer.compare(complexOperation(a), complexOperation(b)); } } As you can see above, there is a need to compare and sort students according to an integer rank returned by the complexOperation() method. As the name suggests, it is a heavy operation. Would the above approach be the most efficient? Or would it be better to

Misunderstanding about Comparator in java 8

雨燕双飞 提交于 2019-11-30 22:59:11
问题 public class Test { public static void main(String[] args) { List<Pair<String, Integer>> list = new ArrayList<>(); list.add(new Pair<>("1", 8)); list.add(new Pair<>("3", 2)); list.add(new Pair<>("2", 15)); list.stream() .sorted(Comparator.comparingInt(p -> p.v)) .map(p -> p.k) .forEach(System.out::println); } } class Pair<K, V> { K k; V v; public Pair(K k, V v) { this.k = k; this.v = v; } } Ok, as you understood this code is printing my pair keys from the lowest value associated to the

Curious about the implementation of CaseInsensitiveComparator [duplicate]

我只是一个虾纸丫 提交于 2019-11-30 21:47:31
问题 This question already has answers here : Understanding logic in CaseInsensitiveComparator (5 answers) Closed 4 years ago . While I check the implementation of CaseInsensitiveComparator , which is private inner class of String , I found strange thing. private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable { ... public int compare(String s1, String s2) { int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i

Efficiency of the way comparator works

廉价感情. 提交于 2019-11-30 17:22:18
问题 I am trying to use a comparator to help sort a list of objects. I have a question about how exactly the comparator works and what it would be doing exactly in the following example: private static Comparator<Student> comparator() { return (Student a, Student b) -> { return Integer.compare(complexOperation(a), complexOperation(b)); } } As you can see above, there is a need to compare and sort students according to an integer rank returned by the complexOperation() method. As the name suggests,

Sorting using Comparator Interface and java 8 Streams

限于喜欢 提交于 2019-11-30 16:34:51
问题 Parent is a class which is inherited by Child. which is inherited by GrandChild. Each class contains List of the child class(i.e Parent contains List of Child and Child contains List of GrandChild). Each class contains 50 attributes(attrib1-atrib50). getChildList() returns the arrayList of objects of type Child getGrandChildList() returns the arrayList of objects of type GrandChild Let resultSet be List of Parent List<Parent> resultSet Now I want to sort the list based on some attributes. For

Java comparator for multi-column sorting?

匆匆过客 提交于 2019-11-30 13:46:10
Is there any Java open-source comparator for comparing beans by multiple fields for multi-column sorting? Each column can be sorted asceding or descending. For single-column sorting it can be achieved by using org.apache.commons.beanutils.BeanComparator together with org.springframework.util.comparator.InvertibleComparator . I'm aware that this functionality is quite trivial to write, but what's the benefit from reinventing the wheel, if it was already written and tested? I wrote this a few months ago. public abstract class ChainedComparator<T> implements Comparator<T> { private Comparator<T>

Nice general way to sort nulls to the bottom, regardless?

老子叫甜甜 提交于 2019-11-30 13:26:57
I'm writing some custom Comparators, and I'd like them to push null items to the bottom of the list, regardless of whether I'm sorting ascending or descending. What's a good strategy or pattern for approaching this? Offhand: Simply write separate ascending and descending comparators, sharing code where possible Delegate null handling to another class, either by throwing an NPE or by calling it explicitly Include an ascending flag and put conditional logic in it to navigate around the nulls Wrap regular comparators in a null-handling class Any other strategies? I'd like to hear about any