Java Language
Métodos de la fábrica de recolección
Buscar..
Introducción
La llegada de Java 9 trae muchas características nuevas a la API de colecciones de Java, una de las cuales son los métodos de la fábrica de colecciones. Estos métodos permiten una fácil inicialización de colecciones inmutables , ya sean vacías o no vacías.
Tenga en cuenta que estos métodos de fábrica solo están disponibles para las siguientes interfaces: List<E> , Set<E> y Map<K, V>
Sintaxis
-
static <E> List<E> of() -
static <E> List<E> of(E e1) -
static <E> List<E> of(E e1, E e2) -
static <E> List<E> of(E e1, E e2, ..., E e9, E e10) -
static <E> List<E> of(E... elements) -
static <E> Set<E> of() -
static <E> Set<E> of(E e1) -
static <E> Set<E> of(E e1, E e2) -
static <E> Set<E> of(E e1, E e2, ..., E e9, E e10) -
static <E> Set<E> of(E... elements) -
static <K,V> Map<K,V> of() -
static <K,V> Map<K,V> of(K k1, V v1) -
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2) -
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, ..., K k9, V v9, K k10, V v10) -
static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)
Parámetros
| Método con parámetro | Descripción |
|---|---|
List.of(E e) | Un tipo genérico que puede ser una clase o interfaz. |
Set.of(E e) | Un tipo genérico que puede ser una clase o interfaz. |
Map.of(K k, V v) | Un par clave-valor de tipos genéricos que pueden ser una clase o interfaz. |
Map.of(Map.Entry<? extends K, ? extends V> entry) | Una instancia de Map.Entry donde su clave puede ser K o uno de sus hijos, y su valor puede ser V o cualquiera de sus hijos. |
Lista Ejemplos de métodos de fábrica
-
List<Integer> immutableEmptyList = List.of();- Inicializa una
List<Integer>vacía e inmutableList<Integer>.
- Inicializa una
-
List<Integer> immutableList = List.of(1, 2, 3, 4, 5);- Inicializa una
List<Integer>inmutableList<Integer>con cinco elementos iniciales.
- Inicializa una
-
List<Integer> mutableList = new ArrayList<>(immutableList);- Inicializa una
List<Integer>mutableList<Integer>de unaList<Integer>inmutableList<Integer>.
- Inicializa una
Conjunto Ejemplos de métodos de fábrica
-
Set<Integer> immutableEmptySet = Set.of();- Inicializa un conjunto vacío e inmutable
Set<Integer>.
- Inicializa un conjunto vacío e inmutable
-
Set<Integer> immutableSet = Set.of(1, 2, 3, 4, 5);- Inicializa un
Set<Integer>inmutableSet<Integer>con cinco elementos iniciales.
- Inicializa un
-
Set<Integer> mutableSet = new HashSet<>(immutableSet);- Inicializa un
Set<Integer>mutableSet<Integer>partir de unSet<Integer>inmutableSet<Integer>.
- Inicializa un
Mapa Ejemplos de métodos de fábrica
-
Map<Integer, Integer> immutableEmptyMap = Map.of();- Inicializa un
Map<Integer, Integer>vacío e inmutableMap<Integer, Integer>.
- Inicializa un
-
Map<Integer, Integer> immutableMap = Map.of(1, 2, 3, 4);- Inicializa un
Map<Integer, Integer>inmutableMap<Integer, Integer>con dos entradas iniciales de clave-valor.
- Inicializa un
-
Map<Integer, Integer> immutableMap = Map.ofEntries(Map.entry(1, 2), Map.entry(3, 4));- Inicializa un
Map<Integer, Integer>inmutableMap<Integer, Integer>con dos entradas iniciales de clave-valor.
- Inicializa un
-
Map<Integer, Integer> mutableMap = new HashMap<>(immutableMap);- Inicializa un
Map<Integer, Integer>mutableMap<Integer, Integer>partir de unMap<Integer, Integer>inmutableMap<Integer, Integer>.
- Inicializa un
Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow