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, URL> urlMap = new HashMap<String, URL>();
    try {/*from   ww  w. ja va 2  s .  com*/
        urlMap.put("java", new URL("http://www.java2s.com"));
    } catch (MalformedURLException e) {
    }
    String s = urlMap.get("java").getHost();
}

From source file:Main.java

public static void main(String[] args) {
    HashMap<String, String> hm = new HashMap<String, String>();
    hm.put("3", "three");
    hm.put("1", "one");
    hm.put("4", "four");
    hm.put("2", "two");
    printMap(hm);//  www  .  ja  v a 2s  .c  om
}

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

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

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

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

    Hashtable<String, String> ht = new Hashtable<String, String>();
    ht.put("1", "REPLACED !!");
    ht.put("4", "Four");

    Enumeration e = ht.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }//  w  ww.  j  ava  2 s .  co  m

    ht.putAll(hMap);
    e = ht.elements();

    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:Main.java

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

    grades.put(1, "A");
    grades.put(2, "B");
    grades.put(3, "C");
    grades.put(4, "D");
    grades.put(5, "E");

    String value = grades.get(1);

    List<String> dayNames = new ArrayList<String>();
    dayNames.add("Sunday");
    dayNames.add("Monday");
    dayNames.add("Tuesday");
    dayNames.add("Wednesday");

    String firstDay = dayNames.get(0);
}

From source file:Tutorial.java

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

    map.put(Tutorial.CSS, "1");
    map.put(Tutorial.Python, "2");
    map.put(Tutorial.PHP, "3");
    map.put(Tutorial.Java, "4");

    System.out.println(map.hashCode());

}

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, Integer> data = new HashMap<Integer, Integer>();
    data.put(10, 2);// w  w w.j a va2 s  .com
    data.put(20, 3);
    data.put(30, 5);
    data.put(40, 15);
    data.put(50, 4);

    SortedSet<Integer> keys = new TreeSet<Integer>(data.keySet());
    String s = "";
    for (Integer key : keys) {
        s += key + " : ";
        for (int i = 0; i < data.get(key); i++) {
            s += "*";
        }
        s += "\n";
    }
    System.out.println(s);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee, String> map2 = new HashMap<Employee, String>();
    Employee e2 = new Employee("J", 26);
    map2.put(e2, "MGMT");
    System.out.println(map2);/* w ww  .ja  v  a 2s  . c  om*/
    e2.setAge(27);
    System.out.println(map2);
    System.out.println(map2.containsKey(e2));//false

    IdentityHashMap<Employee, String> map1 = new IdentityHashMap<Employee, String>(map2);

    System.out.println(map1);
}

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

    System.out.println("Map: " + map.toString());

    listValues(map);//from  w w w.  j a  v  a2  s  .c  om
    listEntries(map);
}

From source file:Main.java

public static void main(String[] args) {

    HashMap<String, Double> map = new HashMap<String, Double>();
    ValueComparator bvc = new ValueComparator(map);
    TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);

    map.put("A", 9.0);
    map.put("B", 6.0);
    map.put("C", 7.0);
    map.put("D", 10.0);

    System.out.println("unsorted map: " + map);

    sorted_map.putAll(map);//from   www  .  j  a v a2 s. c  o  m

    System.out.println("results: " + sorted_map);
}