List of usage examples for java.util LinkedList LinkedList
public LinkedList(Collection<? extends E> c)
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 . j av 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> Map<K, V> sortMapByValue(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<V>) o1.getValue()).compareTo(o2.getValue()); } else { return -((Comparable<V>) o1.getValue()).compareTo(o2.getValue()); }/*from www .ja va 2 s . co 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 ww w . j a v a 2s. c o m*/ }); Map<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { 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.Entry<K, V>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { public int compare(Entry<K, V> o1, Entry<K, V> o2) { return (o2.getValue()).compareTo(o1.getValue()); }/*from w ww . ja v a 2 s . c o m*/ }); Map<K, V> result = new LinkedHashMap<K, V>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
@SuppressWarnings("unchecked") public static HashMap<?, ?> sortByValue(HashMap<?, ?> map, final int flag) { // flag = 0 decreasing order otherwise increasing List list = new LinkedList(map.entrySet()); Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { if (flag == 0) return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue()); else/*www. j av a 2 s. c om*/ return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); } }); HashMap result = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
/** * Sort a map by supplied comparator logic. * * @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map. * @author Maxim Veksler// ww w .j ava 2 s .com */ public static <K, V> LinkedHashMap<K, V> sortMap(final Map<K, V> map, final Comparator<Map.Entry<K, V>> comparator) { // Convert the map into a list of key,value pairs. List<Map.Entry<K, V>> mapEntries = new LinkedList<Map.Entry<K, V>>(map.entrySet()); // Sort the converted list according to supplied comparator. Collections.sort(mapEntries, comparator); // Build a new ordered map, containing the same entries as the old map. LinkedHashMap<K, V> result = new LinkedHashMap<K, V>(map.size() + (map.size() / 20)); for (Map.Entry<K, V> entry : mapEntries) { // We iterate on the mapEntries list which is sorted by the comparator putting new entries into // the targeted result which is a sorted map. result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static HashMap<?, ?> sortByValue(HashMap<?, ?> map, final int flag) { // flag = 0 decreasing order otherwise increasing List<Map.Entry<?, ?>> list = new LinkedList<Map.Entry<?, ?>>(map.entrySet()); Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { if (flag == 0) return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue()); else//w ww .j a va 2 s . c o m return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); } }); HashMap result = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
/** * Sorts given map by given comparator//w w w . jav a 2s . c o m * * @param map The map * @param comparator The comparator to sort the map * @param <K> The key type * @param <V> The value type * @return The sorted map */ public static <K, V> Map<K, V> sortMapByValue(Map<K, V> map, Comparator<Map.Entry<K, V>> comparator) { List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); list.sort(comparator); Map<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:Main.java
/** * sort Map by key desc/*from ww w .j a va 2 s. com*/ * * @param unsortMap * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Map sortByComparator(Map unsortMap) { List list = new LinkedList(unsortMap.entrySet()); // sort list based on comparator Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey()); } }); // put sorted list into map again Map sortedMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; }
From source file:Main.java
public static List<Integer> reverse(List<Integer> aList) { List<Integer> tmp = new LinkedList<>(aList); Collections.reverse(tmp);//w w w.j a va 2 s.co m return tmp; }