Skip to content
Advertisement

Tag: comparator

How to write custom comparator in java?

I want to store key-value pairs in TreeMap and sort the entries based on the value of key as per the following logic: “type” (case insensitive) key should be at first. the key start with “metadata” (case insensitive) should be at last in ascending order rest of the keys(case insensitive) should be in middle in ascending order I am using

Why compareTo throws NPE using a comparator who doesn’t do that when user separatedly

I created a comparator that never throws NPE. However, when using it inside compareTo, it throws NPE. Why? When expliciting the comparator on Collections.sort call, sort operation doesn’t use compareTo implementation, as expected. When not doing this, sort operation uses the implementation of compareTo. Since this method calls the exact same comparator, why do I get NPE here? I mean,

Sort List of string arrays by first element

I want to sort a list of string arrays by first element in each array element of same list, in reverse order, so 2, 1, 0 Here’s what i tried so far: The problem is that at sorted line i have an error highlighted, saying: “sorted(java.util.List, java.util.Comparator Answer Stream.sorted() takes a comparator (in addition to the overload that takes no

understanding comparator in Collections.max

Can somebody please explain what the following lines of code mean?. I had a hard time understanding the Comparator part.I tried googling but all of them were too complex for me to understand. Could somebody please explain it in simpler way? Answer The max method returns the element that is considered the “biggest” in the collection. In this case, you

What is the equivalent way to do Ordering.lexicographical() in Java 8 Comparator?

Is there a way to implement Ordering.lexicographical() with Java 8 Comparator? Comparator.thenCompare seems to be limited in this Answer Seemingly not, no. As a result, Guava still provides this functionality, but in the new class Comparators: https://guava.dev/releases/snapshot/api/docs/com/google/common/collect/Comparators.html#lexicographical(java.util.Comparator). Note that Guava generally does a good job telling you whether and how to migrate off of it to new Java features, so

Java sorting is not the same with MySQL sorting

I need to check the sorting in the table and table content is given by MySQL. I’m trying the following: Collections.sort(sorted, String.CASE_INSENSITIVE_ORDER); And get the following result: tes3@test.com test4@test.com test5@test.com test@test.com test_user@mail.com user-og@driver.com And this is what I get from MySQL by query: SELECT ’email’ FROM ‘user’ WHERE 1 ORDER BY ‘user’.’email’ ASC : tes3@test.com test_user@mail.com test@test.com test4@test.com test5@test.com user-og@driver.com

TreeSet constructor with Comparator parameter

In Java’s documentation for its class TreeSet one of the constructors is shown to have the following header: 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. Answer The elements in a TreeSet are kept sorted. If you use a constructor that

how to test Comparator at junit test

I need to test this method – compare(). Can You get advice? How better I can do this(all part if, else-if, else). After this recomendations – we have next picture (Thank YOU guys a lot!): All is normal testing now. Answer Just instantiate your comparator class and pass in objects:

Advertisement