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



No comments:

Post a Comment