List interface

 In Java, the List interface is an ordered collection that allows us to store and access elements sequentially. It extends the Collection interface.

Classes that Implement List:

  • Vector
  • Stack

How to use List?

In Java, we must import java.util.List package in order to use List.

List<String> list1 = new ArrayList<>();

Methods of List:

  • add() - adds an element to a list
  • addAll() - adds all elements of one list to another
  • get() - helps to randomly access elements from lists
  • iterator() - returns iterator object that can be used to sequentially access elements of lists
  • set() - changes elements of lists
  • remove() - removes an element from the list
  • removeAll() - removes all the elements from the list
  • clear() - removes all the elements from the list (more efficient than removeAll())
  • size() - returns the length of lists
  • toArray() - converts a list into an array
  • contains() - returns true if a list contains specified element.

Post a Comment