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(Map<? extends K, ? extends V> m) 

Source Link

Document

Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.

Usage

From source file:Main.java

/**
 * it returns an ordered Map implementation with the specified initial capacity
 * /*from   w w w .  jav a  2  s.  c  om*/
 * @return
 */
public static <T, D> Map<T, D> newOrderedMap(int initialCapacity) {
    return new LinkedHashMap<>(initialCapacity);
}

From source file:Main.java

public static <K, V> LinkedHashMap<K, V> headMap(LinkedHashMap<K, V> map, int endIndex, boolean inclusive) {
    int index = endIndex;
    if (inclusive) {
        index++;/* w  w  w.j  ava  2  s.c om*/
    }
    if ((map == null) || (map.size() == 0) || (map.size() < index) || (endIndex < 0)) {
        return new LinkedHashMap<K, V>(0);
    }
    LinkedHashMap<K, V> subMap = new LinkedHashMap<K, V>(index + 1);
    int i = 0;
    for (Map.Entry<K, V> entry : map.entrySet()) {
        if (i < index) {
            subMap.put(entry.getKey(), entry.getValue());
        }
        i++;
    }
    return subMap;
}

From source file:Main.java

/**
 * Builds map from list of strings/*from  ww w. j  a v a2s. c  om*/
 *
 * @param args key-value pairs for build a map. Must be a multiple of 2
 * @return Result map. If args not multiple of 2, last argument will be ignored
 */
public static Map<String, Object> mapFrom(Object... args) {
    if (args.length % 2 != 0) {
        Log.w("VKUtil", "Params must be paired. Last one is ignored");
    }
    LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>(args.length / 2);
    for (int i = 0; i + 1 < args.length; i += 2) {
        if (!(args[i] instanceof String))
            Log.e("VK SDK", "Error while using mapFrom",
                    new InvalidParameterSpecException("Key must be string"));
        result.put((String) args[i], args[i + 1]);
    }
    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//from ww w . j av a2 s  . co m
 */
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:MapUtils.java

/**
 * Sorts map by values in ascending order.
 * /*  w w w  .j av  a  2s .  com*/
 * @param <K>
 *            map keys type
 * @param <V>
 *            map values type
 * @param map
 * @return
 */
public static <K, V extends Comparable<V>> LinkedHashMap<K, V> sortMapByValue(Map<K, V> map) {
    List<Entry<K, V>> sortedEntries = sortEntriesByValue(map.entrySet());
    LinkedHashMap<K, V> sortedMap = new LinkedHashMap<K, V>(map.size());
    for (Entry<K, V> entry : sortedEntries) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }

    return sortedMap;
}

From source file:Main.java

public static <K, V> Map<K, V> immutableMap(Map<K, V> map) {
    return Collections.unmodifiableMap(new LinkedHashMap<>(map));
}

From source file:Main.java

/**
 * Combines two lists into map, using first list as keys and second as values
 *
 * @param keys   List used as keys//  w  w  w. ja  va 2s  .c  o  m
 * @param values List used as values
 * @param <K>    Type of values in keys list
 * @param <V>    Type of values in values list
 * @return {@link LinkedHashMap}
 */
public static <K, V> Map<K, V> combineLists(List<? extends K> keys, List<? extends V> values) {
    if (keys.size() != values.size()) {
        throw new IllegalArgumentException("Cannot combine lists with dissimilar sizes");
    }

    if (keys.isEmpty() && values.isEmpty()) {
        return Collections.emptyMap();
    }

    int size = values.size();

    Map<K, V> map = new LinkedHashMap<>(size);
    for (int i = 0; i < size; i++) {
        map.put(keys.get(i), values.get(i));
    }

    return map;
}

From source file:Main.java

public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(final int initialCapacity) {
    return new LinkedHashMap<K, V>(initialCapacity);
}

From source file:edu.northwestern.bioinformatics.studycalendar.tools.MapBasedDictionary.java

public static <K, V> Dictionary<K, V> copy(Map<K, V> src) {
    return new MapBasedDictionary<K, V>(new LinkedHashMap<K, V>(src));
}

From source file:Main.java

public static <KEY, VALUE> LinkedHashMap<KEY, VALUE> newLinkedHashMap(Map<KEY, VALUE> map) {
    return new LinkedHashMap<KEY, VALUE>(map);
}