Java - List (sequence)

Java Conceptuel Diagram


A list (also known as a sequence):

  • is ordered
  • allow duplicates (multiple null elements if they allow them)
  • is zero based. The first element is at the position 0.
  • can contain themselves as elements (but caution as the equals and hashCode methods are no longer well defined on such a list)

A special iterator, called a ListIterator, allows element insertion and replacement, and bidirectional access (from begin or from end) in addition to the normal operations.

Interface and Implementation

List is an interface from the Java Collections Framework representing an ordered collection (also known as a sequence).

See List for all implementations. The most known is the ArrayList: a Resizable-array that permits all elements, including null.



From Array

From an array

List<Class> myList = new ArrayList<Class>();

// With String as Class
List<String> messages = new java.util.ArrayList(Arrays.asList("Nico", "Gerard"));

// The java.util.ArrayList transform the list implementation of the return list from Arrays.asList

See asList

  • As generic function
public static <T> List<T> asList(T... object) {

New Operator

List<Class> myList = new ArrayList<Class>();

The ArrayList(int) constructor gives a List that has size 0. It only ensures that n elements can be added before it needs to reallocate the underlying array.

From scalar



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


String join = String.join("joinSeparator", list);
  • With a stream: Example with columns, to create the first line of a csv
final String headers =


The element are not copied.

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

Deep Copy

The element are also copied.

for(Object obj:list){


  • cast of generic list with parameters. You can't cast a generic type of one parameter to another directly. However, since you can cast to and from wildcard types, you can cast through an intermediate wildcard type.
List<B> variable = (List<B>)(List<?>) collectionOfListA;


  • Ascending order
foreignKeys =
// same as
foreignKeys =
                .sorted(Comparator.comparing(s -> (s.getForeignPrimaryKey().getTableDef().getName())))
  • Descending order
foreignKeys =
// same as
foreignKeys =
                .sorted(Comparator.comparing(s -> (s.getForeignPrimaryKey().getTableDef().getName())).reversed())


  • Stream
List<String> elements  =
  • Collections methods

Stream to

Java - Stream Processing


  • List of object to a list of attribute
List<Person> persons = new ArrayList<>();
// ... add persons
List<String> names =
                .map(a -> a.getName())
// or 
//     .collect(Collectors.toList());
  • List of list to list
List<String> groups =


  • from a list of object
List<Person> persons = new ArrayList<>();
// ... add persons
Map<Integer, CliWord> personsMap =
                .filter(p -> p.getNummer()!=null)
                .collect(Collectors.toMap(Person::getNummer, Function.identity()));
  • extends a list of string with attributes.
List<String> persons = new ArrayList<>();
Map<String, String> personsMap =
                s -> s, 
                s -> lookupFirstName(s)
  • groupBy
Map<String, List<Person>> groupedByName = persons
  • from a list of generic
Map<T, Double> buckets = genericList


List to array

  • General construction
list.toArray(new Class[0])
// before version I don't know anymore
list.toArray(new Class[arraySize])
  • from a list of scalar
// For a list of string
list.toArray(new String[5])
// since opendJdk java 6 better not to pre-size
list.toArray(new String[0])

// Example
List<String> myList = new ArrayList<>();

String[] myArray = myList.toArray(new String[myList.size()])
  • from a list of object Object
// from Stream
final String[] nativeColumns = foreignKeyDef.getNativeColumns().stream()


// Based on
T[] arrayGeneric = (T[]) Array.newInstance(clazz,size)

// With Stream
T[] objects =
        .map(o -> Casts.cast(o, columnDef.getClazz()))
        .toArray((T[]) Array.newInstance(clazz,0));


mySet = new HashSet<>(list);

Documentation / Reference

Discover More
Java Collection Type
Java - (Collection|container) Framework (Types)

A collection (known also as a container) is an object that groups multiple objects (elements) into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. ...
Java Conceptuel Diagram
Java - Array

in Java. Arrays implement most of the List operations, but not remove and add. They are “fixed-size” Lists. An array is a container object that holds : a fixed number of values of a single...
Java Conceptuel Diagram
Java - Set (Collection)

This interface is a member of the Java Collections Framework. Set is a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent...

Share this page:
Follow us:
Task Runner