在Java 8中使用Streams的Hashmap可以收集Map的值 [英] Hashmap with Streams in Java 8 Streams to collect value of Map

查看:498
本文介绍了在Java 8中使用Streams的Hashmap可以收集Map的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个hashmap

  Map< Integer,List> id1 = new HashMap< Integer,List>(); 

我在hashmap中插入了一些值。

<例如,

 列表< String> list1 = new ArrayList< String>(); 

list1.add(r1);
list1.add(r4);

列表< String> list2 = new ArrayList< String>();
list2.add(r2);
list2.add(r5);

列表< String> list3 = new ArrayList< String>();
list3.add(r3);
list3.add(r6);

id1.put(1,list1);
id1.put(2,list2);
id1.put(3,list3);
id1.put(10,list2);
id1.put(15,list3);

Q1)现在我想对hashmap中的键应用过滤条件并检索相应的值(例如:这里我的查询是key = 1,输出应该是'list1'。

我写了

  id1.entrySet()。stream()。filter(e  - > e.getKey()== 1 ); 

但是我不知道如何检索列表作为这个流操作的输出。 p>

Q2)再一次,我想对hashmap中的键应用一个过滤条件,并检索相应的列表列表。

例如:这里我的查询是key = 1%(即key可以是1,10,15),输出应该是'list1','list2','list3'(列表列表)。

解决方案

如果您确定最多只会获得一个通过过滤器的元素(通过过滤器保证),您可以使用 findFirst

 可选< List> o = id1.entrySet()
.stream()
.filter(e - > e.getKey()== 1)
.map(Map.Entry :: getValue)
.findFirst();

在一般情况下,如果过滤器可能匹配多个列表,您可以将它们收集到List列表:

 列表< List> list = id1.entrySet()
.stream()
.filter(..某些谓词...)
.map(Map.Entry :: getValue)
.collect (Collectors.toList());


Let consider a hashmap

Map<Integer, List> id1 = new HashMap<Integer,List>();

I inserted some values into both hashmap.

For Example,

    List<String> list1 = new ArrayList<String>();

    list1.add("r1");
    list1.add("r4");

    List<String> list2 = new ArrayList<String>();
    list2.add("r2");
    list2.add("r5");

    List<String> list3 = new ArrayList<String>();
    list3.add("r3");
    list3.add("r6");

    id1.put(1,list1);
    id1.put(2,list2);
    id1.put(3,list3);
    id1.put(10,list2);
    id1.put(15,list3);

Q1) Now I want to apply a filter condition on the key in hashmap and retrieve the corresponding value(List).

Eg: Here My query is key=1, and output should be 'list1'

I wrote

id1.entrySet().stream().filter( e -> e.getKey() == 1);

But I don't know how to retrieve as a list as output of this stream operation.

Q2) Again I want to apply a filter condition on the key in hashmap and retrieve the corresponding list of lists.

Eg: Here My query is key=1%(i.e key can be 1,10,15), and output should be 'list1','list2','list3'(list of lists).

解决方案

If you are sure you are going to get at most a single element that passed the filter (which is guaranteed by your filter), you can use findFirst :

Optional<List> o = id1.entrySet()
                      .stream()
                      .filter( e -> e.getKey() == 1)
                      .map(Map.Entry::getValue)
                      .findFirst();

In the general case, if the filter may match multiple Lists, you can collect them to a List of Lists :

List<List> list = id1.entrySet()
                     .stream()
                     .filter(.. some predicate...)
                     .map(Map.Entry::getValue)
                     .collect(Collectors.toList());

这篇关于在Java 8中使用Streams的Hashmap可以收集Map的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆