gpt4 book ai didi

java - Apache Common MultiValueMap 迭代

转载 作者:行者123 更新时间:2023-12-04 22:05:59 25 4
gpt4 key购买 nike

我有一个 MultiMap 假设 Company 以 String 作为键,另一个 MultiMap 假设 Employee 为值。 Employee Multimap 将 String 作为键,将另一个 multimap 作为值。我的问题是如何检索和迭代存储在 multimap 中的 multimap?我正在使用 Apache 通用 MultiMap。

示例:compMap 有 2 家公司。 CompA 和 CompB每个公司有 10 名员工。(员工可以多次出现,因此使用 multimap )

Coll 包含来自 compA 的员工多图,但是我如何从员工多图中检索特定员工(如果他出现 4 次)?

代码:

if (!compMap.isEmpty()){
Set compSet = compMap.keySet( );
Iterator compIterator = compSet.iterator();
while( compIterator.hasNext( ) ) {
comp= compIterator.next().toString();
Collection coll = (Collection) compMap.get(comp);
.....
}

最佳答案

您遇到的问题是因为一个员工不应该在一个集合中出现两次。如果您将两个值存储在 MultiValueMap 中的同一个键下,那么这些值就是不同的值(尽管它们可以通过同一个键访问)。

您应该将您的数据模型迁移到更面向对象的模型,而不是使用位于另一个 key 存储容器内的 key 存储容器( map 中的 map )。

考虑仅使用集合/列表的以下模型。值根据等于/哈希码进行区分。如果您使用列表,您可以在公司内部拥有许多员工(如果您确实需要,他们可以具有相同的名称)。为什么使用 MultiValueMap 使它复杂化?

public class AbcTest {
public static void main(String[] args) {

Address address1 = new Address("street1");
Address address2 = new Address("street2");

Employee employee1 = new Employee("employee1");
employee1.getAddresses().add(address1);
employee1.getAddresses().add(address2);

Employee employee2 = new Employee("employee2");
employee2.getAddresses().add(address2);

Company companyA = new Company("compA");

companyA.getEmployees().add(employee1);
companyA.getEmployees().add(employee1); // you can add employee to the list as many times as you want, if you really need this?
companyA.getEmployees().add(employee2);

// now to get the employee with give name simly iterate over list of employees

Iterator<Employee> employeeIt = companyA.getEmployees().iterator();


Employee wantedEmployee = null;

while (employeeIt.hasNext() && (wantedEmployee == null)) {
Employee next = employeeIt.next();

if (next.getName().equals("employee1")) {
wantedEmployee = next;
}
}

System.out.println(wantedEmployee);


}



}


class Company {
final String name;

final List<Employee> employees;

Company(String name) {
this.name = name;
this.employees = new ArrayList<>();
}

public String getName() {
return name;
}

public List<Employee> getEmployees() {
return employees;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if ((o == null) || (getClass() != o.getClass())) {
return false;
}

Company company = (Company) o;

if ((employees != null) ? (!employees.equals(company.employees)) : (company.employees != null)) {
return false;
}
if ((name != null) ? (!name.equals(company.name)) : (company.name != null)) {
return false;
}

return true;
}

@Override
public int hashCode() {
int result = (name != null) ? name.hashCode() : 0;
result = (31 * result) + ((employees != null) ? employees.hashCode() : 0);
return result;
}
}

class Employee {
final String name;
final Set<Address> addresses;

Employee(String name) {
this.name = name;
this.addresses = new HashSet<>();
}

public String getName() {
return name;
}

public Set<Address> getAddresses() {
return addresses;
}

@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if ((o == null) || (getClass() != o.getClass())) {
return false;
}

Employee employee = (Employee) o;

if ((addresses != null) ? (!addresses.equals(employee.addresses)) : (employee.addresses != null)) {
return false;
}
if ((name != null) ? (!name.equals(employee.name)) : (employee.name != null)) {
return false;
}

return true;
}

@Override
public int hashCode() {
int result = (name != null) ? name.hashCode() : 0;
result = (31 * result) + ((addresses != null) ? addresses.hashCode() : 0);
return result;
}
}


class Address {
final String street;

Address(String street) {
this.street = street;
}

public String getStreet() {
return street;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if ((o == null) || (getClass() != o.getClass())) {
return false;
}

Address address = (Address) o;

if ((street != null) ? (!street.equals(address.street)) : (address.street != null)) {
return false;
}

return true;
}

@Override
public int hashCode() {
return (street != null) ? street.hashCode() : 0;
}
}

关于java - Apache Common MultiValueMap 迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376692/

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