Example usage for java.util LinkedHashMap LinkedHashMap

List of usage examples for java.util LinkedHashMap LinkedHashMap

Introduction

In this page you can find the example usage for java.util LinkedHashMap LinkedHashMap.

Prototype

public LinkedHashMap() 

Source Link

Document

Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).

Usage

From source file:Main.java

@SuppressWarnings("unchecked")
static public LinkedHashMap<Object, Comparable> sortHashMapByValues(HashMap<Object, Comparable> passedMap) {
    ArrayList mapKeys = new ArrayList(passedMap.keySet());
    ArrayList mapValues = new ArrayList(passedMap.values());
    Collections.sort(mapValues);/*from   www.java 2s  . c  o m*/
    Collections.sort(mapKeys);

    LinkedHashMap<Object, Comparable> sortedMap = new LinkedHashMap<Object, Comparable>();

    Iterator<Comparable> valueIt = mapValues.iterator();
    while (valueIt.hasNext()) {
        Comparable val = valueIt.next();
        Iterator keyIt = mapKeys.iterator();

        while (keyIt.hasNext()) {
            Object key = keyIt.next();
            Comparable comp = passedMap.get(key);

            if (comp.equals(val)) {
                passedMap.remove(key);
                mapKeys.remove(key);
                sortedMap.put(key, val);
                break;
            }

        }
    }
    return sortedMap;
}

From source file:Main.java

public static Map<Object, Object> copySafelyToObjectToObjectMap(Map<?, ?> map) {
    Map<Object, Object> castedCopy = new LinkedHashMap<Object, Object>();

    if (map == null) {
        return castedCopy;
    }/*from   ww w  .j  a  v  a  2 s . c  o  m*/

    Iterator<?> it = map.keySet().iterator();
    while (it.hasNext()) {
        Object nextKey = it.next();
        castedCopy.put(nextKey, map.get(nextKey));
    }
    return castedCopy;
}

From source file:Main.java

public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
    List list = new LinkedList(map.entrySet());
    Collections.sort(list, new Comparator() {

        @Override/*from   w w w.ja  v a2  s  . c o  m*/
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
        }
    });

    Map 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

public static Map<String, String> convertFromString(String text) {
    Map<String, String> result = new LinkedHashMap<String, String>();
    List<String> keyValuePairs = decode(text, ';');
    for (String pair : keyValuePairs) {
        List<String> keyAndValue = decode(pair, '=');
        String key = keyAndValue.get(0);
        String value = keyAndValue.get(1);
        result.put(key, value);// ww w  . j ava  2 s . c  om
    }
    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 ww . j  a  v a2s  . 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> LinkedHashMap<K, V> createLinkedHashMap(Map<? extends K, ? extends V> map) {
    if (map == null) {
        return new LinkedHashMap<K, V>();
    }// www . j  a  v  a  2 s. co m

    return new LinkedHashMap<K, V>(map);
}

From source file:MyComparator.java

public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) {

    List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet());

    Collections.sort(list, new MyComparator());

    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    for (Entry<String, Integer> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }//from www  .j a v a 2 s  .c  om
    return sortedMap;
}

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());
        }/*  w ww . j  av a 2s  .c om*/

    });
    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

public static <K, V> Map<K, V> mapOf(K key, V value) {
    Map<K, V> map = new LinkedHashMap<K, V>();
    map.put(key, value);/*w  w  w  . j a v  a2 s  . com*/
    return map;
}

From source file:Main.java

public static Map<String, Object> map(final String key, final Object value) {
    LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
    map.put(key, value);/*from  w w  w.jav a 2s. c  om*/
    return map;
}