List in Java

Posted by Akhila on January 17th, 2020

List Interface in Java:

In the collection, there is a child interface called as Java.util.List. This is a collection of ordered objects in which copied values are saved. List stores in insertion order, it enables positional access and insertion of elements. List Interface is implemented by the following classes Stack, Vector, LinkedList, and ArrayList.

To get in-depth knowledge on Java, take Java Online Training.

Declaration of List:

public abstract interface List extends Collection.

Creation of List Objects:

The list is an interface and we can create instances of List by using different classes.

List d = new ArrayList();

List c = new LinkedList();

List b = new Vector();

List a = new Stack();

Operations on List:

List interface extends collection, so it helps all the operations of collection interface, with the following extra operations:

1. Positional Access: List enables addition, removing, getting, and setting operations depending on the numerical positions of elements in List. The list has the below methods to perform these operations.

  • void add(int index, Object O) - This helps in adding the given element at a particular index.
  • boolean addAll(int index, Collection c) - This adds all elements from the specified collection to list. First elements get inserted at the provided index. If there is an element at that position, then that element and other subsequent elements are moved to the right side by increasing their index.
  • Object remove(int index) - This deletes an element from the specified index. It moves the consequent element to left and decreases their indexed by 1.
  • Object get(int index) - This returns element at particular index.
  • Object set(int index, Object new) - This replaces element at a given index with a new element. This function returns the element that was replaced by a new element.

Get more on How to learn Java?

2. Search:

List also provides methods for searching elements and returns its numeric position. The below methods are supported by the list for performing this operation.

  • int indexOf(Object o) - It returns the first occurrence of a given element or -1 if the element is not there in the list.
  • int lastIndexOf(Object o) - It returns the final occurrence of given element or -1 if the element is not there in the list.

3. Iteration: 

ListIterator(extends Iterator) is used for iterating over the list element. List iterator is a bidirectional iterator.

4. Range-view:

A method to get the List view of the part of the given list among two indices is given by the List Interface. It supports the operations using the below methods

List subList(int fromIndex, int toIndex) returns List view of given list between fromIndex (included) and toIndex (not included).

I hope you got the overview of List in Java. Follow my articles, to get more updates on Java Programming language.

To learn, take Java Online Course from basics.

Like it? Share it!


Akhila

About the Author

Akhila
Joined: November 27th, 2019
Articles Posted: 2

More by this author