• <legend id='8mVRR'><style id='8mVRR'><dir id='8mVRR'><q id='8mVRR'></q></dir></style></legend>

      • <bdo id='8mVRR'></bdo><ul id='8mVRR'></ul>

      <small id='8mVRR'></small><noframes id='8mVRR'>

      <tfoot id='8mVRR'></tfoot>
      1. <i id='8mVRR'><tr id='8mVRR'><dt id='8mVRR'><q id='8mVRR'><span id='8mVRR'><b id='8mVRR'><form id='8mVRR'><ins id='8mVRR'></ins><ul id='8mVRR'></ul><sub id='8mVRR'></sub></form><legend id='8mVRR'></legend><bdo id='8mVRR'><pre id='8mVRR'><center id='8mVRR'></center></pre></bdo></b><th id='8mVRR'></th></span></q></dt></tr></i><div id='8mVRR'><tfoot id='8mVRR'></tfoot><dl id='8mVRR'><fieldset id='8mVRR'></fieldset></dl></div>

        Java 8 从一对多分组

        时间:2023-09-26
          <tbody id='JAiE6'></tbody>

          <bdo id='JAiE6'></bdo><ul id='JAiE6'></ul>
              <i id='JAiE6'><tr id='JAiE6'><dt id='JAiE6'><q id='JAiE6'><span id='JAiE6'><b id='JAiE6'><form id='JAiE6'><ins id='JAiE6'></ins><ul id='JAiE6'></ul><sub id='JAiE6'></sub></form><legend id='JAiE6'></legend><bdo id='JAiE6'><pre id='JAiE6'><center id='JAiE6'></center></pre></bdo></b><th id='JAiE6'></th></span></q></dt></tr></i><div id='JAiE6'><tfoot id='JAiE6'></tfoot><dl id='JAiE6'><fieldset id='JAiE6'></fieldset></dl></div>
              <tfoot id='JAiE6'></tfoot>

                  <legend id='JAiE6'><style id='JAiE6'><dir id='JAiE6'><q id='JAiE6'></q></dir></style></legend>
                1. <small id='JAiE6'></small><noframes id='JAiE6'>

                  本文介绍了Java 8 从一对多分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想学习如何将 Java 8 语法与流一起使用,但有点卡住了.

                  I want to learn how to use the Java 8 syntax with streams and got a bit stuck.

                  如果每个值都有一个键,那么 groupingBy 就很容易了.但是,如果我对每个值都有一个键列表并且仍然想用 groupingBy 对它们进行分类怎么办?我是否必须将其分解为多个语句,或者是否可以使用一些流魔术来使其更简单.

                  It's easy enough to groupingBy when you have one key for every value. But what if I have a List of keys for every value and still want to categorise them with groupingBy? Do I have to break it into several statements or is there possibly a little stream magic that can be done to make it simpler.

                  这是基本代码:

                  List<Album> albums = new ArrayList<>();
                  Map<Artist, List<Album>> map = albums.stream().collect(Collectors.groupingBy(this::getArtist));
                  

                  如果每张专辑只有一位艺术家,效果会很好.但我必须返回一个列表,因为一个专辑可以有很多艺术家.专辑和艺术家当然是用来说明的,我有真实世界的类型..

                  It works great if there is only one Artist for every Album. But I must return a List since an Album can have many Artists. Album and Artist are used for illustration of course, I have real-world types..

                  可能有一个简单的解决方案,但我有一段时间没有找到它,所以我呼吁这个网站所代表的集体大脑来解决它.:) 如果不存在简单的解决方案,也欢迎使用复杂的解决方案.

                  There's probably a simple solution but I haven't found it in a while so I'm calling on the collective brain this site represents to solve it. :) A complex solution is also welcome in case a simple one doesn't exist.

                  在 Album 类中或作为以 Album 作为参数的实用方法:

                  In Album class or as an utility method taking an Album as argument:

                  Artist getArtist(); // ok
                  
                  List<Artist> getArtist(); // Not ok, since we now have many "keys" for every Album
                  

                  干杯,迈克尔·格雷夫

                  推荐答案

                  我想你是在 Collectors.mapping 之后,它可以作为第二个参数传递给 groupingBy

                  I think you are after Collectors.mapping which can be passed as a second argument to groupingBy

                  完整示例

                  import java.util.AbstractMap;
                  import java.util.List;
                  import java.util.Map;
                  
                  import static java.util.Arrays.asList;
                  import static java.util.Map.Entry;
                  import static java.util.stream.Collectors.*;
                  
                  public class SO {
                  
                      public static void main(String... args) {
                  
                          List<Album> albums = asList(
                                  new Album(
                                          asList(
                                                  new Artist("bob"),
                                                  new Artist("tom")
                                          )
                                  ),
                                  new Album(asList(new Artist("bill")))
                          );
                  
                          Map<Artist, List<Album>> x = albums.stream()
                                  .flatMap(album -> album.getArtist().stream().map(artist -> pair(artist, album)))
                                  .collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList())));
                  
                          x.entrySet().stream().forEach(System.out::println);
                      }
                  
                      static class Artist {
                          private final String name;
                  
                          Artist(String name) {
                              this.name = name;
                          }
                  
                          public String toString() {return name;}
                  
                      }
                  
                      static class Album {
                          private List<Artist> artist;
                  
                          Album(List<Artist> artist) {
                              this.artist = artist;
                          }
                  
                          List<Artist> getArtist() {
                              return artist;
                          }
                  
                      }
                  
                      private static <T,U> AbstractMap.SimpleEntry<T,U> pair(T t, U u) {
                          return new AbstractMap.SimpleEntry<T,U>(t,u);
                      }
                  
                  
                  }
                  

                  这篇关于Java 8 从一对多分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么将 Mapper 和 Reducer 类声明为静态的? 下一篇:尝试格式化 namenode 时找不到或加载主类;在 MA

                  相关文章

                  最新文章

                2. <i id='3mFvm'><tr id='3mFvm'><dt id='3mFvm'><q id='3mFvm'><span id='3mFvm'><b id='3mFvm'><form id='3mFvm'><ins id='3mFvm'></ins><ul id='3mFvm'></ul><sub id='3mFvm'></sub></form><legend id='3mFvm'></legend><bdo id='3mFvm'><pre id='3mFvm'><center id='3mFvm'></center></pre></bdo></b><th id='3mFvm'></th></span></q></dt></tr></i><div id='3mFvm'><tfoot id='3mFvm'></tfoot><dl id='3mFvm'><fieldset id='3mFvm'></fieldset></dl></div>
                    • <bdo id='3mFvm'></bdo><ul id='3mFvm'></ul>
                  1. <tfoot id='3mFvm'></tfoot>

                    1. <legend id='3mFvm'><style id='3mFvm'><dir id='3mFvm'><q id='3mFvm'></q></dir></style></legend>

                      <small id='3mFvm'></small><noframes id='3mFvm'>