简体   繁体   English

反向清单<Object>

[英]Reverse List<Object>

List listDataClient; 列出listDataClient;

My class DataCLient: 我的类DataCLient:

Client client;
List<String> phoneNumber;

i have a second list 我还有第二个清单

List<DataPhoneNumber> listPhoneNumber;

My class DataPhoneNumber: 我的班级DataPhoneNumber:

String phoneNumber;
List<Client> client;

In my code i put data in my first list but now i want to reverse my list in the second. 在我的代码中,我将数据放在第一个列表中,但是现在我想在第二个列表中反转列表。 In the first list i have a Client wiht x NumberPhone now i want to have NumberPhone for x Client 在第一个列表中,我现在有一个使用x NumberPhone的客户端,我现在想要使用xPhone的NumberPhone。

It sounds like you're trying to model a many-to-many relationship. 听起来您正在尝试建立多对多关系的模型。

One approach would be to create a class to explicitly represent this relationship: PhoneNumberAssociation ; 一种方法是创建一个类来显式表示此关系: PhoneNumberAssociation ; eg 例如

public class PhoneNumberAssociation {
  private final Set<Client> clients;
  private final Set<String> phoneNumbers;

  public void addClient(Client client) {
    this.clients.add(client);
  }

  public void addPhoneNumber(String phoneNumber) {
    this.phoneNumbers.add(phoneNumber);
  }
}

I tend to favour this approach when their is no obvious parent or child role in the relationship. 当他们在关系中没有明显的父母或孩子角色时,我倾向于采用这种方法。 It is also simpler than embedding lists in multiple objects and having to chain calls to add / remove based on whether a phone number is being added to / removed from a client (or conversely if a client is being added to / removed from a phone number). 这比将列表嵌入多个对象中以及根据是否将电话号码添加到客户端或从客户端删除电话(或者相反,如果要将客户端添加到电话号码/从客户端删除电话)中的链式添加/删除电话简单得多。 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM