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

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  a  v 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

@SuppressWarnings("unchecked")
public static void addToMapInMap(Object keyOuter, Object keyInner, Object value, Map theMap) {
    if (theMap == null)
        return;/*from  w  w  w .  ja  v  a2 s .com*/
    Map innerMap = (Map) theMap.get(keyOuter);
    if (innerMap == null) {
        innerMap = new LinkedHashMap();
        theMap.put(keyOuter, innerMap);
    }
    innerMap.put(keyInner, value);
}

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

    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

License:asdf

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> sort(Map<K, V> in, Comparator<? super V> compare) {
    Map<K, V> result = new LinkedHashMap<K, V>();
    V[] array = (V[]) in.values().toArray();
    for (int i = 0; i < array.length; i++) {

    }//www.  j a  v a 2  s.  c om
    Arrays.sort(array, compare);
    for (V item : array) {
        K key = (K) getKey(in, item);
        result.put(key, item);
    }
    return result;
}

From source file:Main.java

@Nonnull
public static <K, V> Map<K, V> asMap(Object... keyAndValues) throws IllegalArgumentException {
    final Map<K, V> result = new LinkedHashMap<K, V>();
    Object lastKey = VOID;//from  w w  w.j av  a  2s .  com
    if (keyAndValues != null) {
        for (Object keyOrValue : keyAndValues) {
            //noinspection ObjectEquality
            if (lastKey == VOID) {
                lastKey = keyOrValue;
            } else {
                // noinspection unchecked
                result.put((K) lastKey, (V) keyOrValue);
                lastKey = VOID;
            }
        }
    }
    //noinspection ObjectEquality
    if (lastKey != VOID) {
        throw new IllegalArgumentException("There is no value for key: " + lastKey);
    }
    return result;
}

From source file:Main.java

/**
 * sort Map by key desc//from w  w w.jav  a2 s . co m
 * 
 * @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

/** Creates a single Map with fields from the passed in Map and all nested Maps (for Map and Collection of Map entry values) */
@SuppressWarnings("unchecked")
public static Map flattenNestedMap(Map theMap) {
    if (theMap == null)
        return null;
    Map outMap = new LinkedHashMap();
    for (Object entryObj : theMap.entrySet()) {
        Map.Entry entry = (Map.Entry) entryObj;
        Object value = entry.getValue();
        if (value instanceof Map) {
            outMap.putAll(flattenNestedMap((Map) value));
        } else if (value instanceof Collection) {
            for (Object colValue : (Collection) value) {
                if (colValue instanceof Map)
                    outMap.putAll(flattenNestedMap((Map) colValue));
            }/*  ww w  . j a va2  s  . c  o  m*/
        } else {
            outMap.put(entry.getKey(), entry.getValue());
        }
    }
    return outMap;
}

From source file:Main.java

/**
 * Converts the given pair of lists into a map that maps each keys[i] to
 * the corresponding values[i]./*from  w  w  w .  ja va2  s  .c o  m*/
 * @return the map (empty map if keys == null or values == null)
 */
public static <K, V> Map<K, V> asMap(List<K> keys, List<V> values) {
    Map<K, V> map = new LinkedHashMap<K, V>();
    if (keys == null || values == null) {
        return map;
    }

    for (int i = 0, len = Math.min(keys.size(), values.size()); i < len; i++) {
        map.put(keys.get(i), values.get(i));
    }
    return map;
}

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/* w w w. j  a va2  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

/**
 * This method will find all the parameters under this <code>paramsElement</code> and return them as
 * Map<String, String>. For example,
 * <pre>/* w  ww .  ja v a2 s . co  m*/
 *   <result ... >
 *      <param name="param1">value1</param>
 *      <param name="param2">value2</param>
 *      <param name="param3">value3</param>
 *   </result>
 * </pre>
 * will returns a Map<String, String> with the following key, value pairs :-
 * <ul>
 *  <li>param1 - value1</li>
 *  <li>param2 - value2</li>
 *  <li>param3 - value3</li>
 * </ul>
 *
 * @param paramsElement
 * @return
 */
public static Map getParams(Element paramsElement) {
    LinkedHashMap params = new LinkedHashMap();

    if (paramsElement == null) {
        return params;
    }

    NodeList childNodes = paramsElement.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);

        if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) {
            Element paramElement = (Element) childNode;
            String paramName = paramElement.getAttribute("name");

            String val = getContent(paramElement);
            if (val.length() > 0) {
                params.put(paramName, val);
            }
        }
    }
    return params;
}