comparator

Collections.sort() Comparison method violates its general contract in Java [duplicate]

社会主义新天地 提交于 2020-01-30 13:17:07
问题 This question already has answers here : “Comparison method violates its general contract!” (11 answers) Closed 2 years ago . I know that this kind of question has been asked millions of times if not billions, however I couldn't find my answer yet :) This compare() method doesn't have Long , Double , Float , ..., it only has Date , boolean , and Null checker, however it shows me that contract violation error , can any one help plz? Collections.sort(users, new Comparator<MiniUser>() {

How to use thenComparing in java stream

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-30 02:53:12
问题 I have a map with strings as values. I want to sort it firstly by length, and if length of the strings is the same, i want to sort it alphabetic. I wrote those code : String out = outMap.values().stream() .sorted(Comparator.comparing(e -> e.length()).thenComparing()...) .collect(Collectors.joining()); The problem is, when i am writing thenComparing, I couldn't use e.length() anymore. How can i fix it? EDIT : Map<Character, String> . I want to sort the strings and make one string in output by

Java: Sorting an array based on another array with indexOf method

时光毁灭记忆、已成空白 提交于 2020-01-22 19:42:06
问题 I want to iterate through two arrays(A, B) based on the sorted order of another array(indexes), which is 10, 34, 32, 21 in this case. String[] A: a, b, c, d String[] B: e, f, g, h int[] indexes: 10, 34, 32, 21 Apology for the bad example here. I have updated the indexes array to clear the confusion. Expected Input and Output The input are the three arrays. I wanted to iterate through A, B using the sorted of the indexes array. i.e. I want to find a way to iterate A using the order (a, d, c, b

Not implementing all the methods of an interface

不羁的心 提交于 2020-01-22 13:52:05
问题 I tried reproducing the code below on eclipse. I get an error telling me that I have to implement all the inherited methods (because Comparator is an interface). The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() . There are many of these methods and the only one I want to overwrite is compare. Do I have to implement all the other methods or is there a way to specify I don't need to implement them? I understand that I will have to do it because of

Not implementing all the methods of an interface

一笑奈何 提交于 2020-01-22 13:51:07
问题 I tried reproducing the code below on eclipse. I get an error telling me that I have to implement all the inherited methods (because Comparator is an interface). The type new Comparator(){} must implement the inherited abstract method Comparator.reversed() . There are many of these methods and the only one I want to overwrite is compare. Do I have to implement all the other methods or is there a way to specify I don't need to implement them? I understand that I will have to do it because of

Java: SortedMap, TreeMap, Comparable? How to use?

帅比萌擦擦* 提交于 2020-01-19 05:36:27
问题 I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this. Do I implement Comparable with the class I'm sorting, or do I create a new class? How do I instantiate the SortedMap and pass in the Comparator? How does the sorting work? Will it automatically sort everything as new objects are inserted? EDIT: This code is giving me an error: private TreeMap<Ktr> collection = new TreeMap<Ktr>(); (Ktr

Java: SortedMap, TreeMap, Comparable? How to use?

▼魔方 西西 提交于 2020-01-19 05:36:09
问题 I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this. Do I implement Comparable with the class I'm sorting, or do I create a new class? How do I instantiate the SortedMap and pass in the Comparator? How does the sorting work? Will it automatically sort everything as new objects are inserted? EDIT: This code is giving me an error: private TreeMap<Ktr> collection = new TreeMap<Ktr>(); (Ktr

Sorting Guava table on values

不羁的心 提交于 2020-01-16 06:34:44
问题 Is there any possible way to do that? The expecting effect would be that rowMap and columnMap entry values would be sorted by value. The problem is that I cannot create a comparator without the underlying maps in Table. Table table = TreeBasedTable.create(?,?); Map<String, Map<String, String>> rowMap = table.rowMap(); Map<String, String> thisMapShouldBeSortedByValues = rowMap.get(smth); Map<String, Map<String, String>> columnMap = table.columnMap(); Map<String, String>

what is the importance of <Integer,Integer> in Map.Entry.<Integer, Integer>comparingByValue()

会有一股神秘感。 提交于 2020-01-16 00:41:24
问题 I am trying to sort elements by their frequency import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int itr = Integer.parseInt(br.readLine()); for (int i = 0; i < itr; i++) { int n = Integer.parseInt(br.readLine()); String[] val = br.readLine

Using comparator in custom quicksort in java

强颜欢笑 提交于 2020-01-15 03:18:07
问题 I have a quicksort method that i implemented myself. i give an array of objects to that method. how do i use a comparator to tell the method which attribute the objects will be sorted by? I have googled around and found out how to implement a comparator, but not how to use it in the search method as every example ive found just used arrays.sort(). i need different getter-methods to get to the different attributes, i dont see how a comparator helps with that? i just need a little help to