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[] args) throws Exception {
    String bubba = "this is a test this is a test";
    Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>();

    for (String currentWord : bubba.split(" ")) {
        Integer current = occurrences.get(currentWord.length());
        if (current == null) {
            current = 0;//  w  w w .  j a v  a 2s  .  c  o  m
        }
        occurrences.put(currentWord.length(), current + 1);
    }
    for (Integer currentKey : occurrences.keySet()) {
        System.out.println("There are " + occurrences.get(currentKey) + " " + currentKey + " letter words");
    }
}

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();/* w w w.j  a va 2s. com*/
    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:HashMapExample.java

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

    map.put(new Integer(1), "One");
    map.put(new Integer(2), "Two");
    map.put(new Integer(3), "Three");
    map.put(new Integer(4), "Four");
    map.put(new Integer(5), "Five");

    System.out.println("Map Values Before: ");
    Set keys = map.keySet();//  ww w.  ja v a  2  s.c  o m
    for (Iterator i = keys.iterator(); i.hasNext();) {
        Integer key = (Integer) i.next();
        String value = (String) map.get(key);
        System.out.println(key + " = " + value);
    }

    System.out.println("\nRemove element with key 6");
    map.remove(new Integer(6));

    System.out.println("\nMap Values After: ");
    keys = map.keySet();
    for (Iterator i = keys.iterator(); i.hasNext();) {
        Integer key = (Integer) i.next();
        String value = (String) map.get(key);
        System.out.println(key + " = " + value);
    }
}

From source file:Counter.java

public static void main(String[] args) {
    Map hm = new HashMap();
    for (int i = 0; i < 10; i++) {
        Integer r = new Integer(20);
        if (hm.containsKey(r))
            ((Counter) hm.get(r)).i++;/*ww  w .j a  va  2  s.  c  o m*/
        else
            hm.put(r, new Counter());
    }
    System.out.println(hm);
}

From source file:Main.java

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

    map.put("a", new Integer(1));
    map.put("b", new Integer(2));
    map.put("c", new Integer(3));

    int size = map.size(); // 2

    Object oldValue = map.put("a", new Integer(9)); // 1

    oldValue = map.remove("c"); // 3

    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();//from ww w  .j  a  v  a  2  s . c  o  m
    }

    it = map.values().iterator();
    while (it.hasNext()) {
        Object value = it.next();
    }
}

From source file:Pizza.java

public static void main(String... args) {
    Map<String, Pizza> pizzaStock = new HashMap<String, Pizza>();

    pizzaStock.put("cheese", new CheesePizza());
    pizzaStock.put("veggie", new VeggiePizza());
    System.out.println(pizzaStock);
}

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);/*from   ww w .j a va 2 s . com*/
    map.put(e2, 2);

    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[]) {

    String s = "this is a test this is a test";
    String[] splitted = s.split(" ");
    Map<String, Integer> hm = new HashMap<String, Integer>();

    for (int i = 0; i < splitted.length; i++) {
        if (hm.containsKey(splitted[i])) {
            int cont = hm.get(splitted[i]);
            hm.put(splitted[i], cont + 1);
        } else {//from  w w w  .  j ava  2s .c o m
            hm.put(splitted[i], 1);
        }
    }
    System.out.println(hm);
}

From source file:MultiKeyExampleV2.java

public static void main(String args[]) {

    codeAndLangToText = new HashMap();
    addMultiKeyAndValue("en", "GM", "Good Morning");
    addMultiKeyAndValue("en", "GE", "Good Evening");
    addMultiKeyAndValue("en", "GN", "Good Night");
    addMultiKeyAndValue("de", "GM", "Guten Morgen");
    addMultiKeyAndValue("de", "GE", "Guten Abend");
    addMultiKeyAndValue("de", "GN", "Guten Nacht");

    System.err.println("Good Evening in English: " + codeAndLangToText.get(new MultiKey("en", "GE")));
    System.err.println("Good Night in German: " + codeAndLangToText.get(new MultiKey("de", "GN")));
}

From source file:Main.java

public static void main(String[] args) {
    List<String> aList = new ArrayList<String>();
    Map<Integer, String> aMap = new HashMap<Integer, String>();
    aList.add("A");
    aList.add("B");

    for (int i = 0; i < aList.size(); i++) {
        aMap.put(i + 1, aList.get(i));/*from   ww  w  .j  a v  a 2 s .  co m*/
    }

    System.out.println(aMap.toString());
}