Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

396
Visualizações
Java: aplana el mapa anidado usando Stream

Estuve jugando con Java 8 Stream API y encontré algo que solo podía hacer a través de bucles for tradicionales.

Dado un mapa anidado de

 { 1999: { 3: [23, 24, 25], 4: [1, 2, 3] }, 2001: { 11: [12, 13, 14], 12: [25, 26, 27] } }

¿Cómo puedo transformar esto en

 ['23,3,1999', '24,3,1999', '25,3,1999', '1,4,1999', '2,4,1999', '3,4,1999', '12,11,2001', '13,11,2001', '14,11,2001', '25,12,2001', '26,12,2001', '27,12,2001']

Básicamente quiero replicar:

 Map<Integer, Map<Integer, List<Integer>>> dates... List<String> flattened = new ArrayList<>(); for (Integer key1 : map.keySet()) { for (Integer key2 : map.get(key1).keySet()) { for (Integer value : map.get(key1).get(key2)) { flattened.add(value + "," + key2 + "," + key1); } } }
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Prueba esto por ejemplo:

 public static void main(String[] args) { Map<Integer, Map<Integer, List<Integer>>> dates = new HashMap<Integer, Map<Integer, List<Integer>>>() {{ put(1999, new HashMap<Integer, List<Integer>>() {{ put(3, Arrays.asList(23, 24, 25)); put(4, Arrays.asList(1, 2, 3)); }}); put(2001, new HashMap<Integer, List<Integer>>() {{ put(11, Arrays.asList(12, 13, 14)); put(12, Arrays.asList(25, 26, 27)); }}); }}; dates.entrySet().stream().flatMap(year -> year.getValue().entrySet().stream().flatMap(month -> month.getValue().stream() .map(day -> day + "," + month.getKey() + "," + year.getKey()))) .forEach(System.out::println); }

y si los necesita ordenados por year , luego por month y luego por day , intente esto:

 dates.entrySet().stream().flatMap(year -> year.getValue().entrySet().stream().flatMap(month -> month.getValue().stream() .map(day -> Arrays.asList(year.getKey(), month.getKey(), day)))) .sorted(Comparator.comparing(l -> l.get(0)*10000 + l.get(1)*100 + l.get(2))) .forEach(System.out::println);
about 4 years ago · Santiago Trujillo Relatório

0

 items .entrySet() .stream() .flatMap( l-> l.getValue().entrySet().stream() .flatMap( ll->ll.getValue().stream() .map(lll+","+ll.getKey()+","+l.getKey()))) .collect(Collectors.toList()) .forEach(System.out::println);
about 4 years ago · Santiago Trujillo Relatório

0

Suponiendo que hay un Holder que puede tomar 3 enteros, por ejemplo:

 static class Holder { private final int first; private final int second; private final int third; }

Y estos son algunos mapas anidados, entonces 2 flatMap lograrán lo que quieres:

 List<Holder> holders = map.entrySet().stream().flatMap( e -> e.getValue() .entrySet() .stream() .flatMap(x -> x.getValue().stream().map(y -> new Holder(e.getKey(), x.getKey(), y)))) .collect(Collectors.toList()); System.out.println(holders);
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda