Example usage for java.util TreeMap TreeMap

List of usage examples for java.util TreeMap TreeMap

Introduction

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

Prototype

public TreeMap() 

Source Link

Document

Constructs a new, empty tree map, using the natural ordering of its keys.

Usage

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
    String[] letters = { "a", "b", "c" };
    int[] ints = { 3, 2, 1 };
    for (int i = 0; i < letters.length; i++) {
        navigableMap.put(letters[i], ints[i]);
    }//w ww.j a v a  2s  . c  o  m
    System.out.println("Map = " + navigableMap);
    System.out.println("Last entry = " + navigableMap.lastEntry());
}

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
    String[] letters = { "a", "b", "c" };
    int[] ints = { 3, 2, 1 };
    for (int i = 0; i < letters.length; i++) {
        navigableMap.put(letters[i], ints[i]);
    }/*from  w  ww .jav  a 2s  .co m*/
    System.out.println("Map = " + navigableMap);
    System.out.println("Entry < a is " + navigableMap.lowerEntry("a"));
}

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
    String[] letters = { "a", "b", "c" };
    int[] ints = { 3, 2, 1 };
    for (int i = 0; i < letters.length; i++) {
        navigableMap.put(letters[i], ints[i]);
    }/*from  w  w w .j  a v  a2 s.  c o  m*/
    System.out.println("Map = " + navigableMap);
    System.out.println("Poll first entry: " + navigableMap.pollFirstEntry());
}

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
    String[] letters = { "a", "b", "c" };
    int[] ints = { 3, 2, 1 };
    for (int i = 0; i < letters.length; i++) {
        navigableMap.put(letters[i], ints[i]);
    }//  ww w  .j  ava  2  s  . c o m
    System.out.println("Map = " + navigableMap);
    System.out.println("Entry > c is " + navigableMap.higherEntry("c"));
}

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
    String[] letters = { "a", "b", "c" };
    int[] ints = { 3, 2, 1 };
    for (int i = 0; i < letters.length; i++) {
        navigableMap.put(letters[i], ints[i]);
    }/*from ww w . jav a  2s  .  c om*/
    System.out.println("Map = " + navigableMap);
    System.out.println("First entry = " + navigableMap.firstEntry());
}

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<Integer, String> nmap = new TreeMap<Integer, String>();
    nmap.put(1, "this");
    nmap.put(2, "is");
    nmap.put(3, "a");
    nmap.put(4, "test");
    nmap.put(5, ".");

    System.out.println(nmap);/*from   w  w w.j  av  a  2s .  co m*/
    System.out.println(nmap.subMap(4, true, 5, true).values());
    nmap.subMap(1, true, 3, true).clear();
    System.out.println(nmap);
    // wrap into synchronized SortedMap
    SortedMap<Integer, String> ssmap = Collections.synchronizedSortedMap(nmap);

    System.out.println(ssmap.subMap(4, 5));
    System.out.println(ssmap.subMap(4, 5 + 1));
}

From source file:Main.java

public static void main(String[] args) {
    String str = "this is a test";
    char[] char_array = str.toCharArray();

    Map<Character, Integer> charCounter = new TreeMap<Character, Integer>();
    for (char i : char_array) {
        charCounter.put(i, charCounter.get(i) == null ? 1 : charCounter.get(i) + 1);
    }//from  w ww . java 2  s  . c o m
    for (Character key : charCounter.keySet()) {
        System.out.println("occurrence of '" + key + "' is  " + charCounter.get(key));
    }
}

From source file:Main.java

public static void main(String[] s) {
    // create sorted map
    SortedMap<String, String> map = new TreeMap<String, String>();

    // populate the set
    map.put("1", "New");
    map.put("2", "to");
    map.put("3", "java2s.com");

    System.out.println("Initial sorted map value: " + map);

    // create unmodifiable sorted map
    Map<String, String> unmodsortmap = Collections.unmodifiableSortedMap(map);

    // try to modify the sorted map
    unmodsortmap.put("4", "Modify");
}

From source file:TreeMapExample.java

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

    // Add Items to the TreeMap
    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");
    map.put(new Integer(6), "Six");
    map.put(new Integer(7), "Seven");
    map.put(new Integer(8), "Eight");
    map.put(new Integer(9), "Nine");
    map.put(new Integer(10), "Ten");

    // Use iterator to display the keys and associated values
    System.out.println("Map Values Before: ");
    Set keys = map.keySet();/*from  w  w  w . j av  a  2 s . c  om*/
    for (Iterator i = keys.iterator(); i.hasNext();) {
        Integer key = (Integer) i.next();
        String value = (String) map.get(key);
        System.out.println(key + " = " + value);
    }

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

    // Use iterator to display the keys and associated values
    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: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  w ww .j  av  a2  s.  co m
    }

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