Example usage for java.util HashMap HashMap

List of usage examples for java.util HashMap HashMap

Introduction

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

Prototype

public HashMap() 

Source Link

Document

Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

Usage

From source file:MainClass.java

public static void main(String[] a) {

    Map map = new HashMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Set set = map.entrySet();// ww w.  j  av  a  2 s .  co m

    Iterator iter = set.iterator();

    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        System.out.println(entry.getKey() + " -- " + entry.getValue());
    }
}

From source file:Main.java

public static void main(String[] args) {
    HashMap<String, String> hMap = new HashMap<String, String>();

    hMap.put("1", "One");
    hMap.put("2", "Two");
    hMap.put("3", "Three");

    Set st = hMap.keySet();// w  ww.  j a v a2  s  .  c  om
    Iterator itr = st.iterator();

    while (itr.hasNext())
        System.out.println(itr.next());

    // remove 2 from Set
    st.remove("2");

    System.out.println(hMap.containsKey("2"));
}

From source file:Main.java

public static final void main(String[] args) {
    Map<String, String> m = new HashMap<>();
    m.put("a", "alpha");
    m.put("b", "beta");

    Iterator<Map.Entry<String, String>> it = m.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = it.next();
        if (entry.getKey() == "b") {
            entry.setValue("new Value");
        }/*from  w  w  w.j a  v a 2  s. c om*/
    }
    it = m.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = it.next();
        System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
    }
}

From source file:Main.java

public static void main(String[] args) {
    HashMap<String, String> hMap = new HashMap<String, String>();
    hMap.put("1", "One");
    hMap.put("2", "Two");
    hMap.put("3", "Three");

    Collection c = hMap.values();
    Iterator itr = c.iterator();/* w ww.  ja  v  a  2 s . co  m*/
    while (itr.hasNext()) {
        System.out.println(itr.next());
    }
}

From source file:HashMapDemo.java

public static void main(String args[]) {

    HashMap<String, Double> hm = new HashMap<String, Double>();

    hm.put("A", new Double(3.34));
    hm.put("B", new Double(1.22));
    hm.put("C", new Double(1.00));
    hm.put("D", new Double(9.22));
    hm.put("E", new Double(-19.08));

    Set<Map.Entry<String, Double>> set = hm.entrySet();

    for (Map.Entry<String, Double> me : set) {
        System.out.print(me.getKey() + ": ");
        System.out.println(me.getValue());
    }/*from   w  ww. j  a v  a 2s . com*/

    double balance = hm.get("A");
    hm.put("A", balance + 1000);

    System.out.println(hm.get("A"));
}

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Set set = map.entrySet();/*from w  w w .  ja v a 2s. co  m*/

    Iterator iter = set.iterator();

    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();

    }
}

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Set set = map.entrySet();/*from w ww. j  ava2s  .  c o m*/

    Iterator iter = set.iterator();

    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        System.out.println(entry.getKey() + " -- " + entry.getValue());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    HashMap<Integer, Callable<String>> m = new HashMap<Integer, Callable<String>>() {
        {// w w  w.  j a  v a2  s .com
            put(0, () -> {
                return "n";
            });
            put(1, () -> {
                return "m";
            });
        }
    };
    System.out.println(m.get(0).call());
}

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    Collection set = map.values();
    Iterator iter = set.iterator();

    while (iter.hasNext()) {
        System.out.println(iter.next());
    }//www . j  a  v a 2 s. co m
}

From source file:Main.java

public static void main(String[] args) {
    // create map
    Map<String, String> map = new HashMap<String, String>();

    // populate the map
    map.put("1", "A");
    map.put("2", "B");
    map.put("3", "java2s.com");

    // create a synchronized map
    Map<String, String> synmap = Collections.synchronizedMap(map);

    System.out.println("Synchronized map is :" + synmap);
}