Tuesday 2 June 2015

How to find does LinkedHashSet 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.LinkedHashSet;
import java.util.Set;

public class ContailsElement {
   
      public static void main(String[] args) {
         
          Set<String> lhs = new LinkedHashSet<String>();
         
          lhs.add("mumbai");
          lhs.add("delhi");
          lhs.add("kolkata");
          lhs.add("chandigarh");
          lhs.add("dehradun");
         
          //print linkedhashset
          System.out.println("lhs is = "+lhs);
         
          System.out.println(lhs.contains("java"));
          System.out.println(lhs.contains("delhi"));
          System.out.println(lhs.contains("kolkata"));
          System.out.println(lhs.contains("php"));
           
        }

}







Output:


lhs is = [mumbai, delhi, kolkata, chandigarh, dehradun]

false

true

true

false

No comments:

Post a Comment