Example usage for java.util TreeMap put

List of usage examples for java.util TreeMap put

Introduction

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

Prototype

public V put(K key, V value) 

Source Link

Document

Associates the specified value with the specified key in this map.

Usage

From source file:Main.java

public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("3", "Three");
    treeMap.put("2", "Two");
    treeMap.put("5", "Five");
    treeMap.put("4", "Four");

    SortedMap sortedMap = treeMap.headMap("3");
    System.out.println("Head Map Contains : " + sortedMap);
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<Integer, String> db = new TreeMap<Integer, String>();
    db.put(1000, "1000");
    db.put(1011, "1011");
    db.put(1102, "1102");
    db.put(2023, "2023");
    db.put(2034, "2034");

    TreeMap<Integer, String> db2 = new TreeMap<Integer, String>(db);

    System.out.println(db2);/*from w ww  .  j a  v  a2s  .  co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<Integer, String> db = new HashMap<Integer, String>();
    db.put(1000, "1000");
    db.put(1011, "1011");
    db.put(1102, "1102");
    db.put(2023, "2023");
    db.put(2034, "2034");

    TreeMap<Integer, String> db2 = new TreeMap<Integer, String>(db);

    System.out.println(db2);/*w ww  . ja  va 2 s  .  c o m*/
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("3", "Three");
    treeMap.put("2", "Two");
    treeMap.put("5", "Five");
    treeMap.put("4", "Four");

    SortedMap sortedMap = treeMap.tailMap("2");
    System.out.println("Tail Map Contains : " + sortedMap);
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();

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

    SortedMap sortedMap = treeMap.subMap("2", "5");
    System.out.println("SortedMap Contains : " + sortedMap);
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");

    Collection c = treeMap.values();
    Iterator itr = c.iterator();/* w w w.j a  v  a  2  s . c  o m*/

    while (itr.hasNext()) {
        System.out.println(itr.next());
    }
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("3", "Three");
    treeMap.put("2", "Two");

    Set st = treeMap.keySet();//from  w  w w .ja  v a 2  s .com
    Iterator itr = st.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next());
    }
    st.remove("1");

    System.out.println(treeMap.containsKey("1"));
}

From source file:Main.java

public static void main(String[] args) {

    TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();

    treemap.put(2, "two");
    treemap.put(1, "one");
    treemap.put(3, "three");
    treemap.put(6, "six");
    treemap.put(5, "from java2s.com");

    // Putting value at key 3
    System.out.println("Value before modification: " + treemap);
    System.out.println("Value returned: " + treemap.put(3, "java2s.com"));
    System.out.println("Value after modification: " + treemap);
}

From source file:SortedMapDemo.java

public static void main(String[] args) {
    TreeMap sortedMap = new TreeMap();
    sortedMap.put("Adobe", "Mountain View, CA");
    sortedMap.put("IBM", "White Plains, NY");
    sortedMap.put("Learning Tree", "Los Angeles, CA");
    sortedMap.put("Microsoft", "Redmond, WA");
    sortedMap.put("Netscape", "Mountain View, CA");
    sortedMap.put("O'Reilly", "Sebastopol, CA");
    sortedMap.put("Sun", "Mountain View, CA");
    System.out.println(sortedMap);
    Object low = sortedMap.firstKey(), high = sortedMap.lastKey();
    System.out.println(low);/*from  w w  w .  ja  v a 2s.c om*/
    System.out.println(high);
    Iterator it = sortedMap.keySet().iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedMap.subMap(low, high));
    System.out.println(sortedMap.headMap(high));
    System.out.println(sortedMap.tailMap(low));
}

From source file:Main.java

public static void main(String[] a) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    if (!map.isEmpty()) {
        String last = map.lastKey();
        boolean first = true;
        do {/*w ww .j av  a2  s.c  o m*/
            if (!first) {
                System.out.print(", ");
            }
            System.out.print(last);
            last = map.headMap(last, true).lastKey();
            first = false;
        } while (last != map.firstKey());
        System.out.println();
    }
}