When to use Set and when List in selenium?

I am literally confused always.

I know whenever we work with multiple windows then we use SET for example

Set <String> allwindows = driver.getwindowhandles();

Iterator <String> it = allWindows.iterator();


In case of select or check all checkboxes we use  List <WebElement> for example

List <WebElement> checkbox = driver.findElements(By.xpath(“//input[@type=’checkbox’]”));


When to use SET and LIST can somebody clarify ?


Add Comment
1 Answer(s)

Hey Ankit,

Set and List are two interface to a collection data structure.

Set JavaDoc: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
ist JavaDoc: https://docs.oracle.com/javase/7/docs/api/java/util/List.html

The question that you have asked is very good. .getWindowHandles returns a set. This is because Set’s basic requirement is that it only stores unique elements. Window handles has to be unique. Duplicate elements are not allowed in sets.

.findElements return a list because lists preserves the order. That means that the order in which elements were found on the page are preserved in the List. Hence user gets the list of element in exact order in which the elements were found in HTML dom.

I hope that clears your doubt?


Virender Singh Professor Answered on June 3, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.