What Is The Difference Between Set And List in Java?


Difference between List and Set

List can contain duplicate elements whereas Set contains unique elements only.

java.util.List

An ordered collection (also known as a sequence). The user can access elements by their integer index (position in the list), and search for elements in the list.

  • Ordered lists of element (unique or not)
  • Conform to Java's interface List<E>
  • Can be accessed by integer index(position in the list)

List implemetented using:

  • java.util.LinkedList
  • java.util.ArrayList

java.util.Set

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

  • Lists of unique elements
  • Conform to Java's interface Set<E>
  • Can not be accessed by index


Set implemetented using:


Both interfaces Set and List extend interface Collection

public interface List<E> extends Collection<E> {}
public interface Set<E> extends Collection<E> {}

張貼留言

0 留言