Showing posts with label java treemap. Show all posts
Showing posts with label java treemap. Show all posts

Friday, 19 June 2015

How to delete all elements from TreeMap

import java.util.Map;
import java.util.TreeMap;

public class TreeMapDeleteAll {

   
    public static void main(String[] args) {
       
        Map<Integer, String> tm = new TreeMap<Integer,String>();
       
        // add key and values
        tm.put(1,"java");
        tm.put(2,"cpp");
        tm.put(3,"php");
        tm.put(4,"c");
        tm.put(5, "mysql");
       
        //print treemap
   
        System.out.println("Before Clear TreeMap Is="+tm);
       
        //Removes all of the mappings from this map
        tm.clear();
       
        System.out.println("After Clear TreeMap Is ="+tm);
       
    }
   
}




Output:


Before Clear TreeMap Is={1=java, 2=cpp, 3=php, 4=c, 5=mysql}

After Clear TreeMap Is ={}

How to get value of key in TreeMap

import java.util.Map;
import java.util.TreeMap;

public class TreeMapGetValueOfkey {
   
    public static void main(String[] args) {
       
        Map<Integer, String> tm = new TreeMap<Integer,String>();
       
        // add key and values
        tm.put(1,"java");
        tm.put(2,"cpp");
        tm.put(3,"php");
        tm.put(4,"c");
        tm.put(5, "mysql");
       
    /*
     * Returns the value to which the specified key is
       mapped, or null if this map contains no mapping for
       the key.
     */
      System.out.println("key =1  and value ="+tm.get(1));
      System.out.println("key =1 and value ="+tm.get(5));
      System.out.println("key =1 and value ="+tm.get(10));
      
       
    }

}

Output:

key =1  and value =java

key =1 and value =mysql

key =1 and value =null





Thursday, 18 June 2015

How to storing Java Object In TreeMap

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapStoreObject {

    public static void main(String[] args) {
       
    Map<Integer, Emp> tm = new TreeMap<Integer, Emp>();
   
    tm.put(1, new Emp("yogesh", 100));
    tm.put(2, new Emp("shivam", 200));
    tm.put(3, new Emp("ankush",250));
    tm.put(4, new Emp("anurag",300));
    tm.put(5, new Emp("ravi", 370));
   
    //Returns a Set view of the keys contained in this map.
    Set<Integer> keys = tm.keySet();
   
    for (Integer key : keys) {
        Emp emp = tm.get(key);
        System.out.println(" key ="+ key +" name ="+emp.getName()+" id = "+ emp.getName());
    }
 }
}

class Emp{
    private String name;
    private int id;
   
    public Emp(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
   
}

Output:

 key =1 name =yogesh id = yogesh

 key =2 name =shivam id = shivam

 key =3 name =ankush id = ankush

 key =4 name =anurag id = anurag

 key =5 name =ravi id = ravi


How to create TreeMap In Java

import java.util.Map;
import java.util.TreeMap;

public class CreateTreeMap {

    public static void main(String[] args) {
       
        Map<Integer, String> tm = new TreeMap<Integer,String>();
       
        // add key and values
        tm.put(1,"java");
        tm.put(2,"cpp");
        tm.put(3,"php");
        tm.put(4,"c");
        tm.put(5, "mysql");
       
        //print treemap
   
        System.out.println("TreeMap Is="+tm);
       
    }
   
}





Output:


TreeMap Is={1=java, 2=cpp, 3=php, 4=c, 5=mysql}

Java TreeMap Examples