0

I want to see if two objects in the same array list are the same. Similarly if there are not objects that are the same in the array list I also want to display an appropriate message. At the moment it is printing too much. I just want it to display one line: "there are books that are the same" or "there are NO books that are the same". Here are my efforts so far.

public static void main(String[] args) {

    List<Book> listOfBooks = new ArrayList<>();
    Book book1 = new Book();
    Book book2 = new Book();
    Book book3 = new Book();

    listOfBooks.add(book1);
    listOfBooks.add(book2);
    listOfBooks.add(book3);
    listOfBooks.add(book2);

    for (Book bookA : listOfBooks) {
        for (Book bookB : listOfBooks) {
            if (bookA.equals(bookB)) {
                System.out.println("there are books that are the same");
            }
            System.out.println("there are NO books that are the same");
        }

    }
}

Thanks in advance.

EDIT: I amended my code as such according to "duplicate question"

List<Book> listOfBooks = new ArrayList<>();
    Set<Book> setOfBooks = new HashSet<Book>(listOfBooks);

    Book book1 = new Book();
    Book book2 = new Book();
    Book book3 = new Book();

    listOfBooks.add(book1);
    listOfBooks.add(book2);
    listOfBooks.add(book3);

    if (setOfBooks.size() < listOfBooks.size()) {
        System.out.println("there are duplicates");
    } else {
        System.out.println("there are no duplicates");
    }

Still prints "there are duplicates" when there clearly are not.

RESOLVED: Ordering of my code was incorrect. Solution-

    List<Book> listOfBooks = new ArrayList<>();

    Book book1 = new Book();
    Book book2 = new Book();
    Book book3 = new Book();

    listOfBooks.add(book1);
    listOfBooks.add(book2);
    listOfBooks.add(book3);

    Set<Book> setOfBooks = new HashSet<>(listOfBooks);

    if (setOfBooks.size() < listOfBooks.size()) {
        System.out.println("there are duplicates");
    } else {
        System.out.println("there are no duplicates");
    }
4
  • Hi @shmosel this is not a duplicate question, actually the problem in this program, he missed the else statement. BigAl1992 you should add this line 'System.out.println("there are NO books that are the same");' within else condition, otherwise it will run all time doesn't matter book are same or not. Commented Nov 1, 2017 at 7:41
  • @BhushanUniyal The else won't help if it's inside the loop. You need to set a variable. I'm not sure what you think it's not a duplicate. Commented Nov 1, 2017 at 7:43
  • @shmosel I changed my code as such List<Book> listOfBooks = new ArrayList<>(); Set<Book> setOfBooks = new HashSet<Book>(listOfBooks); Book book1 = new Book(); Book book2 = new Book(); Book book3 = new Book(); listOfBooks.add(book1); listOfBooks.add(book2); listOfBooks.add(book3); if (setOfBooks.size() < listOfBooks.size()) { System.out.println("there are duplicates"); } else { System.out.println("there are no duplicates"); } and it still prints "there are duplicates" when there clearly arnt Commented Nov 1, 2017 at 7:43
  • You're initializing setOfBooks too early. You need to do it after your list is populated. Commented Nov 1, 2017 at 7:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.