Las listas son la colección de secuencia ordenada de Java.
A diferencia de los arrays, un List puede crecer de tamaño para albergar cualquier número de elementos.
Una implementación estándar es ArrayList, que se apoya en un array redimensionable.
Otra implementación estándar es la clase LinkedList, que se apoya en una lista doblemente enlazada.
Los List pueden estar vacíos o contener cualquier número de elementos (incluidos los duplicados).
Los List son una interfaz genérica tipada para indicar qué tipo de objetos pueden contener.
Por ejemplo:
List<String> emptyListOfStrings = List.of();
List<Integer> singleInteger = List.of(1);
List<Boolean> threeBooleans = List.of(true, false, true);
List<Object> listWithMultipleTypes = List.of("hello", 1, true);
Los List tienen varios métodos útiles para añadir, eliminar, obtener y comprobar si un elemento está presente:
List<Character> vowels = new ArrayList<>(List.of('a', 'e', 'i', 'o', 'i', 'e', 'a'));
int startingSize = vowels.size(); // 7
vowels.add('u'); // vowels is now ['a', 'e', 'i', 'o', 'i', 'e', 'a', 'u']
char a = vowels.get(0); // 'a'
boolean hadI = vowels.remove('i'); // true and vowels is now ['a', 'e', 'o', 'i', 'e', 'a', 'u']
boolean hasI = vowels.contains('i'); // true (still have one more left)
La clase Collections también tiene algunas utilidades útiles para trabajar con colecciones como List:
List<Integer> numbers = new ArrayList<>(List.of(1, 5, 3, 2, 4));
Collections.sort(numbers); // [1, 2, 3, 4, 5]
Collections.reverse(numbers); // [5, 4, 3, 2, 1]
Collections.fill(numbers, 42); // [42, 42, 42, 42, 42]
List<Integer> fiveNines = Collections.nCopies(5, 9); // [9, 9, 9, 9, 9]