List<String> nameNickNameList = new ArrayList<String>();
List<String> combinedList = new ArrayList<String>();
// add all names to nameNickNameList
empList.stream().filter( e -> (nameNickNameList.add(e.getName()) )).collect(Collectors.toList());
// add all nickNames to nameNickNameList
empList.stream().filter( e -> (nameNickNameList.add(e.getNickName()) )).collect(Collectors.toList());
// filter out non null values and assign to combinedList
combinedList = nameNickNameList.stream().filter(s -> (s!=null) && !s.trim().isEmpty()).collect(Collectors.toList());
for(String s : combinedList) {
System.out.println(s);
}
输入:emplist已
Employee e1 = new Employee("James","Jimmy", 20.5);
Employee e2 = new Employee("Robert",null, 25.5);
Employee e3 = new Employee("Kevin","Kev", 12.0);
Employee e4 = new Employee("David","", 16.0);
Employee e5 = new Employee("Alexander","Alex", 5.0);
1条答案
按热度按时间pgccezyw1#
这是获得预期结果的方法之一
输入:emplist已
输出