Java 将列表值流式处理为Map.Entry.values

Java 将列表值流式处理为Map.Entry.values,java,lambda,java-8,java-stream,Java,Lambda,Java 8,Java Stream,我正在寻找一种方法,将流一个列表放入一个映射,在那里我可以指定键 例如,假设我有两个列表: List<String> one = Arrays.asList("1","2","3"); List<String> two = Arrays.asList("100","200","300"); 非常感谢您的帮助。到目前为止,我的想法是: Stream.of( one.stream(), .collect(Collectors.toMap("below 100

我正在寻找一种方法,将
一个
列表
放入一个
映射
,在那里我可以指定键

例如,假设我有两个列表:

List<String> one = Arrays.asList("1","2","3");
List<String> two = Arrays.asList("100","200","300");
非常感谢您的帮助。到目前为止,我的想法是:

Stream.of(
  one.stream(),
      .collect(Collectors.toMap("below 100", ??)
  ,
  two.stream()
      .collect(Collectors.toMap("above 100", ??)
 )
 .reduce(Stream::concat)
编辑:

不确定我是否理解您的问题,但您可以尝试以下方法:

Map map = new HashMap<String, List<String>>();
map.put("Below 100", new ArrayList<String>());
map.put("Above 100", new ArrayList<String>());

List one = Arrays.asList("1","2","3");
List two = Arrays.asList("100","200","300");

one.stream().forEach(s -> map.get("Below 100").add(s));
two.stream().forEach(s -> map.get("Above 100").add(s));
编辑:

不确定我是否理解您的问题,但您可以尝试以下方法:

Map map = new HashMap<String, List<String>>();
map.put("Below 100", new ArrayList<String>());
map.put("Above 100", new ArrayList<String>());

List one = Arrays.asList("1","2","3");
List two = Arrays.asList("100","200","300");

one.stream().forEach(s -> map.get("Below 100").add(s));
two.stream().forEach(s -> map.get("Above 100").add(s));

我发现了两种相似的方法。在这两种方式中,我们分别将每个列表转换为Map>,然后将两个映射连接起来。第一个选项较短,使用内联收集器,而第二个选项创建一个新的收集器类

对于这两个选项,我们首先声明列表:

    List<String> one = Arrays.asList("1", "2", "3");
    List<String> two = Arrays.asList("100", "200", "300");
List one=Arrays.asList(“1”、“2”、“3”);
列表二=数组。asList(“100”、“200”、“300”);
  • 内联收集器的较短选项:

    private BinaryOperator<List<String>> addToList() {
        return (list, str) -> {
            ((ArrayList<String>) list).addAll(str);
            return list;
        };
    }
    
    Map<String, List<String>> map = Stream.of(
            // first list
            one.stream()
                    .collect(Collectors.toMap(
                            l -> "below 100", 
                            // we need List as map value
                            l -> Stream.of(l).collect(Collectors.toList()), 
                            // merge function
                            addToList(), 
                            // provide HashMap for result 
                            HashMap::new 
                    // we can't stream collected Map directly, only through EntrySet
                    )).entrySet(),
            // second list
            two.stream()
                    .collect(Collectors.toMap(
                            l -> "above 100", 
                            l -> Stream.of(l).collect(Collectors.toList()),
                            addToList(), 
                            HashMap::new 
                    )).entrySet()
            )
            // extract Entry<String, List<String>> 
            .flatMap(entrySet -> entrySet.stream())
             // convert Entry<String, List<String>> to Map<String, List<String>
            .collect(Collectors.toMap(
                    entry -> entry.getKey(),
                    entry -> entry.getValue()));
    
    private二进制运算符addToList(){
    返回(列表,str)->{
    ((ArrayList)list.addAll(str);
    退货清单;
    };
    }
    Map=Stream.of(
    //第一名单
    一,流()
    .collect(collector.toMap)(
    l->“低于100”,
    //我们需要列表作为映射值
    l->Stream.of(l).collect(Collectors.toList()),
    //合并函数
    addToList(),
    //为结果提供HashMap
    HashMap::新建
    //我们不能直接流式传输收集的地图,只能通过EntrySet
    )).entrySet(),
    //第二份名单
    二,流()
    .collect(collector.toMap)(
    l->“100以上”,
    l->Stream.of(l).collect(Collectors.toList()),
    addToList(),
    HashMap::新建
    )).entrySet()
    )
    //摘录条目
    .flatMap(entrySet->entrySet.stream())
    //将条目转换为映射条目。getKey(),
    entry->entry.getValue());
    
  • 使用自定义收集器的选项:

  • private BinaryOperator<List<String>> addToList() {
        return (list, str) -> {
            ((ArrayList<String>) list).addAll(str);
            return list;
        };
    }
    
    Map<String, List<String>> map = Stream.of(
            // first list
            one.stream()
                    .collect(Collectors.toMap(
                            l -> "below 100", 
                            // we need List as map value
                            l -> Stream.of(l).collect(Collectors.toList()), 
                            // merge function
                            addToList(), 
                            // provide HashMap for result 
                            HashMap::new 
                    // we can't stream collected Map directly, only through EntrySet
                    )).entrySet(),
            // second list
            two.stream()
                    .collect(Collectors.toMap(
                            l -> "above 100", 
                            l -> Stream.of(l).collect(Collectors.toList()),
                            addToList(), 
                            HashMap::new 
                    )).entrySet()
            )
            // extract Entry<String, List<String>> 
            .flatMap(entrySet -> entrySet.stream())
             // convert Entry<String, List<String>> to Map<String, List<String>
            .collect(Collectors.toMap(
                    entry -> entry.getKey(),
                    entry -> entry.getValue()));
    
    原始流代码较短,只调用ListToMapCollector,而不是实现内联收集器

        Map<String, List<String>> map = Stream
                .of(
                        one.stream()
                                // use custom ListToMapCollector
                                .collect(new ListToMapCollector("below 100"))
                                // we can't stream collected Map directly, only through EntrySet
                                .entrySet(), 
                        two.stream()
                                .collect(new ListToMapCollector("above 100"))
                                .entrySet()) 
                // extract Entry<String, List<String>> 
                .flatMap(entrySet -> entrySet.stream())
                 // convert Entry<String, List<String>> to Map<String, List<String>
                .collect(Collectors.toMap(
                        entry -> entry.getKey(),
                        entry -> entry.getValue()));
    
    Map=Stream
    .的(
    一,流()
    //使用自定义ListOMPCollector
    .collect(新的ListOMapCollector(“低于100”))
    //我们不能直接流式传输收集的地图,只能通过EntrySet
    .entrySet(),
    二,流()
    .collect(新ListToMapCollector(“100以上”))
    .entrySet())
    //摘录条目
    .flatMap(entrySet->entrySet.stream())
    //将条目转换为映射条目。getKey(),
    entry->entry.getValue());
    
    还有我在创建它时使用的ListToMapCollector:

         public class ListToMapCollector implements Collector<String, Map<String, 
         List<String>>, Map<String, List<String>>>
        {
        private String mapKey;
    
        public TestCollector(String string)
        {
            this.mapKey = string;
        }
    
        @Override
        public Supplier<Map<String, List<String>>> supplier() {
            // provide HashMap for result
            return HashMap::new;
        }
    
        @Override
        public BiConsumer<Map<String, List<String>>, String> accumulator() {
            return (map, stringValue) -> {
                if (!map.containsKey(mapKey))
                {
                    map.put(mapKey, new ArrayList<>());
                }
                map.get(mapKey).add(stringValue);
            };
        }
    
        @Override
        public BinaryOperator<Map<String, List<String>>> combiner() {
            // Needed for parrallel stream, excluded for brevity.
            return null;
        }
    
        @Override
        public Function<Map<String, List<String>>, Map<String, List<String>>> finisher() {
            return Function.identity();
        }
    
        @Override
        public Set<java.util.stream.Collector.Characteristics> characteristics() {
            return Collections.singleton(Characteristics.IDENTITY_FINISH);
        }
        }
    
    公共类ListOMapCollector实现收集器
    {
    私有字符串映射密钥;
    公共TestCollector(字符串)
    {
    this.mapKey=string;
    }
    @凌驾
    公共供应商(){
    //为结果提供HashMap
    返回HashMap::new;
    }
    @凌驾
    公共双消费者累加器(){
    返回(映射、字符串值)->{
    如果(!map.containsKey(mapKey))
    {
    put(mapKey,newarraylist());
    }
    get(mapKey).add(stringValue);
    };
    }
    @凌驾
    公共二进制运算符组合器(){
    //平行流需要,为简洁起见排除在外。
    返回null;
    }
    @凌驾
    公共函数完成器(){
    返回函数.identity();
    }
    @凌驾
    公共集特征(){
    返回集合。单例(特征。标识\u完成);
    }
    }
    
    我发现了两种类似的方法。在这两种方式中,我们分别将每个列表转换为Map>,然后将两个映射连接起来。第一个选项较短,使用内联收集器,而第二个选项创建一个新的收集器类

    对于这两个选项,我们首先声明列表:

        List<String> one = Arrays.asList("1", "2", "3");
        List<String> two = Arrays.asList("100", "200", "300");
    
    List one=Arrays.asList(“1”、“2”、“3”);
    列表二=数组。asList(“100”、“200”、“300”);
    
  • 内联收集器的较短选项:

    private BinaryOperator<List<String>> addToList() {
        return (list, str) -> {
            ((ArrayList<String>) list).addAll(str);
            return list;
        };
    }
    
    Map<String, List<String>> map = Stream.of(
            // first list
            one.stream()
                    .collect(Collectors.toMap(
                            l -> "below 100", 
                            // we need List as map value
                            l -> Stream.of(l).collect(Collectors.toList()), 
                            // merge function
                            addToList(), 
                            // provide HashMap for result 
                            HashMap::new 
                    // we can't stream collected Map directly, only through EntrySet
                    )).entrySet(),
            // second list
            two.stream()
                    .collect(Collectors.toMap(
                            l -> "above 100", 
                            l -> Stream.of(l).collect(Collectors.toList()),
                            addToList(), 
                            HashMap::new 
                    )).entrySet()
            )
            // extract Entry<String, List<String>> 
            .flatMap(entrySet -> entrySet.stream())
             // convert Entry<String, List<String>> to Map<String, List<String>
            .collect(Collectors.toMap(
                    entry -> entry.getKey(),
                    entry -> entry.getValue()));
    
    private二进制运算符addToList(){
    返回(列表,str)->{
    ((ArrayList)list.addAll(str);
    退货清单;
    };
    }
    Map=Stream.of(
    //第一名单
    一,流()
    .collect(collector.toMap)(
    l->“低于100”,
    //我们需要列表作为映射值
    l->Stream.of(l).collect(Collectors.toList()),
    //合并函数
    addToList(),
    //为结果提供HashMap
    HashMap::新建
    //我们不能直接流式传输收集的地图,只能通过EntrySet
    )).entrySet(),
    //第二份名单
    二,流()
    .collect(collector.toMap)(
    l->“100以上”,
    l->Stream.of(l).collect(Collectors.toList()),
    addToList(),
    HashMap::新建
    )).entrySet()
    )
    //摘录条目
    .flatMap(entrySet->entrySet.stream())
    //将条目转换为映射条目。getKey(),
    entry->entry.getValue());
    
  • 使用自定义收集器的选项:

  • private BinaryOperator<List<String>> addToList() {
        return (list, str) -> {
            ((ArrayList<String>) list).addAll(str);
            return list;
        };
    }
    
    Map<String, List<String>> map = Stream.of(
            // first list
            one.stream()
                    .collect(Collectors.toMap(
                            l -> "below 100", 
                            // we need List as map value
                            l -> Stream.of(l).collect(Collectors.toList()), 
                            // merge function
                            addToList(), 
                            // provide HashMap for result 
                            HashMap::new 
                    // we can't stream collected Map directly, only through EntrySet
                    )).entrySet(),
            // second list
            two.stream()
                    .collect(Collectors.toMap(
                            l -> "above 100", 
                            l -> Stream.of(l).collect(Collectors.toList()),
                            addToList(), 
                            HashMap::new 
                    )).entrySet()
            )
            // extract Entry<String, List<String>> 
            .flatMap(entrySet -> entrySet.stream())
             // convert Entry<String, List<String>> to Map<String, List<String>
            .collect(Collectors.toMap(
                    entry -> entry.getKey(),
                    entry -> entry.getValue()));
    
    原始流代码较短,只调用ListToMapCollector,而不是实现内联收集器

        Map<String, List<String>> map = Stream
                .of(
                        one.stream()
                                // use custom ListToMapCollector
                                .collect(new ListToMapCollector("below 100"))
                                // we can't stream collected Map directly, only through EntrySet
                                .entrySet(), 
                        two.stream()
                                .collect(new ListToMapCollector("above 100"))
                                .entrySet()) 
                // extract Entry<String, List<String>> 
                .flatMap(entrySet -> entrySet.stream())
                 // convert Entry<String, List<String>> to Map<String, List<String>
                .collect(Collectors.toMap(
                        entry -> entry.getKey(),
                        entry -> entry.getValue()));
    
    Map=Stream
    .的(
    一,流()
    //使用自定义ListOMPCollector
    .collect(新的ListOMapCollector(“低于100”))