comparator

Passing Object type and Field to Comparator

陌路散爱 提交于 2019-12-23 04:24:10
问题 Is it possible to write a Comparator so that I can pass the Object type, the field type and the field I like to sort on? I've made some small changes to http://www.davekoelle.com/files/AlphanumComparator.java to accommodate sorting on the field email of type String in the Object type User . I have this code that works. public class Main { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User(7, "user1", "user1@c.com")); users.add(new User(11,

in java, use String::length for Comparator.comparing()

孤街醉人 提交于 2019-12-22 17:59:21
问题 I have a code piece like this: String[] yetToSortedArray = {"Abc","hello world","nihao?","chilemaNin"}; Arrays.parallelSort(yetToSortedArray, Comparator.comparing(String::length)); for(String str : yetToSortedArray){ System.out.println(str + ", "); } My question is here: "String::length" What am I really passing in to Comparator.comparing()? Why there is no parenthesis for String::length()? I think I am using this : https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing

Implementing Java Priority Queue

安稳与你 提交于 2019-12-22 12:58:32
问题 public class PriorityQueue<T> { private PriorityNode<T> head, tail; private int numItems; public PriorityQueue(){ numItems = 0; head=null; tail=null; } public void add(int priority, T value){ PriorityNode<T> newNode = new PriorityNode<T>(priority,value); if(numItems == 0){ head = newNode; tail = newNode; } else{ head.setNext(newNode); head = newNode; } } } Where PriorityNode is defined as: public class PriorityNode<T> implements Comparable<T> { private T value; private PriorityNode<T> next;

Comparator<File> for “directories-first” order

廉价感情. 提交于 2019-12-22 12:54:23
问题 I'm stumped... Let's say I have this directory tree: {someRoot}/ {someRoot}/bar/ {someRoot}/bar/file1.txt {someRoot}/foo/ {someRoot}/foo/baz/ {someRoot}/foo/baz/file3.txt {someRoot}/foo/abracadabra.txt {someRoot}/foo/file2.txt {someRoot}/aardvark.txt {someRoot}/food.txt {someRoot}/zebra.txt You'll note the ordering. Call this order1 . At each stage, the directories come first before the files. ( NOTE: bar/file1.txt comes before foo , so on a global basis, the directories do not all come

Sorting Map using Comparator

旧时模样 提交于 2019-12-22 10:26:41
问题 I'm trying Comparator to implement a sort in TreeMap according to a sequence. final String sequence="People,Object,Environment,Message,Service"; Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String key1, String key2) { int returned = sequence.indexOf(key1) - sequence.indexOf(key2); if (returned == 0 && !key1.contains(key2)) returned = -1; return returned; } }; List<String> list=new ArrayList<String>(); Map<String,String> lhm = new TreeMap<String

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

心已入冬 提交于 2019-12-22 09:42:10
问题 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

Working of Comparator.nullsFirst when both are null

我们两清 提交于 2019-12-22 05:11:24
问题 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

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

一世执手 提交于 2019-12-22 04:28:10
问题 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

Chain of comparators in java

混江龙づ霸主 提交于 2019-12-22 03:49:01
问题 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

Partial sort Collection with limit and custom Comparator

微笑、不失礼 提交于 2019-12-21 20:17:33
问题 I want to sort an ArrayList called imageList like this: Collections.sort(imageList, new MapComparator(Function.KEY_TIMESTAMP, "dsc")); This works fine, but now I want to be able to set a limit (show only the newest 100 images, where the ArrayList is unsorted, so simply creating a sublist won't work) for performance reasons. My MapComparator class looks like this: class MapComparator implements Comparator<HashMap<String, String>> { private final String key; private final String order; public