Java 8个流列表中的剩余元素,当一个元素出现异常时,如何处理?
How to process remaining elements in the list of Java 8 streams, when exception occurs at one element?
这里主要是获取姓名长度>3的员工的id,但是员工4的姓名为null,所以会抛出空指针异常。如何跳过员工 4
这会引发异常并处理列表中的剩余元素,而不是终止列表。期望的输出可以是 [1, 2, 3, 5, 6, 7, 8]
下面是代码:
public class EmployeeTest {
public static void main(String[] args) {
List<Employee> empList = new ArrayList<>();
createEmpList(empList);
List<Integer> employeeIds = empList.stream()
.filter(x -> x.getName().length() > 3)
.map(x -> x.getId())
.collect(Collectors.toList());
System.out.println(employeeIds);
}
private static void createEmpList(List<Employee> empList) {
Employee e1 = new Employee("siddu", 1, "Hyderabad", 70000);
Employee e2 = new Employee("Swami", 2, "Hyderabad", 50000);
Employee e3 = new Employee("Ramu", 3, "Bangalore", 100000);
Employee e4 = new Employee(null, 4, "Hyderabad", 65000);
Employee e5 = new Employee("Krishna", 5, "Bangalore", 160000);
Employee e6 = new Employee("Naidu", 6, "Poland", 250000);
Employee e7 = new Employee("Arun", 7, "Pune", 45000);
Employee e8 = new Employee("Mahesh", 8, "Chennai", 85000);
empList.add(e1);
empList.add(e2);
empList.add(e3);
empList.add(e4);
empList.add(e5);
empList.add(e6);
empList.add(e7);
empList.add(e8);
}
}
您可以像这样添加过滤器 .filter(x-> x.getName() != null)
:
List<Employee> modifiedEmpList = empList.stream()
.filter(x-> x.getName() != null)
.filter(x -> x.getName().length() > 3)
.collect(Collectors.toList());
以下代码动态处理所有异常。谢谢
List<Employee> empList = new ArrayList<>();
createEmpList(empList);
List<Integer> employeeIds = empList.stream().filter(x -> {
try {
return x.getName().length() > 3;
} catch (Exception e) {
return false;
}
}).map(x -> x.getId()).collect(Collectors.toList());
System.out.println(employeeIds);
输出:[1, 2, 3, 5, 6, 7, 8]
这里主要是获取姓名长度>3的员工的id,但是员工4的姓名为null,所以会抛出空指针异常。如何跳过员工 4 这会引发异常并处理列表中的剩余元素,而不是终止列表。期望的输出可以是 [1, 2, 3, 5, 6, 7, 8]
下面是代码:
public class EmployeeTest {
public static void main(String[] args) {
List<Employee> empList = new ArrayList<>();
createEmpList(empList);
List<Integer> employeeIds = empList.stream()
.filter(x -> x.getName().length() > 3)
.map(x -> x.getId())
.collect(Collectors.toList());
System.out.println(employeeIds);
}
private static void createEmpList(List<Employee> empList) {
Employee e1 = new Employee("siddu", 1, "Hyderabad", 70000);
Employee e2 = new Employee("Swami", 2, "Hyderabad", 50000);
Employee e3 = new Employee("Ramu", 3, "Bangalore", 100000);
Employee e4 = new Employee(null, 4, "Hyderabad", 65000);
Employee e5 = new Employee("Krishna", 5, "Bangalore", 160000);
Employee e6 = new Employee("Naidu", 6, "Poland", 250000);
Employee e7 = new Employee("Arun", 7, "Pune", 45000);
Employee e8 = new Employee("Mahesh", 8, "Chennai", 85000);
empList.add(e1);
empList.add(e2);
empList.add(e3);
empList.add(e4);
empList.add(e5);
empList.add(e6);
empList.add(e7);
empList.add(e8);
}
}
您可以像这样添加过滤器 .filter(x-> x.getName() != null)
:
List<Employee> modifiedEmpList = empList.stream()
.filter(x-> x.getName() != null)
.filter(x -> x.getName().length() > 3)
.collect(Collectors.toList());
以下代码动态处理所有异常。谢谢
List<Employee> empList = new ArrayList<>();
createEmpList(empList);
List<Integer> employeeIds = empList.stream().filter(x -> {
try {
return x.getName().length() > 3;
} catch (Exception e) {
return false;
}
}).map(x -> x.getId()).collect(Collectors.toList());
System.out.println(employeeIds);
输出:[1, 2, 3, 5, 6, 7, 8]