0

So I have this class which reads from 2 files and fills 2 Arraylists with Contact Objects. Now I want to Merge these Arraylists to a new Arraylist which then I want to Sort and eliminate duplicates. My problem is: How do I get the filled Arraylists to another method so I can do the sorting? Here is my Code:

 import java.util.List;
 import java.util.Scanner;
 
 final class Addressbook{
 
     public List<Contact> contacts1 = new ArrayList<Contact>();
 
     public List<Contact> contacts2= new ArrayList<Contact>();
 
     public List<Contact> allcontacts = new ArrayList<Contact>();
 
     public void readContacts1(Scanner scanner1) {
 
         scanner1.useDelimiter(";");
 
         while (scanner1.hasNext()) {
             final Contact contact= readContacts1(scanner1);
             contacts1.add(Contact);
 
         }
         
 
 
     }
 
     public void readContacts2(Scanner scanner2) {
 
         while (scanner2.hasNext()) {
             final Contact contact = readContacts2(scanner2);
             contacts.add(contact);
 
         }
     
         
 }
     
     public int ContactSearch1(Contact c) {
 
         for (int i = 0; i < contacts1.size(); i++)
 
             if (contacts1.get(i).equals(c))
                 return i;
         return -1;
 
     }
 
     public int ContactSearch2(Contact c) {
 
         for (int i = 0; i < contacts2.size(); i++)
 
             if (contacts2.get(i).equals(c))
                 return i;
         return -1;
 
     }
 
     private static Contact readContact1(Scanner scanner1) {
         scanner1.useDelimiter(";");
 
         final String name= scanner1.next();
         final String lastname = scanner1.next();
         final String address = scanner1.next();
         final String number = scanner1.next();
 
         final Contact contact= new Contact(name, lastname, address, number);
         return contact;
 
     }
 
     private static Contact ReadContact2(Scanner scanner2) {
         scanner2.useDelimiter(";");
 
         final String name= scanner2.next();
 
         final String lastname = scanner2.next();
 
         final String address = scanner2.next();
 
         final String number = scanner2.next();
 
         final Contact contact= new Contact(name, lastname, address, number);
         return contact;
 
     }
 

 
 }
PK2121
  • 3
  • 2
  • Have you tried to compile your code? It seems there are infinite loops in `readContacts1`/`readContacts2` -- these methods call themselves recursively instead of implied `readContact1`. And why do you need so much duplicated code? – Nowhere Man Jan 28 '21 at 14:19
  • I need to read in 2 files convert them to "Contact Objects" and do operations with these. I also need a complete list of these.Therefor I want to merge contacts1 with contacts2 but I don`t get how I can get these lists out of the method. – PK2121 Jan 28 '21 at 14:46

3 Answers3

0
contacts1.addAll(contacts2);
contacts1.sort((o1, o2) -> o1 < o2 ? o1 : o2);

contacts1.addAll(contacts2)

This adds all contacts2 to contacts1 and contacts1 List will have all the contacts.

Here u can replace o1 < o2 can be replaced with the logic u need to sort.

0

The easiest way to ensure, that you do not have duplicates in your ArrayList is to copy your list into a Collection that does not allow duplicates (for example a set) and then copy it back into the ArrayList you need. I would recommend to check out this post for this:

Answers to Question: I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

Ultimately your problem could be answered the following way (using the code of mentioned blog post):

allcontacts.addAll(contacts1);
allcontacts.addAll(contacts2);

Set<Contact> set = new HashSet<>(allcontacts);
allcontacts.clear();
allcontacts.addAll(set);

Now you got rid of all duplicates and merged the ArrayLists contacts1 and contacts2 into allcontacts. You now only have to sort allcontacts, which can be done like this (if Contact implements Comparable):

Collections.sort(allcontacts);
M.Tichy
  • 11
  • 4
  • 1
    A `TreeSet` would sort the contents directly at insert time – wiomoc Jan 28 '21 at 14:28
  • You can not apply `Collections.sort(..)` on a `Set` – wiomoc Jan 28 '21 at 14:29
  • allcontacts is defined by the user PK2121 as an ArrayList. I just copied the ArrayList into a set and the copied it back into an ArrayList and then apply Collections.sort(..). Sorry if I expressed it in a confusing way. – M.Tichy Jan 28 '21 at 14:35
0

First, the provided code contains a lot of duplicated parts which may easily be encapsulated in a separate helper class which takes care of reading and searching the lists.

final class Addressbook{

    private List<Contact> contacts1;
    private List<Contact> contacts2;
 
    private List<Contact> allContacts;

    private static class ContactHelper {
        public static List<Contact> readContacts(Scanner scanner) {
            // scanner.setDelimiter(";"); // it's better to set this parameter in the call
            List<Contact> result = new ArrayList<>();
            boolean hasData = scanner.hasNext();
            while (hasData) {
                final String name= scanner.next();
                final String lastname = (hasData &= scanner.hasNext()) ? scanner.next() : "NO_LAST_NAME";
                final String address = (hasData &= scanner.hasNext()) ? scanner.next() : "NO_ADDRESS";
                final String number = (hasData &= scanner.hasNext()) ? scanner.next() : "NO_NUMBER";

                result.add(new Contact(name, lastname, address, number));

                hasData = scanner.hasNext();
            }
            return result;
        }

        public static int indexOfContact(Contact contact, List<Contact> list) {
            Objects.requireNonNull(contact);
            Objects.requireNonNull(list);

            for (int i = 0, n = list.size(); i < n; i++) {
                if (contact.equals(list.get(i))) {
                    return i;
                }
            }
            return -1;
        }
    }
}

Then appropriate methods to set fields contacts1, contacts2 should use the helper's methods:

// class AddressBook
public void readContacts1(Scanner scanner) {
    contacts1 = ContactHelper.readContacts(scanner);
}

public void readContacts2(Scanner scanner) {
    contacts2 = ContactHelper.readContacts(scanner);
}

A method to join the lists, sort them, and remove the duplicates can be implemented using TreeSet if class Contact implements interface Comparable required for sorting the contacts in their natural order, otherwise custom comparator must be provided.

// another helper method
public static List<Contact> joinContacts(List<Contact> ... contactLists) {
    Set<Contact> sortedWithoutDups = new TreeSet<>();
    for (List<Contact> list : contactLists) {
        if (null != list) {
            sortedWithoutDups.addAll(list);
        }
    }

    return new ArrayList<>(sortedWithoutDups);
}

// setting allContacts
public void joinContacts() {
    allContacts = AddressBook.joinContacts(contacts1, contacts2);
}

This task can be resolved using Stream API (allowing for joining more than 2 lists any of which may be null):

// AddressBook
public void joinContacts() {
    allContacts = ContactHelper.joinContactLists(contacts1, contacts2);
}

// ContactHelper
public static List<Contact> joinContactLists(List<Contact> ... lists) {
    return Arrays.stream(lists)  // Stream<List<Contact>>
                 .filter(Objects::nonNull) // filter out null lists
                 .flatMap(List::stream)  // convert to Stream<Contact>
                 .sorted()               // sort
                 .distinct()             // _and then_ remove duplicates
                 .collect(Collectors.toList());
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42