查找分配给 ArrayList 中的字符串的相同 objects

Finding same objects assigned to a string in an ArrayList

我不确定我是否用标题正确表达了自己的意思,但我会在这里尝试更具体。

我有: arraylist with clients/customers. arraylistphone numbers 分配给 clients/customers.

我的客户class: 导入 java.util.*;

public class Clients implements Comparable<Clients> {

    private String name;
    private String address;

    public ArrayList<Share> shareList = new ArrayList<Share>();

    private PhoneBook phoneBook = new PhoneBook(); 

    public Clients(String name, String address) {

        this.name = name;
        this.address = address;
    }


    public void addPhoneDescription(String description) {
        phoneBook.addPhoneDescription(description);
    }

    public void addPhoneNumber(String number) {
        phoneBook.addPhoneNumber(number);
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public PhoneBook getPhoneBook() {
        return phoneBook;
    }

    public boolean equals(Object obj) {
        Clients c = (Clients) obj;
        return this.name.equals(c.name);
    }

    public String toString() {
        String result;

        result = ("\n" + "Name: " + this.getName() + "\n" + "Address: "
                + this.getAddress() + "\n" + "Phone description: " + this
                .getPhoneBook());

        return result;
    }

    public int compareTo(Clients other) {
        return name.compareTo(other.name);

    }

}

这是我的 PhoneBook class,由 set/get 方法组成:

import java.util.ArrayList;

public class PhoneBook {

    private ArrayList<String> numbersList = new ArrayList<String>();
    private ArrayList<String> phoneDescription = new ArrayList<String>();

    public void addPhoneDescription(String description) {
        phoneDescription.add(description);
    }

    public void addPhoneNumber(String number) { // Add the phone number to the numbersList

        numbersList.add(number);

    }

    public String toString(){
        return numbersList.toString() + phoneDescription.toString();
    }


}

我想要实现的目标:因此,如果我创建了 3 个客户端,其中 2 个具有相同的编号,我想打印出这 2 个客户端的编号 share/have,依此类推。我已经在我的 Program class 中创建了一个方法:

public void findDuplicatedNumbers() {

        // method that looks for duplicated numbers that clients have

        ArrayList<Integer> sNumber = new ArrayList<>();
        ArrayList<Integer> duplicateNumber = new ArrayList<>();

        for (int i = 0; i < clientList.size(); i++) {
            for (int k = 0; k < (clientList.get(i).getPhoneBook().size); k++) {
                if (sNumber.contains(clientList.get(i).getPhoneBook().get(k).getNumber())) {
                    if (duplicateNumber.contains(clientList.get(i).getPhoneBook().get(k).getNumber())) {

                } else {
                        // adds to duplicateNumber arrayList
                        duplicateNumber.add(clientList.get(i).getPhoneBook().get(k).getNumber());
                    }
                } else {
                    // adds to sNumber arrayList
                    sNumber.add(clientList.get(i).getPhoneBook().get(k).getNumber());
                }
            }

        }
        for (int i = 0; i < duplicateNumber.size(); i++) {
            System.out.println("Phone number: " + duplicateNumber.get(i) + " is share with these clients: ");
            for (int k = 0; k < clientList.size(); k++) {
                for (int p = 0; p < (clientList.get(p).getPhoneBook().size()); p++) {
                    if (duplicateNumber.get(i) == clientList.get(k).getPhoneBook().get(p).getNumber()) {
                        System.out.println(clientList.get(k).getName() + ", ");
                    }
                }
            }
            System.out.println("\n");
        }


    }

Puh .. 为一个简单的任务安静了很多代码。 KISS(保持简单):

public void findDuplicatedNumbers() {
    Map<String, Set<Client> > duplicates = 
       new HashMap<String, Set<Client> >();
    Set<String> phoneNumbers = new HashSet<String>();

    for(Client client : clientList) {
         PhoneBook phoneBook = client.getPhoneBook();
         phoneNumbers.addAll(phoneBook.getNumberList());
    }

    for(String phoneNumber : phoneNumbers) {
       Set<Client> clients = findClientsByPhoneNumber(phoneNumber);

       if(clients.size() > 1)
           duplicates.put(phoneNumber, clients);
    }

    for(Entry<String, Set<Client> entry : duplicates.entrySet()) {

          System.out.println("phonenumber " +  entry.getKey() + " is dubplicated / is share with these clients:");
          for(Client client : entry.getValue()) {
             System.out.println(client.getName());
          }

    }
}
protected Set<Client> findClientsByPhoneNumber(String phoneNumber) {
    Set<Client> clients = new HashSet<Client>();

    for(Client client : clientList ) {
       List<String> phoneNumbers = client.getPhonebook().getNumberList();
       if(phoneNumbers.contains(phoneNumber)) {
          clients.add(client);
       }
    }
    return clients;
}

这不仅结构更清晰,而且实现了一个额外的接口来按号码查找客户,而且在大多数情况下性能更高。 只要清楚你想做什么:如果你想存储一个唯一对象列表,它是 Set 实现之一。如果你想通过一个键存储一些东西,使用 Map - 实现。

public class PhoneBook {

    private ArrayList<String> numbersList = new ArrayList<String>();
    private ArrayList<String> phoneDescription = new ArrayList<String>();

    public void addPhoneDescription(String description) {
        phoneDescription.add(description);
    }

    public void addPhoneNumber(String number) { // Add the phone number to the numbersList

        numbersList.add(number);

    }

    public String toString(){
        return numbersList.toString() + phoneDescription.toString();
    }

    public ArrayList<String> findDuplicatedNumbers() {
        ArrayList<String> dupes = new ArrayList<String>();
        Collections.sort(numbersList);
        Iterator<String> iter = numbersList.iterator();
        while(iter.hasNext()) {
            String next = iter.next();
            if(iter.hasNext() && next == iter.next()) {
                dupes.add(next);
            }
        }

        return dupes;
    }
}