Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

565
Vistas
How to - grouping a list by key and split the values using streams Java 8

I need to group the ABC List, while doing this I also wanted to split the value. Input:

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

ABC A1 = new ABC();
A1.setName("OO");
A1.setVal("OO111,OO222,OO333"); // split this while grouping
list.add(A1);

ABC A2 = new ABC();
A2.setName("OO");
A2.setVal("OO1");
list.add(A2);

ABC A3 = new ABC();
A3.setName("YY");
A3.setVal("1333,11444");
list.add(A3);

Output:

```java
Map<String, List<String>> postsPerType;
//Group by Name: ex: OO & YY. Split the Value by "," and add it to list
     Key: 00 Values: List: "OO111","OO222","OO333","OO1"
     Key: YY Values: List: "1333","11444"

Please suggest, thanks.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

If you're on Java 9+, you can use Collectors.flatMapping() as groupingBy's downstream:

list.stream().collect(Collectors.groupingBy(ABC::getName,
                    Collectors.flatMapping(a -> Arrays.stream(a.getVal().split(",")), 
                                           Collectors.toList())));

That should produce exactly your desired result.

over 4 years ago · Santiago Trujillo Denunciar

0

I think you can chain a reducing collector to the groupingBy collector to get what you need:

Map<String, List<String>> postsPerType =
    list.stream()
        .collect(Collectors.groupingBy(ABC::getName,
                                       Collectors.reducing(new ArrayList<String>(),
                                                           a -> Arrays.asList(a.getVal().split(",")),
                                                           (l1,l2)-> {
                                                             List<String> l = new ArrayList<>(); 
                                                             l.addAll(l1);
                                                             l.addAll (l2);
                                                             return l;
                                                           })));

This produces the following Map:

{YY=[1333, 11444], OO=[OO111, OO222, OO333, OO1]}
over 4 years ago · Santiago Trujillo Denunciar

0

Another way is using groupingBy and collectingAndThen collectors.

list.stream()
            .collect(Collectors.groupingBy(
                    ABC::getName,
                    Collectors.collectingAndThen(
                            Collectors.mapping(ABC::getVal, toList()),
                            strings -> strings.stream()
                                     .flatMap(s -> Arrays.stream(s.split(",")))
                                     .collect(toList()))
            ));
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda