package com.cy;
import java.util.*;
public class Demo03 {
public static void main(String[] args) {
List<Map<Integer, Person>> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Person person = new Person();
person.setName("a");
person.setAge(i);
person.setSex("a");
Map<Integer, Person> map = new HashMap<>();
map.put(i, person);
list.add(map);
}
Person person = new Person();
person.setName("a");
person.setAge(1);
person.setSex("a");
System.out.println(list.stream().filter(stringPersonMap -> {
if (stringPersonMap.get(1) != null) {
return true;
} else {
return false;
}
}).findAny().orElse(null));
System.out.println(list.stream().filter(stringPersonMap -> {
if (stringPersonMap.get(1) == null) {
return true;
} else {
return false;
}
}).findAny().orElse(null));
System.out.println(list.stream().filter(stringPersonMap -> {
if (false) {
return true;
} else {
return false;
}
}).findAny().orElse(null));
System.out.println(list.stream().filter(stringPersonMap -> {
if (true) {
return true;
} else {
return false;
}
}).findAny().orElse(null));
System.out.println(list.stream().filter(stringPersonMap -> {
if (stringPersonMap.get(1).equals(null)) {
return true;
} else {
return false;
}
}).findAny().orElse(null));
}
}
class Person {
private String name;
private Integer age;
private String sex;
public Person() {
}
public Person(String name, Integer age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
运行结果:
为什么会出现这样的情况?这个里面stringPersonMap到底是不是空值啊?空值的话,那第一个结果是啥意思啊?