Hi all,
The following is the Selenium Interview question along with its answer. If you are interested in finding out our complete list of Selenium Interview questions, you can refer to http://www.qafox.com/selenium-interview-questions/
Question: What is the difference between List and Set?
Answer: The following are the differences between List and Set:
List:
- Stores the collection of elements in a sequential order and the stored elements from List can be accessed using their index (i.e. First element will have 0 as index, Second element will have index 1 and so on)
- We can store duplicate values in the same List collection as elements
Example program for a List collection:
List<Integer> list = new ArrayList<Integer>();
list.add(9);
list.add(6);
list.add(6);
//Here index 0 is passed to the get() method to print the first element value 9
System.out.println(list.get(0));
//Here index 1 is passed to the get() method to print the second element value 6
System.out.println(list.get(1));
//Here index 2 is passed to the get() method to print the third element value 6
System.out.println(list.get(2));
Set:
- Stores the collection of elements in a random order and the stored elements from the List cannot be accessed using index (i.e. Index cannot be used for retrieving the values from the Set collection)
- Duplicate values cannot be stored in as Set collection.
Example program for a Set collection:
Set<Integer> set = new HashSet<Integer>();
set.add(9);
set.add(6);
set.add(3);
//Using for-each loop to retrieve the elements as the Set don’t have index
for(Integer temp: set){
System.out.println(temp);
}
Output: The elements will be stored in a random order as shown below. Hence when printed, they will be printed in the same random order in which they got stored in the Set internally.
6
3
9
If you are interested in finding out our complete list of Selenium Interview questions, you can refer to http://www.qafox.com/selenium-interview-questions/
Please leave your questions/comments/feedback below.
Happy Learning ?
Naveen Repala (www.QAFox.com)