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:TreeMapDemo.java

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

    tm.put("A", new Double(3.34));
    tm.put("B", new Double(1.22));
    tm.put("C", new Double(1.00));
    tm.put("D", new Double(9.22));
    tm.put("E", new Double(-1.08));

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

    for (Map.Entry<String, Double> me : set) {
        System.out.print(me.getKey() + ": ");
        System.out.println(me.getValue());
    }//from  w ww.j a  va2s  .  c o m

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

    System.out.println(tm.get("A"));
}

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();//from w  ww .jav  a 2 s  .c  om

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

From source file:Main.java

public static void main(String args[]) {

    SortedMap<String, String> smap = new TreeMap<String, String>();

    smap.put("1", "tutorial");
    smap.put("2", "from");
    smap.put("3", "java2s.com");

    // get typesafe view of the sorted map
    SortedMap<String, String> tsmap = Collections.checkedSortedMap(smap, String.class, String.class);

    System.out.println(tsmap);//from  w w w.j  ava2 s .c  om
}

From source file:Main.java

public static void main(String[] args) {
    String yourString = "UNDERSTAND";
    Map<Character, Integer> count = new TreeMap<Character, Integer>();
    for (char c : yourString.toCharArray()) {
        if (count.containsKey(c)) {
            count.put(c, (int) count.get(c) + 1);
        } else {/*w  w w.  jav a2s. c  o m*/
            count.put(c, 1);
        }
    }
    System.out.println(count);
}

From source file:MainClass.java

public static void main(String args[]) {
    TreeMap map = new TreeMap();
    map.put("Virginia", "Richmond");
    map.put("Massachusetts", "Boston");
    map.put("New York", "Albany");
    map.put("Maryland", "Annapolis");

    if (!map.isEmpty()) {
        Object last = map.lastKey();
        boolean first = true;
        do {//from   w  w w.  j  a v  a 2 s  .  c  o  m
            if (!first) {
                System.out.print(", ");
            }
            System.out.print(last);
            last = map.headMap(last).lastKey();
            first = false;
        } while (last != map.firstKey());
        System.out.println();
    }
}

From source file:Main.java

public static void main(String[] args) {

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

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

    // using comparator
    Comparator comp = treemap.comparator();

    System.out.println("Following natural ordering");
    System.out.println("Comparator value: " + comp);
}

From source file:NavigableMapDemo.java

public static void main(String[] args) {
    NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
    map.put(2, "two");
    map.put(1, "one");
    map.put(3, "three");
    System.out.println("Original map: " + map + "\n");

    Entry firstEntry = map.pollFirstEntry();
    System.out.println("First entry: " + firstEntry);
    System.out.println("After polling the first entry: " + map + "\n");

    Entry lastEntry = map.pollLastEntry();
    System.out.println("Last entry:" + lastEntry);
    System.out.println("After polling last entry:" + map);
}

From source file:Main.java

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

    // populate the map
    map.put("1", "A");
    map.put("2", "B");
    map.put("3", "from java2s.com");

    // create a sorted map
    SortedMap<String, String> sortedmap = (SortedMap<String, String>) Collections.synchronizedSortedMap(map);

    System.out.println("Synchronized sorted map is :" + sortedmap);
}

From source file:Main.java

public static void main(String[] args) {

    NavigableMap<String, String> nMap = new TreeMap<>();
    nMap.put("CSS", "style");
    nMap.put("HTML", "mark up");
    nMap.put("Oracle", "database");
    nMap.put("XML", "data");
    System.out.println("Navigable Map:" + nMap);

    Entry<String, String> lowerXML = nMap.lowerEntry("XML");
    Entry<String, String> floorXML = nMap.floorEntry("XML");
    Entry<String, String> higherXML = nMap.higherEntry("XML");
    Entry<String, String> ceilingXML = nMap.ceilingEntry("XML");

    System.out.println("Lower:" + lowerXML);
    System.out.println("Floor:" + floorXML);
    System.out.println("Higher:" + higherXML);
    System.out.println("Ceiling:" + ceilingXML);

    // Get the reverse order view of the map
    NavigableMap<String, String> reverseMap = nMap.descendingMap();
    System.out.println("Navigable Map(Reverse  Order):" + reverseMap);

}

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 a v  a  2 s .  c o  m
    System.out.println("Map = " + navigableMap);
    System.out.println("Poll first entry: " + navigableMap.pollLastEntry());
}