share

Saturday, 6 June 2015

How to find does TreeSet contains elements or not

Returns true if this set contains the specified element.More formally, returns true if and only if this set contains an element e such that (o==null ? e==null :
 o.equals(e)).


import java.util.Set;
import java.util.TreeSet;

public class ContailsElement {
   
    public static void main(String[] args) {
       
           Set<String> ts = new TreeSet<String>();
            ts.add("mumbai");
            ts.add("delhi");
            ts.add("kolkata");
            ts.add("chandigarh");
            ts.add("dehradun");

            // print treeset
            System.out.println(" ts ="+ts);
           
            System.out.println(ts.contains("java"));
            System.out.println(ts.contains("delhi"));
            System.out.println(ts.contains("php"));
       
        }

}

Output:

ts =[chandigarh, dehradun, delhi, kolkata, mumbai]

false

true

false


Related Posts: 

No comments:

Post a Comment