我有一个条件,有一些检查要创建一个对象.
我正在使用Streams,我很难完成这项工作.
输入是具有键/值对的HashMap对象,输出应在下面.
| userrole | userid | username | output |
|------------|--------|----------|----------|
| "" (blank) | 111 | amathews | 111 |
| "" | | amathews | amathews |
| Admin | 111 | amathews | 111 |
| Admin | 111 | "" | 111 |
| Admin | | amathews | Admin |
优先级如下:userid> userrole> username.
每个HashMap对象都将包含userrole / username / userid作为键及其值以及其他键/值对.
在Java的早期版本中,我们将有一堆嵌套的if / else语句来完成此任务.
这是我的代码:
map.entrySet().stream()
.filter(e -> e.getValue() instanceof String || e.getValue() instanceof Integer)
.filter(e -> e.getKey().contains("userrole") || e.getKey().contains("userid") || e.getKey().contains("username") )
.map(e -> e.getValue())
.collect(Collectors.t