Write a tree map basic example?

This example shows basic operations on TreeMap like creating an object, adding key-value pair objects, gtting value by passing key object, checking whether the map has elements or not, deleting specific entry, and size of the TreeMap.


								
package MindcluesTreeMap;

import java.util.TreeMap;

/**
 *
 * @author mindclues
 */
public class TreeMapBasic {

    public static void main(String a[]) {
        TreeMap tmap = new TreeMap();
       
        tmap.put("one", "mindclues1");
        tmap.put("two", "mindclues2");
        tmap.put("three", "mindclues3");
        tmap.put("four", "mindclues4");
        tmap.put("five", "mindclues5");
        System.out.println(tmap);
        //getting value for the given key from TreeMap
        System.out.println("Value of first: " + tmap.get("one"));
        System.out.println("Is TreeMap empty? " + tmap.isEmpty());
        tmap.remove("four");
        System.out.println(tmap);
        System.out.println("Size of the TreeMap: " + tmap.size());
    }
}

							

								
{five=mindclues5, four=mindclues4, one=mindclues1, three=mindclues3, two=mindclues2}
Value of first: mindclues1
Is TreeMap empty? false
{five=mindclues5, one=mindclues1, three=mindclues3, two=mindclues2}
Size of the TreeMap: 4
							

Related Articles

post a comment