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) 我有两个地图,我需要检查两者是否具有相同的键和相同的键数,并返回一个布尔值。我想为此使用流,我得到的是通过创建另一个列表
mapA
.entrySet()
.stream()
.filter(entry -> mapB.containsKey(entry.getKey()))
.collect(
Collectors.toMap(Entry::getKey, Entry::getValue));
Run Code Online (Sandbox Code Playgroud)
但是我的问题是,我可以在一行中执行此操作吗?它不会创建另一个列表,但是会返回一个布尔值,无论它们是否相同。