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[] 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.get("key2"));
    System.out.println(map.containsKey("key2"));
    System.out.println(map.containsKey("key4"));
}

From source file:Main.java

public static final void main(String[] args) {
    Map<String, String> m = new HashMap<>();
    m.put("a", "alpha");
    m.put("b", "beta");

    Iterator<Map.Entry<String, String>> it = m.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = it.next();
        if (entry.getKey() == "b") {
            entry.setValue("new Value");
        }//from w w  w . j  av a2s.co m
    }
    it = m.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = it.next();
        System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
    }
}

From source file:Main.java

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

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

    weakMap.put(keyObject, weakValue);/*from ww  w. j a v  a2 s. c  o  m*/

    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> oldMap = new HashMap<String, String>();
    oldMap.put("a", "1");
    oldMap.put("A", "2");

    Map<String, String> newMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    newMap.putAll(oldMap);/*from w ww  .j av  a 2 s  .c o  m*/
    String value = newMap.get("a");
    System.out.println(value);
}

From source file:Weak.java

public static void main(String args[]) {
    final Map map = new WeakHashMap();
    map.put(new String("Java2s"), "www.java2s.com");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("Java2s")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }/*from  w  ww  .  j av a 2  s . c  om*/
                System.out.println("Waiting");
                System.gc();
            }
        }
    };
    Thread t = new Thread(runner);
    t.start();
    System.out.println("Main waiting");
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> yourMap = new HashMap<String, String>();
    yourMap.put("1", "one");
    yourMap.put("2", "two");
    yourMap.put("3", "three");

    Map<String, String> sortedMap = new TreeMap<String, String>(yourMap);

    System.out.println(sortedMap);
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Map<String, String> attributes = new HashMap<>();
    attributes.put("create", "true");
    URI zipFile = URI.create("jar:file:/home.zip");
    try (FileSystem zipFileSys = FileSystems.newFileSystem(zipFile, attributes);) {
        Path path = zipFileSys.getPath("docs");
        Files.createDirectory(path);
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(zipFileSys.getPath("/"));) {
            for (Path file : directoryStream) {
                System.out.println(file.getFileName());
            }/*  ww  w  . ja  v a 2  s . c o m*/
        }
    }
}

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

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

    while (iter.hasNext()) {
        System.out.println(iter.next());
    }// w ww  .  j  a va 2 s  .co m
}

From source file:Main.java

public static void main(String[] argv) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "one");
    map.put("2", "two");
    map.put("3", "three");
    map.put("4", "four");

    System.out.println(getKeyFromValue(map, "three"));
}

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");
    Set set = map.entrySet();//from  ww  w.  java 2s  .c  o m

    Iterator iter = set.iterator();

    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();

    }
}