在数组列表中查找重复的元素

时间:2016-09-18 03:48:16

标签: java

以下代码假设为作者和标题取一个字符串,以检查该精确书是否在数组列表中,如果是,那么它将返回数组中的副本数。到目前为止,它只是检查这本书是否在数组列表中,但我想知道是否有任何IPA我可以用来查找数组列表中的重复元素

public class Book{
       private title;
       private author;
    }

public class Library {
        private ArrayList<Book>libraryBooks;
         public int checkNumCopies(String title,String author){
            int numBookCopies = 0;
            for(Book b:libraryBooks){
                if((b.equals(title))&& (b.equals(author))){
                    return "Book is in the library";    
                    }
                 else
                     return "Book is not in the library";
                }
            }

5 个答案:

答案 0 :(得分:1)

Book

中正确实现equals和hashcode实现
class Book {

    private String author;
    private String title;

    //Getters and Setters
    //hashCode and equals impl
    //toString impl

}

这可以使用流来实现

    List<Book> books = Arrays.asList(new Book("book-1", "title-1"), new Book("book-2", "title-2"), new Book("book-3", "title-3"),
            new Book("book-1", "title-1"), new Book("book-2", "title-2"));
    Map<Book, Long> bookCount = books.stream().collect(Collectors.groupingBy(b -> b, Collectors.counting()));
    System.out.println(bookCount);

输出

{Book [author=book-3, title=title-3]=1, Book [author=book-2, title=title-2]=2, Book [author=book-1, title=title-1]=2}

答案 1 :(得分:0)

如果你想找到一本书的副本数量,那就是:

public int checkNumCopies(String title, String author) {
    int numBookCopies = 0;
    for (Book b : libraryBooks) {
        if ((b.title.equals(title)) && (b.author.equals(author))) {
            numBookCopies++;
        }
    }
    return numBookCopies;
}

否则,您可以发送书籍对象,而不是单独发送标题和作者。这是更好的方式:

public int checkNumCopies(Book book) {
    int numBookCopies = 0;
    for (Book b : libraryBooks) {
        if ((b.title.equals(book.title)) && (b.author.equals(book.author))) {
            numBookCopies++;
        }
    }
    return numBookCopies;
}

由于您已创建字段private,因此需要编写getter方法以从字段中获取值。

答案 2 :(得分:0)

您的Book字段缺少类型(我假设您需要String s),并且您没有这些字段的访问者(或 getters )。另外,我可能会覆盖equals并使用它。像,

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

    private String title;
    private String author;

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Book) {
            Book b = (Book) obj;
            return b.title.equals(title) && b.author.equals(author);
        }
        return false;
    }
}

然后使用它,我会做类似

的事情
private List<Book> libraryBooks = new ArrayList<>();

public int checkNumCopies(String title, String author) {
    Book toFind = new Book(title, author);
    int numBookCopies = 0;
    for (Book b : libraryBooks) {
        if (b.equals(toFind)) {
            numBookCopies++;
        }
    }
    return numBookCopies;
}
Java 8+中的

,如

Book toFind = new Book(title, author);
return (int) libraryBooks.stream().filter(book -> book.equals(toFind)).count();

答案 3 :(得分:0)

或者您可以在Book上创建一个equals方法然后使用HashSet。像

这样的东西
   {
        //Resources
        "TreesRespawn": true,
        "RocksRespawn": true,
        "MiscResourceRespawn": true,
.
.
.
.
.
.

我完全不确定你需要什么(抱歉不能理解这个问题),但希望至少能给你一些想法。

注意:我没有编译这个,所以请原谅任何编译错误。

答案 4 :(得分:0)

以下是使用java stream API的解决方案

public class Library {

    private ArrayList<Book> libraryBooks;

    public long checkNumCopies(String title, String author) {

        return libraryBooks
            .stream()
            .filter(book -> book.getAuthor().equals(author) && book.getTitle().equals(title))
            .count();

    }
}

我预先假定Book类中有getter方法。