java2db

Difference between List and Set



Both List and Set are sub interfaces to the Collection interface, in java.util package.The main  Difference between List and Set  are given bellow with an example code.

 

List

Difference between List and Set List is an ordered collection , it allows both duplicate and null elements .

Elements can get by the  index .

Difference between List and Set List is not a synchronized collection , Use Collections.synchronizedList(List list) for synchronized List.

Set

Set is an unordered collection , it won’t allow duplicates, but it allows null values.

Elements cannot get by the index or key .

Set is not a synchronized collection , Use Collections.synchronizedSet(Set set)for synchronized Set.

 

 

Difference between List and Set Difference between List and Set

 

Syntax:

Set setObject = new <Set implemented class>(); List listObject = new <List implemented class>();

Difference between list and set example :

Execution Result:

Performance-wise both List and Set are good .
Set iterates with Iterator , and List iterates with both Iterator , ListIterator.

We can add ,get or remove the List elements  by  index .
In case of Set , we can get higher , subset , tailset elements.

 




Related Posts :
What is the difference between interface and abstract class in java
What is List interface in java
What is Set interface in java
Difference between Set and Map

Comments

  1. saideepa says:

    Very nice example. It’s very useful for beginners.

  2. sai krishnamraju says:

    It’s a very good explanation and example for beginners.Keep it my dear admin. we are expecting more like this from you……

  3. Tester says:

    nice post

   [will not be published]



^ <