筛选对象中的子列表并返回java 8。

huangapple go评论67阅读模式
英文:

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 -&gt; a.getPostCode().equals(&quot;40001&quot;))
    .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(&quot;40001&quot;)) {
        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 -&gt; a.getPostCode().equals(&quot;40001&quot;))
    .limit(1)
    .map(a -&gt; 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&lt;Address&gt; addresses = eObj.getAddresses()
    .stream()
    .map(Employee::getAddress)
    .filter(a -&gt; a.getPostCode().equals(&quot;40001&quot;))
    .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&lt;Address&gt; filterdAddresses = eObj.getAdresses()
                            .stream()
                            .filter(a -&gt; a.getPin().equals(&quot;40001&quot;))
                            .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&lt;Employee&gt; employees = empObjs.stream()
                                   .filter(e -&gt; (&quot;40001&quot;).equals(e.getPin())
                                   .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年8月17日 13:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63445137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定