Java List is an ordered collection. Java List is an interface that extends Collection interface. Java List provides control over the position where you can insert an element. You can access elements by their index and also search elements in the list.

Java List

Some of the important points about Java List are;

  • Java List interface is a member of the Java Collections Framework.
  • List allows you to add duplicate elements.
  • List allows you to have ‘null’ elements.
  • List interface got many default methods in Java 8, for example replaceAll, sort and spliterator.
  • List indexes start from 0, just like arrays.
  • List supports Generics and we should use it whenever possible. Using Generics with List will avoid ClassCastException at runtime.

Java List Class Diagram

Java List interface extends Collection interface. Collection interface externs Iterable interface. Some of the most used List implementation classes are ArrayList, LinkedList, Vector, Stack, CopyOnWriteArrayList. AbstractList provides a skeletal implementation of the List interface to reduce the effort in implementing List.

java-list-class-diagram

Java List Methods

Some of the useful Java List methods are;

  1. int size(): to get the number of elements in the list.
  2. boolean isEmpty(): to check if list is empty or not.
  3. boolean contains(Object o): Returns true if this list contains the specified element.
  4. Iterator<E> iterator(): Returns an iterator over the elements in this list in proper sequence.
  5. Object[] toArray(): Returns an array containing all of the elements in this list in proper sequence
  6. boolean add(E e): Appends the specified element to the end of this list.
  7. boolean remove(Object o): Removes the first occurrence of the specified element from this list.
  8. boolean retainAll(Collection<?> c): Retains only the elements in this list that are contained in the specified collection.
  9. void clear(): Removes all the elements from the list.
  10. E get(int index): Returns the element at the specified position in the list.
  11. E set(int index, E element): Replaces the element at the specified position in the list with the specified element.
  12. ListIterator<E> listIterator(): Returns a list iterator over the elements in the list.
  13. List<E> subList(int fromIndex, int toIndex): Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.

Some of the default methods added to List in Java 8 are;

  1. default void replaceAll(UnaryOperator<E> operator): Replaces each element of this list with the result of applying the operator to that element.
  2. default void sort(Comparator<super E> c): Sorts this list according to the order induced by the specified Comparator.
  3. default Spliterator<E> spliterator(): Creates a Spliterator over the elements in this list.

Java Array to List

We can use Arrays class to get the view of array as list. However we won’t be able to do any structural modification to the list, it will throw java.lang.UnsupportedOperationException. So the best way is to use for loop for creating list by iterating over the array. Below is a simple example showing how to convert java array to list properly.

Choose any of the above methods based on your project requirements.

Java List to Array

A simple example showing the correct way to convert a list to array.

Java List sort

There are two ways to sort a list. We can use Collections class for natural sorting or we can use List sort() method and use our own Comparator for sorting. Below is a simple example for java list sorting.

A sample output is given below. Since I am using Random for generating list elements, it will be different every time.

Java List Common Operations

Most common operations performed on java list are add, remove, set, clear, size etc. Below is a simple java list example showing common method usage.

Output of above java list example program is;

Java List iterator

Below is a simple example showing how to iterate over list in java.

Output of above java list iterator program is;

That’s all of a quick roundup on List in Java. I hope these Java List examples will help you in getting started with List collection programming.

By admin

Leave a Reply

%d bloggers like this: