Hit enter after type your search item

Java list: A Collection interface explained with 7 examples

The list interface in Java

The list is a sub-interface of Collection in Java. This is an ordered Collection that may contain duplicate elements.

The Collection in Java is the group of objects. The objects are known as elements that can be ordered and unordered. The collection has a few sub-interfaces and List is one of those. One other popular sub-interface is the Set. The set does not allow duplicate entries.

Important points about Java List

  • The List inherits the operation from Collection.
  • Additionally, the List interface includes positional access, search, iteration, and range-view operations.
  • The elements in the List can be added, removed and searched.
  • The index of list elements starts at 0.
  • The List interface is implemented in different classes. The popular are ArrayList, LinkedList, Vector etc.
  • Being an interface, the List object cannot be initialized.

See the following section for implementing the List interface in ArrayList, LinkedList and Vector classes. I will also show you a few examples of using the List methods for different operations.

An example of List Java with ArrayList class

This example shows how you may create a List object with ArrayList class. After creating an object, the elements in the List are added by using the add method of List. Finally, a for loop is used for displaying the elements in that List object:

Java list

See online demo and code

The code for creating and displaying the List:


 

An example of using LinkedList class

In this example, the Java LinkedList class is used with List interface for creating an object. Just like above example, the elements are added by using the add method. However, the list is declared as string type. So, it may contain only string elements.

linkedlist

See online demo and code

The code:


 

A demo of using the Vector class

The Vector class is among those classes that implement the List in Java. See this example where a list object is created with Vector class of string type. The same method i.e. add() is used for elements to be added in the list.

A for..each loop is used for displaying the list elements:

list vector in Java

See online demo and code

The code for creating list with Vector class:


 

An example of removing element from the list

The remove() method can be used for deleting the specific element from the list. You may provide the index of element or object in the remove() method. If there are duplicate elements in the list, only first occurrence will be removed. If the element is found in the list, the method returns true otherwise false.

In this example, a list is created by using the ArrayList class. Five elements are added on the list by using the add method. After that, remove method is used for deleting 3rd index element. Have a look:

list remove

See online demo and code

The code for removing the list element by index:


 

A demo of removing by object with duplicate entries

In this example, duplicate elements exist in the list. The remove method is used where an object is created with duplicate entries. See the output and code before and after using the remove method:

list remove object

See online demo and code

The code of remove method by object:


 

You can see in the output graphic, the mango’s second occurrence still exist in the list after using the remove method.

Accessing list elements example

You may use the get method for accessing the specific elements of the list. In the first example of this tutorial, the get method is used in a for loop for displaying the complete list.

In the get() method, you may specify the index number that starts at 0. See this example where I created a list with four elements. The value of elements with index 1 and 3 displayed by using the get() method:

list get

See online demo and code

The code:


 

Iterating through a list by using the iterator

As shown earlier, you may use a for loop for iterating through a list. Alternatively, you may use the list iterator for going through a list as shown in the example below:

list iterator

See online demo and code

The code for iterating through a list with while loop:


You can see, the elements are displayed just like for loop.
This div height required for enabling the sticky sidebar