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

    String[] names = { "A", "J", "B", "E", "P" };
    String[] ids = { "1", "2", "9", "8", "7" };

    Map<String, String> IDMap = new HashMap<String, String>();

    for (int i = 0; i < names.length; i++)
        IDMap.put(ids[i], names[i]);/*from w w  w. java2  s . co m*/

    System.out.println(IDMap.size() + " Students entered: ");
    System.out.println(IDMap);

}

From source file:Main.java

public static void main(String[] args) {
    String query = "name==p==?header=hello?aname=?????lname=lastname";
    String[] params = query.split("\\?");
    Map<String, String> map = new HashMap<String, String>();
    for (String param : params) {
        String name = param.split("=")[0];
        String value = param.substring(name.length(), param.length());
        map.put(name, value);/*from  www  . j av  a  2 s  . co  m*/
        System.out.println(name);
        if (name.equals("")) {
            value += "?";
        }
        System.out.println(value.replaceAll(" ", ""));
    }
}

From source file:Main.java

public static final void main(String[] ignored) {
    Map<Integer, List<String>> mapOfIntStrs = new HashMap<Integer, List<String>>();

    add(mapOfIntStrs, 1, "one");
    add(mapOfIntStrs, 1, "two");
    add(mapOfIntStrs, 1, "three");
    add(mapOfIntStrs, 2, "four");
    add(mapOfIntStrs, 2, "five");
    add(mapOfIntStrs, 3, "six");
    add(mapOfIntStrs, 3, "seven");

    Set<Integer> keySet = mapOfIntStrs.keySet();

    for (int i : keySet) {
        List<String> strList = mapOfIntStrs.get(i);
        System.out.println(i);//from w w w .  j  ava  2 s .co  m
        for (String s : strList) {
            System.out.println("  " + s);
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();
    ObservableMap<String, String> observableMap = FXCollections.observableMap(map);
    observableMap.addListener(new MapChangeListener() {
        @Override//from  www  .j  a  v  a2s. c o m
        public void onChanged(MapChangeListener.Change change) {
            System.out.println("change! ");
        }
    });
    observableMap.put("key 1", "value 1");
    map.put("key 2", "value 2");

}

From source file:Main.java

public static void main(String[] args) {
    String text = "A,B,C,D";

    String[] keyValue = text.split(",");

    Map<Integer, String> myMap = new HashMap<Integer, String>();
    for (int i = 0; i < keyValue.length; i++) {
        myMap.put(i, keyValue[i]);/*ww  w  . j  a  v a 2 s .  co  m*/
    }

    Set keys = myMap.keySet();
    Iterator itr = keys.iterator();

    while (itr.hasNext()) {
        Integer key = (Integer) itr.next();
        String value = (String) myMap.get(key);
        System.out.println(key + " - " + value);
    }
}

From source file:Helper.java

public static void main(String[] args) {

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

    m.put("A", 42);
    m.put("B", 3);
    m.put("C", 1);

    System.out.println(Helper.get(m, "forty-two", -1));
    System.out.println(Helper.get(m, "A", -1));
    System.out.println(Helper.get(m, "B", -1));
    System.out.println(Helper.get(m, "something_else", -1));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Map<String, List<String>> fontFaceNames = new HashMap<String, List<String>>();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = ge.getAllFonts();

    for (int i = 0; i < fonts.length; i++) {
        String familyName = fonts[i].getFamily();
        String faceName = fonts[i].getName();

        List<String> list = fontFaceNames.get(familyName);
        if (list == null) {
            list = new ArrayList<String>();
            fontFaceNames.put(familyName, list);
        }/*from  w w w.j a va2  s . c om*/
        list.add(faceName);
    }
    System.out.println(fontFaceNames);

}

From source file:Main.java

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

    Map map = new HashMap();
    map = Collections.unmodifiableMap(map);

    try {//from w w  w .ja  v  a 2  s.c o m
        map.put("key", "new value");
    } catch (UnsupportedOperationException e) {
        System.out.println(e.getMessage());
    }

}

From source file:MainClass.java

public static void main(String args[]) {

    HashMap<String, Double> hm = new HashMap<String, Double>();

    hm.put("A", new Double(3434.34));
    hm.put("B", new Double(123.22));
    hm.put("C", new Double(1378.00));
    hm.put("D", new Double(99.22));
    hm.put("E", new Double(-19.08));

    Set<Map.Entry<String, Double>> set = hm.entrySet();

    for (Map.Entry<String, Double> me : set) {
        System.out.print(me.getKey() + ": ");
        System.out.println(me.getValue());
    }/*from  www  .  jav a2  s.  c o m*/

    System.out.println();

    double balance = hm.get("B");
    hm.put("B", balance + 1000);

    System.out.println("B's new balance: " + hm.get("B"));
}

From source file:Main.java

public static void main(final String[] args) {
    Map<String, String> items;

    items = new HashMap<String, String>();
    items.put("A", "1");
    items.put("B", "2");
    items.put("C", "3");

    display(new RandomIterator<String>(items.keySet().iterator()));
    display(new RandomIterator<String>(items.keySet().iterator()));
}