Example usage for java.util Map entrySet

List of usage examples for java.util Map entrySet

Introduction

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

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:Main.java

public static <K, V> Map.Entry<K, V> getFirstEntry(Map<K, V> map) {
    try {/*from w  ww .ja  v a 2s . co m*/
        return map.entrySet().iterator().next();
    } catch (NullPointerException e) {
        return null;
    } catch (NoSuchElementException e) {
        return null;
    }
}

From source file:Main.java

public static <T, U> void putIfAbsent(Map<T, U> destination, Map<T, U> source) {
    for (Entry<T, U> entry : source.entrySet()) {
        if (!destination.containsKey(entry.getKey())) {
            destination.put(entry.getKey(), entry.getValue());
        }//ww  w  . jav  a 2s  .  c  o  m
    }
}

From source file:Main.java

public static <V, T> void showMap(Map<V, T> map) {
    Set<Map.Entry<V, T>> set = map.entrySet();
    for (Iterator<Map.Entry<V, T>> it = set.iterator(); it.hasNext();) {
        Map.Entry<V, T> entry = it.next();
        System.out.println(entry.getKey() + "----->" + entry.getValue());
    }//from  www.jav a 2 s . c om
}

From source file:Main.java

public static <V, T> List<Map.Entry<V, T>> MapToList(Map<V, T> map) {
    Set<Map.Entry<V, T>> set = map.entrySet();
    List<Map.Entry<V, T>> list = new ArrayList<Map.Entry<V, T>>();
    for (Iterator<Map.Entry<V, T>> it = set.iterator(); it.hasNext();) {
        Map.Entry<V, T> entry = it.next();
        list.add(entry);/*from   w  ww .ja  v  a2  s  .  c o m*/
    }
    return list;
}

From source file:Main.java

public static <K, V> Set<Map.Entry<K, V>> entrySet(Map<K, V> map) {
    return map == null ? null : map.entrySet();
}

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. j a  v a2 s .co  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 <T> boolean removeByValue(Map<?, T> map, T value) {
    for (Map.Entry<?, T> o : map.entrySet()) {
        if (o.getValue() == value) {
            map.remove(o.getKey());//from   www .ja  v  a2s.  c o m
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static void print(Map map) {
    Iterator iter = map.entrySet().iterator();
    Map.Entry me;//  w ww.jav a2s.  c  o  m
    System.out.println("Printing Map: ");
    while (iter.hasNext()) {
        me = (Map.Entry) iter.next();
        System.out.println("   " + me.getKey() + " - " + me.getValue());
    }
}

From source file:Main.java

public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
    for (Map.Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            return entry.getKey();
        }/*from ww w  .  j a v a 2 s .c om*/
    }
    return null;
}

From source file:Main.java

public static <K, V> void printMap(Map<K, V> map, String seperator) {
    for (Entry<K, V> entry : map.entrySet()) {
        System.out.println(entry.getKey() + seperator + entry.getValue());
    }/*from w  w  w. j  a  v  a2  s  . c o  m*/
}