Example usage for java.util HashMap put

List of usage examples for java.util HashMap put

Introduction

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

Prototype

public V put(K key, V value) 

Source Link

Document

Associates the specified value with the specified key in this map.

Usage

From source file:Main.java

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

    // populate hash map
    newmap.put(1, "tutorials");
    newmap.put(2, "from");
    newmap.put(3, "java2s.com");

    // check if map is empty
    boolean val = newmap.isEmpty();

    // check the boolean value
    System.out.println("Is hash map empty: " + val);
}

From source file:Main.java

public static void main(String[] args) {
    MyKey e1 = new MyKey(1);
    MyKey e2 = new MyKey(2);

    HashMap<MyKey, Integer> map = new HashMap<>();
    map.put(e1, 1);
    map.put(e2, 2);//from  w  w w  . java 2s .co m

    System.out.println(map.get(e1));
    System.out.println(map);

    e1.setValue(9);
    System.out.println(map.get(e1));
    System.out.println(map);

    e1.setValue(2);
    System.out.println(map.get(e1));
    System.out.println(map);

    MyKey e3 = new MyKey(2);
    System.out.println(map.get(e3));
}

From source file:Main.java

public static void main(String[] args) {
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("d", 5);
    map.put("c", 4);
    map.put("b", 2);
    map.put("a", 1);

    Integer value[] = new Integer[map.size()];

    Set keySet = map.keySet();/*from w w w.ja  va 2s .c  om*/
    Iterator t = keySet.iterator();
    int a = 0;
    while (t.hasNext()) {
        value[a] = map.get(t.next());
        a++;
    }

    Arrays.sort(value);

    for (int i = 0; i < map.size(); i++) {
        t = keySet.iterator();
        while (t.hasNext()) {
            String temp = (String) t.next();
            if (value[i].equals(map.get(temp))) {
                System.out.println(value[i] + " = " + temp);
            }
        }
    }
}

From source file:Main.java

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

    // populate hash map
    newmap.put(1, "tutorials");
    newmap.put(2, "from");
    newmap.put(3, "java2s.com");

    // get keyset value from map
    Set keyset = newmap.keySet();

    // check key set values
    System.out.println("Key set values are: " + keyset);
}

From source file:Main.java

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

    // populate hash map
    newmap.put(1, "tutorials");
    newmap.put(2, "from");
    newmap.put(3, "java2s.com");

    System.out.println("Values before remove: " + newmap);

    // remove value for key 2
    newmap.remove(2);/*from   w  w w  .ja  v  a  2  s.c o  m*/

    System.out.println("Values after remove: " + newmap);
}

From source file:Main.java

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

    // populate hash map
    newmap.put(1, "tutorials");
    newmap.put(2, "from");
    newmap.put(3, "java2s.com");

    System.out.println("Initial map elements: " + newmap);

    // clear hash map
    newmap.clear();//w w  w  .j  av  a  2 s .  c  o m

    System.out.println("Map elements after clear: " + newmap);
}

From source file:Main.java

public static void main(String args[]) {

    HashMap<Integer, String> newmap2 = new HashMap<Integer, String>();
    HashMap<Integer, String> newmap1 = new HashMap<Integer, String>();

    // populate hash map
    newmap1.put(1, "tutorials");
    newmap1.put(2, "from");
    newmap1.put(3, "java2s.com");

    System.out.println("Values in newmap1: " + newmap1);

    // put all values in newmap2
    newmap2.putAll(newmap1);//from www .jav  a 2s . co m

    System.out.println("Values in newmap2: " + newmap2);
}

From source file:Main.java

public static void main(String[] args) {

    HashMap<String, Integer> map = new HashMap<String, Integer>();

    map.put("item1", 1);
    map.put("item2", 2);
    map.put("item3", 1);
    map.put("item4", 7);
    map.put("item5", 3);
    map.put("item6", 4);

    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println("Item is:" + entry.getKey() + " with value:" + entry.getValue());
    }//from   ww  w . j  a  v  a2s .  com

    Map<String, Integer> sortedMap = sortByValue(map);

    for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
        System.out.println("Item is:" + entry.getKey() + " with value:" + entry.getValue());
    }
}

From source file:Main.java

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

    // populate hash map
    newmap.put(1, "tutorials");
    newmap.put(2, "from");
    newmap.put(3, "java2s.com");

    System.out.println("Map value before change: " + newmap);

    // put new values at key 3
    String prevvalue = (String) newmap.put(3, "is great");

    // check returned previous value
    System.out.println("Returned previous value: " + prevvalue);

    System.out.println("Map value after change: " + newmap);
}

From source file:com.sm.replica.TestServer.java

public static void main(String[] args) {
    String[] opts = new String[] { "-store", "-path", "-port", "-mode" };
    String[] defaults = new String[] { "store", "./data", "6900", "0" };
    String[] paras = getOpts(args, opts, defaults);
    String store = paras[0];/*  w  ww .j ava2s  . c o  m*/
    int port = Integer.valueOf(paras[2]);
    String path = paras[1];
    int mode = Integer.valueOf(paras[3]);
    CacheStore cacheStore = new CacheStore(path, null, 0, store, false, mode);
    HashMap<String, CacheStore> storesMap = new HashMap<String, CacheStore>();
    storesMap.put(store, cacheStore);
    logger.info("start server at " + port);
    ReplicaServer server = new ReplicaServer(port, storesMap);

}