List of usage examples for java.util Comparator Comparator
Comparator
From source file:Main.java
public static <T> Comparator<Collection<? extends T>> setComparator( final Comparator<? super T> elementComparator) { return new Comparator<Collection<? extends T>>() { @Override//from ww w . j a v a 2s . com public int compare(Collection<? extends T> list1, Collection<? extends T> list2) { return compareAsSet(elementComparator, list1, list2); } }; }
From source file:Main.java
public static <K, V> List<Map.Entry<K, V>> sortMapByValue(HashMap<K, V> map, final int sort) { List<Map.Entry<K, V>> orderList = new ArrayList<Map.Entry<K, V>>(map.entrySet()); Collections.sort(orderList, new Comparator<Map.Entry<K, V>>() { @Override//from ww w. j av a2 s .c om @SuppressWarnings("unchecked") public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { return (((Comparable<V>) o2.getValue()).compareTo(o1.getValue())) * sort; } }); return orderList; }
From source file:Main.java
public static <T, C> Collection<T> sortBy(Collection<T> collection, final Map<T, C> valuesMap) { List<T> ordered = new ArrayList<T>(collection); Comparator<T> compartor = new Comparator<T>() { @SuppressWarnings("unchecked") @Override//from w w w .ja v a 2 s. co m public int compare(T a, T b) { C aValue = valuesMap.get(a); C bValue = valuesMap.get(b); return ((Comparable<C>) aValue).compareTo(bValue); } }; Collections.sort(ordered, compartor); return ordered; }
From source file:Main.java
public static List<Map.Entry<String, Double>> sortMapByValue(Map<String, Double> map) { List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Double>>() { @Override/* w w w.j a va 2s. c om*/ public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { double result = o2.getValue() - o1.getValue(); if (result > 0) return -1; else if (result == 0) return 0; else return 1; } }); return list; }
From source file:Main.java
public static String[][] returnTable(Collection E) { if ((E == null) || E.isEmpty()) { System.out.println("The collection is empty!"); }// w ww .j ava 2 s .c o m Set<Field> collectionFields = new TreeSet<>(new Comparator<Field>() { @Override public int compare(Field o1, Field o2) { return o1.getName().compareTo(o2.getName()); } }); for (Object o : E) { Class c = o.getClass(); createSetOfFields(collectionFields, c); while (c.getSuperclass() != null) { c = c.getSuperclass(); createSetOfFields(collectionFields, c); } } String[][] exitText = new String[E.size() + 1][collectionFields.size() + 1]; exitText[0][0] = String.format("%20s", "Class"); int indexOfColumn = 0; for (Field f : collectionFields) { exitText[0][indexOfColumn + 1] = String.format("%20s", f.getName()); indexOfColumn++; } int indexOfRow = 0; for (Object o : E) { indexOfColumn = 0; exitText[indexOfRow + 1][0] = String.format("%20s", o.getClass().getSimpleName()); for (Field field : collectionFields) { try { field.setAccessible(true); if (field.get(o) instanceof Date) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20tD", field.get(o)); } else { String temp = String.valueOf(field.get(o)); exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", temp); } } catch (Exception e) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", "-"); } indexOfColumn++; } indexOfRow++; } return exitText; }
From source file:Main.java
public static <T extends Comparable> List<Entry<String, T>> sortMapAsc(Map<String, T> keywordMap) { List<Entry<String, T>> arrayList = new ArrayList<Entry<String, T>>(keywordMap.entrySet()); Collections.sort(arrayList, new Comparator<Entry<String, T>>() { @Override// w w w.j a v a2 s . com public int compare(Entry<String, T> e1, Entry<String, T> e2) { return (e1.getValue()).compareTo(e2.getValue()); } }); return arrayList; }
From source file:Main.java
public static <T extends Comparable> List<Entry<String, T>> sortMapDesc(Map<String, T> keywordMap) { List<Entry<String, T>> arrayList = new ArrayList<Entry<String, T>>(keywordMap.entrySet()); Collections.sort(arrayList, new Comparator<Entry<String, T>>() { @Override/*from w w w . j a va2 s. co m*/ public int compare(Entry<String, T> e1, Entry<String, T> e2) { return (e2.getValue()).compareTo(e1.getValue()); } }); return arrayList; }
From source file:Main.java
public static List<Message> getMessagesWithPage(Folder folder, int pageNum, int pageSize) throws MessagingException { Message[] messages = folder.getMessages(); Arrays.sort(messages, new Comparator<Message>() { public int compare(Message o1, Message o2) { Message m1 = (Message) o1;/*w w w. j a va 2 s . co m*/ Message m2 = (Message) o2; if (m1.getMessageNumber() > m2.getMessageNumber()) { return -1; } return 1; } }); List<Message> list = Arrays.asList(messages); int start = (pageNum - 1) * pageSize; int end = pageNum * pageSize; return list.subList(start, end); }
From source file:Main.java
public static <K, V> Map<K, V> sortMapByKey(Map<K, V> map, final Boolean asc) { List<Entry<K, V>> entries = new LinkedList<Entry<K, V>>(map.entrySet()); Collections.sort(entries, new Comparator<Entry<K, V>>() { @SuppressWarnings("unchecked") public int compare(Entry<K, V> o1, Entry<K, V> o2) { if (asc) { return ((Comparable<K>) o1.getKey()).compareTo(o2.getKey()); } else { return -((Comparable<K>) o1.getKey()).compareTo(o2.getKey()); }//from w w w .jav a 2 s . c o m } }); Map<K, V> result = new LinkedHashMap<K, V>(); for (Entry<K, V> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { return (o1.getValue()).compareTo(o2.getValue()); }/*from w w w.java 2s .c om*/ }); Map<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }