Example usage for java.util Map put

List of usage examples for java.util Map put

Introduction

In this page you can find the example usage for java.util Map put.

Prototype

V put(K key, V value);

Source Link

Document

Associates the specified value with the specified key in this map (optional operation).

Usage

From source file:Main.java

public static void main(String[] args) {
    Map<String, Student> map = new HashMap<>();
    map.put("s1", new Student(5, "A"));
    map.put("s2", new Student(4, "B"));
    map.put("s3", new Student(10, "C"));
    map.put("s4", new Student(2, "D"));
    Collection<Student> students = map.values();
    List<Student> list = new ArrayList<>(students);
    Collections.sort(list, new MyComparator());

    for (Iterator<Student> it = list.iterator(); it.hasNext();) {
        Student stdn = (Student) it.next();
        System.out.println("Student id : " + stdn.id);
        System.out.println("Student Name : " + stdn.name);
    }/*from  www .  ja va  2  s. c  o m*/
}

From source file:com.ykun.commons.utils.http.HttpClientTest.java

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

    System.out.println(HttpClientUtils.get("http://www.weather.com.cn/data/sk/101010100.html"));

    Map<String, String> params = new HashMap<String, String>();
    params.put("gameID", "1000001");
    params.put("accountID", "helloworld2");
    System.out.println(HttpClientUtils.post("http://115.159.156.171/home/main", params));

    System.out.println(HttpClientUtils.execute(Request.Post("http://115.159.156.171/home/main")
            .bodyForm(Form.form().add("gameID", "1000001").add("accountID", "helloworld2").build())));

}

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, Integer> data = new HashMap<Integer, Integer>();
    data.put(10, 2);
    data.put(20, 3);/* w  ww. ja va 2  s  . co  m*/
    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<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:color.java

public static void main(String[] args) {
    Map<color, letter> map = new EnumMap<color, letter>(color.class);

    map.put(color.red, letter.a);
    map.put(color.green, letter.b);//from   w  w w.  j a  va2s.  c  o  m
    map.put(color.blue, letter.c);

    Map<color, letter> map2 = new EnumMap<color, letter>(color.class);
    letter[] letters = new letter[] { letter.a, letter.b, letter.c };

    for (int i = 0; i < color.values().length; i++) {
        map2.put(color.values()[i], letters[i]);
    }
    System.out.println(map);
    System.out.println(map2);
}

From source file:Main.java

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

    map.put(2, "v");
    map.put(3, "h");
    map.put(4, "e");
    map.put(1, "a");

    System.out.println(map);// ww  w. j a v a2 s. c o m
}

From source file:Main.java

public static void main(String args[]) {
    final Map<String, String> map = new WeakHashMap<String, String>();
    map.put(new String("A"), "B");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("A")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }/*from  www  .j  a  v  a 2 s.  com*/
                System.gc();
            }
        }
    };

    Thread t = new Thread(runner);
    t.start();
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}

From source file:jetbrains.exodus.console.Console.java

public static void main(String[] args) throws IOException, ParseException {
    CommandLine line = getCommandLine(args);

    Long port = (Long) line.getParsedOptionValue("l");
    port = port == null ? 2222 : port;/* w  ww.  ja  v a  2 s  .  c  o  m*/
    String password = line.getOptionValue('p', null);

    Map<String, Object> config = new HashMap<>();
    config.put("location", line.getOptionValue('x', null));

    new RhinoServer(port.intValue(), password, config);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object keyObject = "";
    Object valueObject = "";
    Map<Object, Object> weakMap = new WeakHashMap<Object, Object>();

    weakMap.put(keyObject, valueObject);
    WeakReference weakValue = new WeakReference<Object>(valueObject);

    weakMap.put(keyObject, weakValue);//from w w w .  ja  v  a2  s  . c  om

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();
        weakValue = (WeakReference) weakMap.get(key);
        if (weakValue == null) {
            System.out.println("Value has been garbage-collected");
        } else {
            System.out.println("Get value");
            valueObject = weakValue.get();
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    Map<String, String> unsortMap = new HashMap<String, String>();
    unsortMap.put("1", "1");
    unsortMap.put("2", "A");
    unsortMap.put("3", "2");
    Iterator iterator = unsortMap.entrySet().iterator();
    for (Map.Entry entry : unsortMap.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    }/*from   www.  ja v  a2  s  . c  o  m*/
    Map<String, String> sortedMap = sortByComparator(unsortMap);
    for (Map.Entry entry : sortedMap.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    }
}