Example usage for java.util Map keySet

List of usage examples for java.util Map keySet

Introduction

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

Prototype

Set<K> keySet();

Source Link

Document

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

Usage

From source file:Main.java

public static Object getKeyFromValue(Map hm, Object value) {
    for (Object o : hm.keySet()) {
        if (hm.get(o).equals(value)) {
            return o;
        }/*www  .j av a  2 s. c  o m*/
    }
    return null;
}

From source file:Main.java

public static List getMapKey(Map map) {
    Object[] arr = map.keySet().toArray();
    return Arrays.asList(arr);
}

From source file:Main.java

/**
 * This method does the same as getAllKeys, and while unused at the time of creation, may be useful for dotNet in the future.
 * The motivation for this method is that in dotNet, returning the keys as a Set requires constructing
 * a new object, whereas returning a collection does not -- so the custom implementation of this method in
 * dotNet is cheaper. Thus, this getAllKeysAsSimpleCollection method is preferable to getAllKeys even in Java, unless
 * the caller needs to call methods contains or containsAll in the resulting collection.
 *
 * In the latter case, i.e. calling code using contains or containsAll, using a Set in Java (i.e. method CollectionHelper.getAllKeys)
 * is preferable because methods contains (lowecase 'c') and containsAll are not available in dotNet's ICollection, only in
 * the ADHashSet. That being said, this is only the case if the method is not called frequently -- if it is, we
 * should add a CollectionHelper.containsKey method, and calling code should use it instead.
 *///  ww  w.  ja  v  a2s .  c o m
public static <K, V> Collection<K> getAllKeysAsSimpleCollection(Map<K, V> map) {
    return map.keySet();
}

From source file:Main.java

public static <K> void aggregateMaps(Map<K, Integer> A, Map<K, Integer> B) {
    for (K k : B.keySet()) {
        if (A.containsKey(k))
            A.put(k, A.get(k) + B.get(k));
        else//from  w  w  w .  j a v a  2  s .  co  m
            A.put(k, B.get(k));
    }
}

From source file:Main.java

License:asdf

public static <K, V> Object getKey(Map<K, V> in, V value) {
    Set<K> key = in.keySet();
    Iterator<K> keyIterator = key.iterator();
    while (keyIterator.hasNext()) {
        K valueObject = (K) keyIterator.next();
        if (in.get(valueObject).equals(value)) {
            return valueObject;
        }/*from w  w  w  .  j  av a2 s.c om*/
    }
    return null;
}

From source file:Main.java

/**
 * <p>turn source map's to list</p>
 * @param map source map//www. j  av a2s.com
 * @return
 */
public static <T extends Object> List<T> turnMapKeyToList(Map map) {
    return new ArrayList<T>(map.keySet());
}

From source file:Main.java

public static String getMapString(Map map) {
    Set set = map.keySet();
    if (set.size() < 1) {
        return "[]";
    }//  w ww .  j  a  v  a2  s .  co m
    StringBuilder strBuilder = new StringBuilder();
    Object[] array = set.toArray();
    strBuilder.append("[").append(array[0]).append("=").append(map.get(array[0]));
    for (int i = 1; i < array.length; ++i) {
        strBuilder.append(", ");
        strBuilder.append(array[i]).append("=");
        strBuilder.append(map.get(array[i]));
    }
    strBuilder.append("]");
    return strBuilder.toString();
}

From source file:Main.java

public static void printMapBySet(Map map) {
    Set<String> ketSet = map.keySet();
    Iterator<String> iterator = ketSet.iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        String value = (String) map.get(key);
        Log.i(TAG, "----key = " + key + ",value = " + value);
    }//from  w  w  w  . j av a 2s. c om

}

From source file:Main.java

public static Object getKeyFromValue(Map map, Object value) {
    for (Object o : map.keySet()) {
        if (map.get(o).equals(value)) {
            return o;
        }//from  w  w w  .ja  v a  2  s  .  c  o m
    }

    return null;
}

From source file:RandomGenTest.java

public static void display(Map<Object, Integer> map) {
    for (Object age : map.keySet()) {
        System.out.print(age + "," + map.get(age) + " ");
        int l = map.get(age) / 50;
        for (int i = 0; i < l; i++)
            System.out.print("*");

        System.out.println("");
    }/* ww  w.  ja  va 2s .  c om*/
}