gpt4 book ai didi

java - 根据subDTO内的属性值从ArrayList中搜索objectDTO

转载 作者:行者123 更新时间:2023-12-01 10:34:08 24 4
gpt4 key购买 nike

根据另一个 subDTO 的属性值从 arraylist 搜索元素的最佳方法是什么。

Scenario: I have Employee Object, it has another ContactDetails Object. I need to find Employee from list of Employees whose ext extNumber is 1234 (extNumber is attribute inside ContactDetails. I need to check if the ext is available while creating the new employee and allocating the new ext.

can't use the JDBC query directly as I am consuming corba api. Don't have any corba api that provides such search.

DTO 有另一个 subDto。

public class Employee {
private String name;
private ContactDetails contact;
}

public class ContactDetails {
private String number;
}

How I am doing it currently: I am looping over the list and check the extNumber like below

public boolean isTelephoneExtAvailable(String telephoneExt, List<Employee> employeeList) {
boolean isAvailable = true;
if(null != employeeList) {
Iterator<Employee> itr = employeeList.iterator();
while(itr.hasNext()){
employeeList emp = itr.next();
ContactDetails contact = emp .getContactDetails();
if(null != contact && null != contact.getNumber()){
if(contact.getNumber().equals(telephoneExt)){
isAvailable = false;
break;
}
}
}
return isAvailable;
}

有没有更好的优化方法来实现这一点?

最佳答案

如果你可以使用 Java 8,那么我会这样做(来自 apache commons 的 StringUtils):

return !employeeList.stream().anyMatch(employee -> 
employee.getContactDetails()!=null &&
StringUtils.equals(telephoneExt, employee.getContactDetails().getNumber()))

关于java - 根据subDTO内的属性值从ArrayList<objectDTO>中搜索objectDTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34900275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com