英文:
Filter the sublist in an object and return java 8
问题
// 需要一个员工对象,其中地址的邮政编码应为40001,从下面的对象中获取。
Employee eObj = new Employee(1, "abc", 10001,
Arrays.asList(new Address("ad1", "ad2", "tel", "40001"),
new Address("ad1", "ad2", "tel1", "40002")));
// 需要帮助编写以下逻辑
Employee filteredEobj = {Logic};
// 期望的响应
new Employee(1, "abc", 10001, Arrays.asList(new Address("ad1", "ad2", "tel", "40001")));
使用Java 8如何实现这一点?
英文:
I need an employee object which should have address having pin 40001 from below object .
Employee eObj= new Employee(1,"abc",10001,
Arrays.asList(new Address("ad1","ad2","tel","40001"),new Address("ad1","ad2","tel1","40002")));
// need help to write below logic
Employee filteredEobj ={Logic}
//Expected response
new Employee(1,"abc",10001,Arrays.asList(new Address("ad1","ad2","tel","40001"));
How can we achieve this using java 8?
答案1
得分: 3
Stream
假设您有相应的getter方法,并且正在请求一个流变体:
Address address = eObj.getAddresses()
.stream()
.map(Employee::getAddress)
.anyMatch(a -> a.getPostCode().equals("40001"))
.orThrow(); // 在未找到时使用的任何逻辑
Employee filteredEObj = new Employee(eObj.getFoo(), eObj.getBar(), eObj.getBaz(), List.of(address)); // 我不太清楚这些参数应该是什么
传统循环
以下是更传统的方法:
Address matchAddress = null;
for (Address address : eObj.getAddresses()) {
if (address.getPostCode().equals("40001")) {
matchAddress = address;
break;
}
}
if (matchAddress == null) {
// TODO 找不到的情况...
}
Employee filteredEObj = new Employee(eObj.getFoo(), eObj.getBar(), eObj.getBaz(), List.of(matchAddress));
仅使用流
最后是一个完全使用流的变体:
Employee filteredEObj = eObj.getAddresses()
.stream()
.map(Employee::getAddress)
.filter(a -> a.getPostCode().equals("40001"))
.limit(1)
.map(a -> new Employee(eObj.getFoo(), eObj.getBar(), eObj.getBaz(), List.of(a)))
.findAny()
.orThrow(); // 在未找到时使用的任何逻辑
多个匹配项
如果您对所有匹配的地址感兴趣,而不仅仅是一个,您可以简单地收集到一个列表中。例如:
List<Address> addresses = eObj.getAddresses()
.stream()
.map(Employee::getAddress)
.filter(a -> a.getPostCode().equals("40001"))
.collect(Collectors.toList());
// 如果没有匹配项,则列表为空
Employee filteredEObj = new Employee(eObj.getFoo(), eObj.getBar(), eObj.getBaz(), addresses);
英文:
Stream
Assuming you have corresponding getters and are asking for a stream variant:
Address address = eObj.getAddresses()
.stream()
.map(Employee::getAddress)
.anyMatch(a -> a.getPostCode().equals("40001"))
.orThrow(); // Whatever logic you want in case not found
Employee filteredEObj = new Employee(eObj.getFoo(), eObj.getBar() , eObj.getBaz(), List.of(address)); // i dont really know what those parameters are supposed to be
Traditional loop
And here the more traditional approach:
Address matchAddress = null;
for (Address address : eObj.getAddresses()) {
if (address.getPostCode().equals("40001")) {
matchAddress = address;
break;
}
}
if (matchAddress == null) {
// TODO case not found ...
}
Employee filteredEObj = new Employee(eObj.getFoo(), eObj.getBar() , eObj.getBaz(), List.of(matchAddress));
Just streams
And finally a variant that uses streams all the way:
Employee filteredEObj = eObj.getAddresses()
.stream()
.map(Employee::getAddress)
.filter(a -> a.getPostCode().equals("40001"))
.limit(1)
.map(a -> new Employee(eObj.getFoo(), eObj.getBar() , eObj.getBaz(), List.of(a));
.findAny()
.orThrow(); // Whatever logic you want in case not found
Multiple matches
In case you are interested in all matching addresses and not just one, you can simple collect into a list. For example:
List<Address> addresses = eObj.getAddresses()
.stream()
.map(Employee::getAddress)
.filter(a -> a.getPostCode().equals("40001"))
.collect(Collectors.toList());
// List is empty in case no matches
Employee filteredEObj = new Employee(eObj.getFoo(), eObj.getBar() , eObj.getBaz(), addresses);
答案2
得分: 1
你可以先筛选地址,然后使用新地址的数据创建员工。
List<Address> filteredAddresses = eObj.getAdresses()
.stream()
.filter(a -> a.getPin().equals("40001"))
.collect(Collectors.toList());
Employee filteredEObj = new Employee(eObj.getId(), eObj.getName(), eObj.getEmpId(), filteredAddresses);
注: 我假设 Employee
类有相应的 getter 方法,因为您尚未展示该类的内容。
英文:
You can filter then addresses then create employee using data of new addresses.
List<Address> filterdAddresses = eObj.getAdresses()
.stream()
.filter(a -> a.getPin().equals("40001"))
.collect(Collectors.toList());
Employee filteredEObj = new Employee(eObj.getId(), eObj.getName() , eObj.getEmpId(), filterdAddresses);
Note: I am assuming the getters for Employee
as you don't show the class yet
答案3
得分: 0
List<Employee> employees = empObjs.stream()
.filter(e -> ("40001").equals(e.getPin()))
.collect(Collectors.toList());
英文:
List<Employee> employees = empObjs.stream()
.filter(e -> ("40001").equals(e.getPin())
.collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论