comparator

Comparator based on different nullable fields of an object

社会主义新天地 提交于 2019-12-06 01:11:50
I have an Employee object which contains two fields name and jobTitle . For sorting the employee objects, first priority should be on jobTitle , if jobTitle is null then the sorting should be based on name. Below is the Employee object public class Employee { private String name; private String jobTitle; } I used chained Comparator with JobTitlecomparator and NameComparator to achieve this: public class EmployeeChainedComparator implements Comparator<Employee> { private List<Comparator<Employee>> listComparators; @SafeVarargs public EmployeeChainedComparator(Comparator<Employee>... comparators

Rules to implement compare method

我们两清 提交于 2019-12-05 21:41:58
like compareTo, that have to be "reflexive, antisymmetric and transitive", are there any rules to implement the compare method?? thanks PermGenError From Comparator API : The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.) The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0. Finally, the implementor must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn

Why doesn't sorted(Comparator::reverseOrder) work?

流过昼夜 提交于 2019-12-05 20:47:25
The below Stream expression works perfectly fine: Stream<String> s = Stream.of("yellow","blue", "white"); s.sorted(Comparator.reverseOrder()) .forEach(System.out::print);` //yellowwhiteblue Why doesn't the equivalent one with method references compile? s.sorted(Comparator::reverseOrder).forEach(System.out::print); The type Comparator does not define reverseOrder(String, String) that is applicable here A method reference is telling Java "treat this method as the implementation of a single-method interface"--that is, the method reference should have the signature int foo(String,String) and thus

“Comparison method violates its general contract” is thrown only in certain cases

巧了我就是萌 提交于 2019-12-05 18:23:25
First of all I know that this issue was described in many other threads. However I was not able to find and answer to the question, why this error is not always thrown? Let me describe what I mean. I have written some sample code to illustrate this : public class Mushroom { public int size; public Mushroom(int size) { this.size = size; } @Override public boolean equals(Object obj) { //this is intentionally false - read in description return false; } } dsa public class MushroomComparator implements Comparator<Mushroom> { @Override public int compare(Mushroom o1, Mushroom o2) { // here is the

Attempting to use Comparator to sort by name, ignore case, as well as nulls first

走远了吗. 提交于 2019-12-05 17:18:08
I am having issues using the Java 8 Comparator class to sort a list of items. My current working comparator is below: comparator = Comparator.comparing(Person::getName, Comparator.nullsFirst(Comparator.naturalOrder())); This works: it orders the list by name with the null values first. However, I am now attempting to ignore case of the names. I know that I can write a new getter that returns the name all lowercase, but I do not want to go with this approach as I have to do this for multiple attributes. Looking online, it looks like I should be using String.CASE_INSENSITIVE_ORDER , but the only

get the three highest values in a TreeMap

守給你的承諾、 提交于 2019-12-05 15:33:26
I am trying to find the three highest values in a TreeMap. I wrote a code that is kind of doing it, but I would like to ask whether you can suggest a more efficient way. Basically, I am saving each word of my text in a TreeMap along with the number of times it appears in the text. Then I am using a comparator to sort the values. Then I am iterating through the newly created Map until I reach the last three values, which are the highest values after the sorting and print them out. I am going to use large texts, so this is not a very good way. Here is my code: class Text{ public static void main

Using binarySearch with Comparator and regex

假装没事ソ 提交于 2019-12-05 13:04:40
I am trying to write a quick search that searches a List<String> Instead of looping through the list and manually checking, I want to do this using binarySearch, but I am not sure how to do it. Old way: for(String s : list) { if(s.startsWith("contact.") return true; } Instead I would like something like this: Collections.sort(list); Collections.binarySearch(list, FindContactComparator()); Can someone help me write this Comparator? Is there any better way of doing this instead of using binarySearch? This should work: Comparator<String> startsWithComparator = new Comparator<String>() { public

Java PriorityQueue Comparator - How/When do you sort?

笑着哭i 提交于 2019-12-05 12:49:06
I'm initialising a Priority Queue like: strategy = new FuelPriority(); incoming = new PriorityQueue<Vehicle>(1, strategy); The code for my Comparator class is: public class FuelPriority implements Comparator<Object> { public int compare(Object o1, Object o2) { Vehicle a1 = (Vehicle) o1; Vehicle a2 = (Vheicle) o2; return Integer.compare(a1.getFuelLevel(), a2.getFuelLevel()); } } After running a simulation, the elements aren't ordered at all - they are random; I set a breakpoint in the compare method of my FuelPriority class, but it wasn't called at all. Am I missing something here? Aside from

Working of Comparator.nullsFirst when both are null

夙愿已清 提交于 2019-12-05 08:57:50
I have a class with several "optional" (not java.util.Optional ) fields. I was writing a Lambda comparator to test for equality by comparing a subset of their attributes I wrote private final static Comparator<MyEntity> COMPARATOR_491 = comparing(MyEntity::getIsin) .thenComparing(MyEntity::getTMarketType) .thenComparing(nullsFirst(comparing(MyEntity::getIsoCode))) .thenComparing(MyEntity::getTaxRate) .thenComparing(nullsFirst(comparing(MyEntity::getEndDate))); ISIN is not null Market type is not null Is code can be null Tax rate is not null End date can be null The problem is that often I get

How to compare strings in golang? [closed]

北城以北 提交于 2019-12-05 08:18:21
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I want to make a function that calculates the length of the common segment (starting from the beginning) in two strings. For example: foo:="Makan" bar:="Makon" The result should be 3. foo:="Indah" bar:="Ihkasyandehlo" The result should be 1. 回答1: It's not clear what you are asking because you limited your test