Tom
Tom

Reputation: 61

How to remove duplicate elements from list of object

How to remove duplicate elements from books list, where the author and language are the same but title is different.

In this case I'd like to remove any two of position (because they have the same author and language):

 books.add(new Book("Hemigway","english","Old man")); 
 books.add(new Book("Hemigway","english","Lord of the ring"));
 books.add(new Book("Hemigway","english","Border life"));
 

And I'd like to remove any one of position (because they have the same author and language):

 books.add(new Book("Harper Lee","english","Mockingbird")); or
 books.add(new Book("Harper Lee","english","Other books"));

Book.class:

public class Book {
    private String author;
    private String language;
    private String title;
    
    public Book(String author, String language, String title) {
        this.author = author;
        this.language = language;
        this.title = title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getTitle() {
        return this.title;
    }
    
    public void setLanguage(String language) {
        this.language = language;
    }
    
    public String getLanguage() {
        return this.language;
    }
    
    public void setAuthor(String author) {
        this.author = author;
    }
    
    public String getAuthor() {
        return this.author;
    }
}

Main.class:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Book> books = new ArrayList<Book>();
        books.add(new Book("Hemigway","english","Old man"));
        books.add(new Book("Willis","english","Black and white"));
        books.add(new Book("Hemigway","english","Lord of the ring"));
        books.add(new Book("Harper Lee","english","Mockingbird"));
        books.add(new Book("Fitzgerald","english","The catcher"));
        books.add(new Book("Hemigway","english","Border life"));
        books.add(new Book("Harper Lee","english","Other books"));
        
        for(Book book : books) {
            System.out.println(book.getAuthor() + " : "+book.getLanguage()+" : "+book.getTitle());
        }
        
    }

}

Upvotes: 0

Views: 210

Answers (2)

ahrooran
ahrooran

Reputation: 1115

You could achieve this simply by using equals and hashcode, and stream and distinct methods.

Change your Books class to include the following methods.

class Book {

    /*
     * your already written code here. Add following in addition to your code
     * */

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return author.equalsIgnoreCase(book.author) && language.equalsIgnoreCase(book.language);
    }

    @Override
    public int hashCode() {
        return Objects.hash(author, language);
    }
}

Now change main method to have a new list of distinct objects.

public static void main(String[] args) {

    /*
     * your already written code here. Add following in addition to your code
     * */

    List<Book> newBooks = books.stream().distinct().collect(Collectors.toList());

    for (Book book : newBooks)
        System.out.println(book.getAuthor() + " : " + book.getLanguage() + " : " + book.getTitle());

}

Upvotes: 1

Khaja Md Sher E Alam
Khaja Md Sher E Alam

Reputation: 906

You should use two concepts.

  1. Set
  2. equal() and hashcode

Here is the code , Use Set in Main method

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<Book> books = new HashSet<>();
        books.add(new Book("Hemigway","english","Old man"));
        books.add(new Book("Willis","english","Black and white"));
        books.add(new Book("Hemigway","english","Lord of the ring"));
        books.add(new Book("Harper Lee","english","Mockingbird"));
        books.add(new Book("Fitzgerald","english","The catcher"));
        books.add(new Book("Hemigway","english","Border life"));
        books.add(new Book("Harper Lee","english","Other books"));

        for(Book book : books) {
            System.out.println(book.getAuthor() + " : "+book.getLanguage()+" : "+book.getTitle());
        }

    }
}

use equals and hashcode

public    class Book {
    private String author;
    private String language;
    private String title;

    public Book(String author, String language, String title) {
        this.author = author;
        this.language = language;
        this.title = title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return this.title;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getLanguage() {
        return this.language;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getAuthor() {
        return this.author;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Objects.equals(author, book.author) && Objects.equals(language, book.language);
    }

    @Override
    public int hashCode() {
        return Objects.hash(author, language);
    }
}

here is the output:

Harper Lee : english : Mockingbird
Fitzgerald : english : The catcher
Hemigway : english : Old man
Willis : english : Black and white

Upvotes: 0

Related Questions