What is the difference between a List and a Set in Java?
The List and Set both are the child interfaces of the Collection class . Both are included in the java 1.2 version. We will see the difference between them by example.
Table of Content :
- Introduction
- What is List in Java?
- What is Set in Java?
- Java program to demonstrate use of List
- Java program to demonstrate use of Set
- Difference between List and Set in Java.
- Questions Related to List and Set in Java
- Summary
What is List in Java?
It is a group of individual elements, where insertion order is preserved.
We can access list elements by their position.
Duplicated elements are allowed in the List.
Null element insertion is possible in the List.
The List interface is a member of the Java Collections Framework.
It's added in java from version 1.2.
The Syntax for the Set is:
public interface List<eE>
extends Collection<eE>
Java program for use of List
package com.javacodestuffs.java.program.list;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<Integer> numberList = new LinkedList<Integer>();
numberList.add(null);
numberList.add(402);
numberList.add(51);
numberList.add(151);
numberList.add(222);
numberList.add(789);
numberList.add(789); // we can add duplicate elements
Iterator<Integer> itr = numberList.iterator();
// using while loop
while (itr.hasNext()) {
System.out.println(itr.next());
}
// using for loop
for (Integer number : numberList) {
System.out.println("The number is : " + number);
}
}
}
Output:
null
402
51
151
222
789
789
The number is : null
The number is : 402
The number is : 51
The number is : 151
The number is : 222
The number is : 789
The number is : 789
What is Set in Java?
Set is the child interface of the Collection Interface.
If in a group of objects we don't want duplicate elements and we don't want to preserve the order of the element then we can use the set interface.
The null elements not added in the Set, it will throw NullPointer Exception.
If we try to add an ineligible element in the Set it will throw an unchecked exception, typically NullPointerException or ClassCastException.
The Syntax for the Set is:
public interface Set<eE>
extends Collection<eE>
It is added in Java since Since: 1.2
Java program for use of List
package com.javacodestuffs.java.program.list;
import java.util.Iterator;
import java.util.TreeSet;
public class SetExample {
public static void main(String[] args) {
TreeSet<Integer> numberList = new TreeSet<Integer>();
//numberList.add(null);//we can't add null element in the set
numberList.add(402);
numberList.add(51);
numberList.add(151);
numberList.add(222);
numberList.add(789);
numberList.add(789); // If we add duplicate element add method returns false,
//means that element will not added
Iterator<Integer> itr = numberList.iterator();
// using while loop
while (itr.hasNext()) {
System.out.println(itr.next());
}
//using for loop
for (Integer number : numberList) {
System.out.println("The number is : " + number);
}
}
}
Output:
51
151
222
402
789
The number is : 51
The number is : 151
The number is : 222
The number is : 402
The number is : 789
Difference between List and Set in Java.
List | Set |
---|---|
list contains duplicate elements. | The set contains no duplicate elements. |
The insertion order for the list is preserved. | The insertion order is not preserved in Set |
We can access list elements by position. | We can't access Set elements by position. |
We can add null to the list. | We cant add a null element in Set, it will throw a null pointer exception. |
New methods are defined inside the List interface for specific requirements. | No New methods are defined inside the List interface. |
Implementation classes are LinkedList,ArrayList,Vector. | Implementation classes are HashSet ,LinkedHashSet ,TreeSet. |
Questions Related to What is the difference between List and Set in Java?
Which is better Set or List in Java?
Both are the best in the collection.
It is depending on our requirements, which collection will fulfill our requirements.
If we want to store the group of elements with duplicated, null and insertion orders should be maintained then use List.
If we want to store the group of elements without duplicated elements, without null, and don't want to preserve the insertion order, then use Set.
Iterable interface in javaIn this article we have seen, What is the difference between List and Set in Java? We also seen the examples as well.
0 Comments
Post a Comment