Sitemap
Suganya Nesappan

Suganya Nesappan

Java: List Interface

5 min readFeb 12, 2024

--

The List interface is found in java.util package and inherits the collection interface. List interface contains the data structures that are used to store ordered data or collection of objects. These data structures are of a list type. These data structures implementing the list interface may or may not have duplicate values.

Press enter or click to view image in full size

List interface contains the methods that are used to access, insert, or remove elements from the list-objects.

Various classes that implement the List interface are: ArrayList, LinkedList, Vector, and Stack. We will discuss each of these classes briefly.

1) ArrayList

ArrayList is the simplest implementation of the List interface. ArrayList preserves the insertion order and it is not synchronized.

Syntax: List <Object> list1= new ArrayList<Object> ();

Example:

Output:

some of the frequently used operations on the List are:

  • Adding elements to List class using add(Object), add(int index, Object) method
  • Updating elements in List class using set() method
  • Searching for elements using indexOf(element), lastIndexOf(element) methods
  • Removing elements using remove(Object),remove(int index) method
  • Accessing Elements in List class using get(int index) method
  • Checking if an element is present in the List class using contains(Object) method
  • find the size of the List size() method is used.

2) Vector

Vector is a class that is implemented in the collection framework and implements a growable array of objects. Vector implements a dynamic array which means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. Vectors fall in legacy classes but now it is fully compatible with collections. Let’s see how to create a list object using this class.

Syntax: List <Object> list1= new Vector<Object>();

Example:

Output:

3) LinkedList

It is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. Let’s see how to create a list object using this class.

Syntax: List<Object> list1= new LinkedList<Object>();

Example:

Output:

4) Stack

Stack is a class that is implemented in the collection framework extends the vector class models and implements the stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions empty, search, and peek.

Get Suganya Nesappan’s stories in your inbox

Join Medium for free to get updates from this writer.

Syntax: List<Object> s = new Stack<Object>();

Example:

Output:

Press enter or click to view image in full size

Frequently Asked Questions

Q #1) What is get method in the list in Java?

Answer: Get method of the list is used to retrieve a particular element in the list based on the index. You pass the required index to the get method and the get method will return the element value at that index.

Q #2) What is the toArray method in Java?

Answer: The method toArray () is used to get the array representation of the list.

Q #3) How do you sort a list in Java?

Answer: You can also use Collections. Sort method to sort the list. This method sorts the list as per natural ordering.

Q #4) What is Arrays.asList() in Java?

Answer: The method ‘asList’ of the array returns the list of elements backed by an array.

To Know More..

Arrays asList() method in Java

The asList() method of java.util.Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

Syntax: List<String> list = Arrays.asList(a);

Example 1:

Output:

Example 2:

Output:

Conclusion:

What is list?

  • The List is an ordered sequence.
  • List allows duplicate elements
  • Elements by their position can be accessed.
  • Multiple null elements can be stored.
  • List implementations are ArrayList, LinkedList, Vector, and Stack.

we have learned some of the methods that a list provides. The Java list provides various methods using which you can manipulate and process lists including searching, sorting, etc. I have explained each method with appropriate programming examples here.

--

--