小编New*_*ser的帖子

Get first instances of a subclass from a list using java 8

I have two subclasses of a class. Now I have a list that can contain either of the subclasses.

Now I need to iterate through the list and get the first occurrence of Subclass B.

I have used the below and looking for Java 8/stream/lambda way of doing this.

SeniorEmployee sEmp = null;    
for(Employee emp: empList){
if(emp instanceof SeniorEmployee)
 sEmp = emp;
 break;
}
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

4
推荐指数
1
解决办法
96
查看次数

比较两个映射的键并返回布尔值

我有两个地图,我需要检查两者是否具有相同的键和相同的键数,并返回一个布尔值。我想为此使用流,我得到的是通过创建另一个列表

mapA
.entrySet()
.stream()
.filter(entry -> mapB.containsKey(entry.getKey()))
.collect(
    Collectors.toMap(Entry::getKey, Entry::getValue));
Run Code Online (Sandbox Code Playgroud)

但是我的问题是,我可以在一行中执行此操作吗?它不会创建另一个列表,但是会返回一个布尔值,无论它们是否相同。

java lambda java-8 java-stream

1
推荐指数
1
解决办法
63
查看次数

标签 统计

java ×2

java-8 ×2

java-stream ×2

lambda ×1