comparator

In Java, sort hash map by its key.length()

◇◆丶佛笑我妖孽 提交于 2019-12-05 07:13:27
i have a hashmap like this: HashMap<String,Integer> map = new HashMap<String,Integer>(); map.put("java",4); map.put("go",2); map.put("objective-c",11); map.put("c#",2); now i want to sort this map by its key length, if two keys length are equal (e.g go and c# both length 2), then sorted by alphba order. so the outcome i expect to get is something like: printed result: objective-c, 11 java, 4 c#, 2 go, 2 here is my own attamp, but it doesnt work at all... HashMap<String,Integer> map = new HashMap<String,Integer>(); map.put("java",4); map.put("go",2); map.put("objective-c",11); map.put("c#",2);

Move specific items to the end of a list

巧了我就是萌 提交于 2019-12-05 06:27:07
I have an ArrayList in Java : {"deleteItem", "createitem", "exportitem", "deleteItems", "createItems"} I want to move all string which contains delete to the end of the list, so I would get the next: {"createitem", "exportitem", "createItems", "deleteItem", "deleteItems"}` I can create two sublists - one for the words which contain the 'delete' word, and one for the others, and then merge them, but I search for a more efficient way. Kristjan Veskimäe Use custom Comparator: List<String> strings = Arrays.asList( "deleteItem", "createitem", "exportitem", "deleteItems", "createItems" ); Comparator

Why does my compare methd throw IllegalArgumentException sometimes?

穿精又带淫゛_ 提交于 2019-12-05 05:50:02
I am having this problem for some time, have searched lots of StackOverflow questions but couldn't solve my problem. I also asked a similar question before and got the suggestion to use, System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); It didn't solve my problem. I never got this exception on any of my test devices, but some of my users have been reporting it regularly. I am really clueless how to solve it. The Exception This is the exception that I am getting, java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeLo

Chain of comparators in java

泪湿孤枕 提交于 2019-12-05 03:06:54
Reading the Java Tutorial by Oracle on interfaces which gives a example on Card (Playing cards) I was trying to understand the default methods in interfaces . Here's the link , section "Integrating default methods in existing interfaces". Now in the last section they sorted the Cards first by rank and then by suits. Following logics have been given. Assume that whatever interfaces, functions or classes that are used have been defined and sort function takes a Comparator Logic 1: package defaultmethods; import java.util.*; import java.util.stream.*; import java.lang.*; public class

How to sort List<File> to list directories first and grouping files by directory?

十年热恋 提交于 2019-12-05 03:00:28
In order to get all files contained in a specified directory and according to some extensions, I'm using the method listFiles of class FileUtils from Apache Commons IO library, as in the following code sample. ArrayList<String> wildcards = new ArrayList<>(); wildcards.add("*.cpp"); wildcards.add("*.h"); wildcards.add("*.txt"); File dir = new File("/path/to/dir"); Collection<File> found = FileUtils.listFiles( dir, new WildcardFileFilter(wildcards, IOCase.SENSITIVE), DirectoryFileFilter.DIRECTORY); List<File> files = new ArrayList<>(found); The order of items in the resulting Collection<File>

Comparator<String> must override super class method

本秂侑毒 提交于 2019-12-05 02:03:43
I'm making a TreeMap<String, String> and want to order it in a descending fashion. I created the following comparator: Comparator<String> descender = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }; I construct the TreeMap like so: myMap = new TreeMap<String, String>(descender); However, I'm getting the following error: The method compare(String, String) of type new Comparator<String>(){} must override a superclass method I've never fully groked generics, what am I doing wrong? Your Eclipse project is apparently set to Java 1.5. The

std::is_sorted and strictly less comparison?

六眼飞鱼酱① 提交于 2019-12-05 00:41:00
I do not understand well the std::is_sorted algorithm and its default behaviour. If we look to cppreference , it says that by default std::is_sorted uses the < operator. Instead of that, I find that using <= would be natural. But my problem is that for the following list of numbers : 1 2 3 3 4 5 it will return true , even if 3 < 3 should be false . How is that possible ? EDIT: its seems to be worse than what I thought, because passing std::less_equal<int> will return false in that case... What is the condition applied when I pass a comparator function? Per 25.4/5: A sequence is sorted with

Implement Comparator for primitive boolean type?

落爺英雄遲暮 提交于 2019-12-04 23:05:36
I need some classes implements Comparator , and for one I want to compare primitive boolean (not Boolean ) values. IF it was a B oolean, I would just return boolA.compareTo(boolB); which would return 0, -1 or 1. But how can I do this with primitives? You can look up how it is implemented for the java.lang.Boolean , since that class, naturally, uses a primitive boolean as well: public int compareTo(Boolean b) { return (b.value == value ? 0 : (value ? 1 : -1)); } mkobit Since Java 7, the logic that Marko Topolnik showed in his answer has moved into another method to expose a way to compare

Java 8: implementing Comparable

夙愿已清 提交于 2019-12-04 22:12:34
问题 I like the new static factory methods of Comparator , as they allow to implement Comparators in a very concise and less error-prone way. But what is the recommended way to implement Comparable ? Should we use Comparators inside the Comparable implementation? public MyClass implements Comparable<MyClass>{ ... public int compareTo(MyClass other){ Comparator<MyClass> naturalOrderComparator = Comparator.comparing(MyClass::getFoo) .thenComparing(MyClass::getBar); return naturalOrderComparator

Sort the outer map by the value of the nested map

倾然丶 夕夏残阳落幕 提交于 2019-12-04 22:05:58
Sorting the outer map Map<String, Map<String, List<Integer>>> by the list size in the nested map retaining the outer and inner keys as before. You may solve this by generalizing the process: private static <K,V,R> Map<K,R> replaceAndSortValues(Map<K,V> m, Function<V,R> f, Comparator<R> c) { return m.entrySet().stream() .map(e -> Map.entry(e.getKey(), f.apply(e.getValue()))) .sorted(Map.Entry.comparingByValue(c.reversed())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a,b) -> { throw new AssertionError(); }, LinkedHashMap::new)); } This method creates a new map with the