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:Main.java

public static void main(String[] argv) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "one");
    map.put("2", "two");
    map.put("3", "three");
    map.put("4", "four");

    System.out.println(getKeyFromValue(map, "three"));
}

From source file:Main.java

public static void main(String[] args) {
    Map<Character, Integer> counting = new HashMap<Character, Integer>();
    String testcase1 = "this is a test";
    for (char ch : testcase1.toCharArray()) {
        Integer freq = counting.get(ch);
        counting.put(ch, (freq == null) ? 1 : freq + 1);
    }//from   www  .  ja  v a2s .  co  m
    System.out.println(counting.size() + " distinct characters:");
    System.out.println(counting);
}

From source file:Main.java

public static void main(String[] a) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.put(null, null);// w ww  .j a v a 2 s.  c  om

    HashMap<String, String> map2 = new HashMap<String, String>();
    map2.put("key4", "value4");
    map2.put("key5", "value5");
    map2.put("key6", "value6");
    map.putAll(map2);

    System.out.println(map);
}

From source file:Main.java

public static void main(String[] args) {
    HashMap hashMap = new HashMap();
    Map map = Collections.synchronizedMap(hashMap);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Map map = new HashMap();

    // Create int wrapper object
    Integer refInt = new Integer(123);

    // Store int in map
    map.put("key", refInt);

    // Get int value from map
    refInt = (Integer) map.get("key");

    // Get the integer value from wrapper object
    int i = refInt.intValue();
}

From source file:Main.java

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("CSS", "style");
    map.put("HTML", "mark up");
    map.put("Oracle", "database");
    map.put("XML", "data");

    printDetails(map);/*  w ww  . j a  v  a  2s .c  o m*/
    map.clear();
    printDetails(map);
}

From source file:Main.java

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

    for (int i = 0; i < 10; i++) {
        map.putIfAbsent(i, "val" + i);
    }//from   w  w  w  .  j a v a  2  s . com

    map.forEach((id, val) -> System.out.println(val));

    map.remove(3, "val3");
    System.out.println(map.get(3)); // val33

    map.remove(3, "val33");
    System.out.println(map.get(3)); // null
}

From source file:Main.java

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

    for (int i = 0; i < 10; i++) {
        map.putIfAbsent(i, "val" + i);
    }//from w  w w. j a  va 2  s  .  c  o  m

    map.forEach((id, val) -> System.out.println(val));

    map.merge(9, "val9", (value, newValue) -> value.concat(newValue));
    System.out.println(map.get(9)); // val9

    map.merge(9, "concat", (value, newValue) -> value.concat(newValue));
    System.out.println(map.get(9)); // val9concat
}

From source file:Main.java

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

    newmap.put(1, "tutorials");
    newmap.put(2, "from");
    newmap.put(3, "java2s.com");

    // create set view for the map
    Set set = newmap.entrySet();// www  . j a va  2  s . co m

    // check set values
    System.out.println("Set values: " + set);
}

From source file:Main.java

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

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

    map.put(1, "first");
    map.put(2, "second");
    // map.put(1, 2); <- Syntax error
}