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");
    Map map2 = new HashMap();
    map2.put("key2", "value2");
    map2.put("key1", "value1");
    map2.put("key3", "value3");
    System.out.println(map1.equals(map2));
}

From source file:Main.java

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

    System.out.println("Size of HashMap : " + hMap.size());

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

    System.out.println("Size of HashMap after addition : " + hMap.size());

    // remove one element from HashMap
    System.out.println(hMap.remove("2"));
}

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");

    System.out.println(map.size());
}

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 value of key 3
    String val = (String) newmap.get(3);

    // check the value
    System.out.println("Value for key 3 is: " + val);
}

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[] a) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    System.out.println(map);//from  w  w  w .j ava2s.  co  m
}

From source file:Main.java

public static void main(String a[]) {
    HashMap hashMap = new HashMap();
    HashMap hashMap1 = new HashMap();

    hashMap.put(1, "One");
    hashMap.put(2, "Two");
    hashMap.put(3, "Three");
    System.out.println("Original HashMap : " + hashMap);

    hashMap1 = (HashMap) hashMap.clone();

    System.out.println("Copied HashMap : " + hashMap1);
}

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);//w ww .  j  a  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();/*from w  w  w.j a  va 2  s.c  o m*/

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

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");
    map.clear();/*from   w  w  w . ja va 2 s. c  om*/

    System.out.println(map.isEmpty());
}