comparator

Does a natural comparator exist in the standard api?

半腔热情 提交于 2019-11-28 22:39:51
问题 I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator: private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } } Seems simple enough, but I was wondering if anyone knew of one in the standard API. I looked at TreeMap, and it does it without such a

Java stream sort 2 variables ascending/desending

落爺英雄遲暮 提交于 2019-11-28 20:38:29
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? In your first example, reversed is applied to the whole comparator which compares seq1 then seq2 in ascending order. What you need is to reverse the

comparing and thenComparing gives compile error

情到浓时终转凉″ 提交于 2019-11-28 13:31:53
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]) .thenComparingInt(s -> Integer.parseInt(s.split("\\s+")[1])); //works or by creating two Compartor s and

Making a Java PriorityQueue into a stable priority queue

╄→尐↘猪︶ㄣ 提交于 2019-11-28 12:25:51
I'm trying to implement a stable (first in first out) priority queue in Java. Supposing that the key is a name and the value is an age, I know I can make an unstable priority queue like this: Queue<Map.Entry<String, Integer>> pq = new PriorityQueue<Map.Entry<String, Integer>>(100, ageComparator); This does pretty much everything that I need it to, except that it doesn't maintain order of key-value pairs as I insert them (or remove them). I've found a "work around" by making a LinkedList, which offers essentially all of the same functionality, except that it doesn't include a constructor with a

How to use Java comparator properly?

本小妞迷上赌 提交于 2019-11-28 12:22:49
If I have the following class: public class Employee { private int empId; private String name; private int age; public Employee(int empId, String name, int age) { // set values on attributes } // getters & setters } How can I use comparator that compares by name, then age, then id? You need to implement it so that it orders by preferred elements. That is, you need to compare by name, then if that comparison is equal, compare by age, etc. An example is listed below: public class EmployeeComparator implements Comparator<Employee> { @Override public int compare(Employee e1, Employee e2) { int

Is there a built-in way to compare IEnumerable<T> (by their elements)?

為{幸葍}努か 提交于 2019-11-28 09:47:35
问题 I would like to compare lists of elements of a given type, to see which list is "bigger". new BuiltInComparer<IEnumerable<int>>().Compare( new[] {3,2,3}, new[] {1,2,3}) ...would return 1 new BuiltInComparer<IEnumerable<int>>().Compare( new[] {1,2,3}, new[] {1,2,4}) ...would return -1 etc Is there any such built in comparer? 回答1: I don't think there's anything built into the framework - and as Eric says, you haven't provided the comparison criteria. If you mean "compare element-wise in the

A Mechanism for having different equals (physical equals and logical equals) on objects in Collection

柔情痞子 提交于 2019-11-28 08:37:16
问题 Is there any Equalator mechanism like Comparator so I can have different equals for coparing lists? EDIT: My goal is to differentiate between current list1.equals(list2) which checks if its a shallow copy or also a deep copy with all objects a.equals(b) and list1.identical(list2) which checks if its simply shallow copy with unmodified listing All these lists are from the same model. Some are copies of themselves so they hold the pointer to same objects, and others are deep copies so hierarchy

What's the difference between “LIKE” and “=” in SQL?

懵懂的女人 提交于 2019-11-28 07:58:29
Is there any difference between: SELECT * FROM users WHERE username="davyjones" and SELECT * FROM users WHERE username LIKE "davyjones" (I think I've bungled up the syntax... pardon me for that, I'm mostly a desktop app development guy) As per SQL standard, the difference is treatment of trailing whitespace in CHAR columns. Example: create table t1 ( c10 char(10) ); insert into t1 values ('davyjones'); select * from t1 where c10 = 'davyjones'; -- yields 1 row select * from t1 where c10 like 'davyjones'; -- yields 0 rows Of course, assuming you run this on a standard-compliant DBMS. BTW, this

Does Collections.sort keep order on equal elements?

不羁岁月 提交于 2019-11-28 07:10:36
问题 I have a list of objects ordered by a date parameter and want to reorder them by category parameter, but keeping the date order within the category. Is something like this enough, or do I have to implement a comparator that takes on account the date for objects of the same category? // sort the list by category asc(, date asc ) Collections.sort((List<Object>)entries, new Comparator<Object>() { @Override public int compare(Object elementA, Object elementB) { return elementA.category.compareTo

java comparator, how to sort by integer?

一世执手 提交于 2019-11-28 05:46:06
Im trying to learn comparator in java and I have found this great example online, my question is how would this code be changed so that the pet names are ordered by age and in descending order so that the oldest is first and youngest is last? class Dog implements Comparator<Dog>, Comparable<Dog>{ private String name; private int age; Dog(){ } Dog(String n, int a){ name = n; age = a; } public String getDogName(){ return name; } public int getDogAge(){ return age; } // Overriding the compareTo method public int compareTo(Dog d){ return (this.name).compareTo(d.name); } // Overriding the compare