comparator

Java version number sort

亡梦爱人 提交于 2019-12-10 14:11:42
问题 String[] k1 = {"0.10", "0.2", "0.1", "0", "1.10", "1.2", "1.1", "1", "2.10", "2", "2.2", "2.1"}; double[] k2 = {0.10, 0.2, 0.1, 0, 1.10, 1.2, 1.1, 1, 2.10, 2, 2.2, 2.1}; Arrays.sort(k1); Arrays.sort(k2); System.out.println(Arrays.toString(k1)); System.out.println(Arrays.toString(k2)); output: [0, 0.1, 0.10, 0.2, 1, 1.1, 1.10, 1.2, 2, 2.1, 2.10, 2.2] [0.0, 0.1, 0.1, 0.2, 1.0, 1.1, 1.1, 1.2, 2.0, 2.1, 2.1, 2.2] What I would like to have, [0, 0.1, 0.2, 0.10, 1, 1.1, 1.2, 1.10, 2, 2.1, 2.2, 2.10]

Java TreeMap custom comparator weird behaviour

ⅰ亾dé卋堺 提交于 2019-12-10 12:56:21
问题 I am trying to create a Map with sorted keys, sorted according to alphabetically first, and numerical last. For this I am using a TreeMap with a custom Comparator : public static Comparator<String> ALPHA_THEN_NUMERIC_COMPARATOR = new Comparator<String> () { @Override public int compare(String first, String second) { if (firstLetterIsDigit(first)) { return 1; } else if (firstLetterIsDigit(second)) { return -1; } return first.compareTo(second); } }; private static boolean firstLetterIsDigit

get the three highest values in a TreeMap

岁酱吖の 提交于 2019-12-10 07:50:01
问题 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

Why does my compare methd throw IllegalArgumentException sometimes?

你。 提交于 2019-12-10 04:08:04
问题 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

Java 8 Comparator comparing doesn't chain

和自甴很熟 提交于 2019-12-10 02:31:52
问题 Let's say I have a Pair class public class Pair<P, Q> { public P p; public Q q; public Pair(P p, Q q) { this.p = p; this.q = q; } public int firstValue() { return ((Number)p).intValue(); } public int secondValue() { return ((Number)q).intValue(); } } And I wish to sort it, first by first value, then by second value. Now' if I do this List<Pair<Integer, Integer>> pairList = new ArrayList<>(); pairList.add(new Pair<>(1, 5)); pairList.add(new Pair<>(2, 2)); pairList.add(new Pair<>(2, 22));

Java Collections.sort - help me remove the unchecked warning

我怕爱的太早我们不能终老 提交于 2019-12-10 02:01:01
问题 List<Question> questions = new ArrayList<Question>(); questions.addAll(getAllQuestions()); //returns a set of Questions Collections.sort(questions, new BeanComparator("questionId")); //org.apache.commons.beanutils.BeanComparator Under Java 1.5, the above works fine except that the 'new BeanComparator("questionId")' generates an unchecked warning. I do not like warnings. Is there a way I can provide the BeanComparator a type, or do I have to use @SuppressWarnings("unchecked") ? 回答1: Options

Sort the outer map by the value of the nested map

爷,独闯天下 提交于 2019-12-10 00:32:54
问题 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. 回答1: 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,

Get objects from List of objects based on variable in object

杀马特。学长 韩版系。学妹 提交于 2019-12-09 16:35:58
问题 I have List of User object, I just want to get User objects from List based on variables in User object. public class User { private int id; private String sex; private int age; private String country; /** * Getter and setter for all variables */ } I have a model class like this. Now I have list of User objects. List<User> users = new ArrayList<User>(); I want to get the objects from users list if the user is Male. List<User> ageList = new ArrayList<User>(); for(User object : users) { if

Compare two Java Collections using Comparator instead of equals()

人盡茶涼 提交于 2019-12-09 08:30:42
问题 Problem Statement I have two Collections of the same type of object that I want to compare. In this case, I want to compare them based on an attribute that does not factor into equals() for the Objects. In my example, I'm using ranked collections of Names for instance: public class Name { private String name; private int weightedRank; //getters & setters @Override public boolean equals(Object obj) { return this.name.equals(obj.name); //Naive implementation just to show //equals is based on

Comparator best practice

半腔热情 提交于 2019-12-09 06:22:01
问题 If I implement a custom comparator is it considered good practice to overide equals besides compare ? Additionally is there a defined contract for a Comparator ? 回答1: The contract of Comparator is defined in its javadoc. In particular: Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a