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

Friday, 19 June 2015

How to get entry set from TreeMap

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

public class TreeMapEntrySet {
   
    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);
       
        //Returns a Set view of the mappings contained in this
        // map
        Set<Entry<Integer, String>> entry =tm.entrySet();
       
        for (Entry<Integer, String> entry2 : entry) {
            System.out.println("key ="+entry2.getKey()+" value ="+entry2.getValue());
        }
       
    }

}




Output:


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

key =1 value =java

key =2 value =cpp

key =3 value =php

key =4 value =c

key =5 value =mysql

How to get size of TreeMap

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

public class TreeMapKeyCount {
   
    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);

        //Returns the number of key-value mappings in this map
       
        System.out.println("size of treemap = "+tm.size());
       
    }

}

Output:

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

size of treemap = 5








Thursday, 18 June 2015

How to Search Value in TreeMap Java

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

public class TreeMapSearchValue {

    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);
   
        System.out.println(tm.containsValue("java"));
        System.out.println(tm.containsValue("data design"));
        System.out.println(tm.containsValue("html"));
        System.out.println(tm.containsValue("php"));
       
    }
   
   
}

Output:

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

true

false

false

true



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 iterate through TreeMap

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

public class TreeMapIterate {
   
    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 a Set view of the keys contained in this map
       
        Set<Integer> keys = tm.keySet();
       
        for (Integer key : keys) {
            System.out.println(" key ="+key+" value ="+tm.get(key));
        }
       
    }

}

Output:

 key =1 value =java

 key =2 value =cpp

 key =3 value =php

 key =4 value =c

 key =5 value =mysql


Java TreeMap Examples