Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Tuesday, 11 November 2014

Set in java

Set doesn't add duplicate elements like in list.
There is no get function in set.
Set use iterator for extracting the elements.
The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
We use set in selenium windows handling concept.(Switching from one tab to different tab)Set gives us a unique ids of the windows.

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetDemo {
  
    public static void main(String []str){   
        Set<String> s = new HashSet<String>();
        s.add("test1");
        s.add("test2");
        s.add("test3"); 
        System.out.println("Total size   "+s.size());
        s.add("test1");
        //If i again add test1 and run
        System.out.println("Total size after repeated set test1 "+s.size());
        //Size is same because set doesn't add duplicate elements. 
        //how can we extract that elements which we add above?
        //for retriving elements we use iterator in sets
        Iterator <String>it = s.iterator();
        while(it.hasNext()){
            //This line say that if element is present then it will print
            System.out.println(it.next());
        }     
      
    }

}

0 comments:

Post a Comment

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: