使用构造函数与Lambda表达式(设置比较器)

huangapple go评论86阅读模式
英文:

Use Constructor with Lambda expression (Set Comparator)

问题

我被赋予了一个任务,要使用构造函数TreeSet(Comparator<E> comparator)以lambda表达式的形式使集合按书名排序。

以下信息已给出:

public class Book implements Comparable<Book> {
    private String isbn;
    private String title;
    private String author;
    
    public int compareTo(Book obj) {
      return isbn.compareTo(obj.isbn);
    }
    // 带全部参数的构造函数和获取方法
}

我目前考虑的是:
我认为这个构造函数允许您定义比较器。也许我应该这样做:

Comparator<String> comp = 
    (String title, String obj.title) -> (title.compareTo(obj.title));

然后将此 comp 放入 TreeSet<Book> bookSet = new TreeSet<Book>(comp);

然而,这似乎行不通。我认为我需要在创建新的 TreeSet 时在同一行中使用 lambda 表达式,然而,我不确定如何做到这一点。

英文:

I am given the task to use the constructor TreeSet(Comparator&lt;E&gt; comparator) with a lambda expression to make the set be sorted by book title.

The following information is given:

public class Book implements Comparable&lt;Book&gt; {
    private String isbn;
    private String title;
    private String author;
    
    public int compareTo(Book obj) {
      return isbn.compareTo(obj.isbn);
    }
    // all-args constructor and getters
}

What I have thought about so far is:
I think that the constructor allows you to define the Comparator. Perhaps I should do:

Comparator&lt;String&gt; comp = 
    (String title, String obj.title) -&gt; (title.compareTo(obj.title));

Then put this comp into the TreeSet&lt;Book&gt; bookSet = new TreeSet&lt;Book&gt;(comp);

However, that doesn't seem to work. I think what I need to do is use the lambda expression in the same row when creating the new TreeSet, however, I am unsure how to do so.

答案1

得分: 4

你需要一个 Comparator<Book>,而不是 Comparator<String>。最简单的方法是使用 Comparator.comparing

TreeSet<Book> bookSet = new TreeSet<>(Comparator.comparing(Book::getTitle));
英文:

You need a Comparator&lt;Book&gt;, not a Comparator&lt;String&gt;. The easiest way to get one would be to use Comparator.comparing:

TreeSet&lt;Book&gt; bookSet = new TreeSet&lt;&gt;(Comparator.comparing(Book::getTitle));

答案2

得分: 2

你正在寻找一个书籍比较器。即使你要按书名进行比较,你仍然需要使用Comparator<Book>

为了重用你的代码,你需要进行如下更正:

Comparator<Book> comp = 
    (Book book1, Book book2) -> (book1.getTitle().compareTo(book2.getTitle()));

这可以重写为:

Comparator<Book> comp = 
    (book1, book2) -> (book1.getTitle().compareTo(book2.getTitle()));

或者

Comparator<Book> comp = Comparator.comparing(Book::getTitle);

然后,你可以将comp作为参数传递给TreeSet的构造函数。

英文:

You're looking for a book comparator. Even if you're going to compare by book title, you'll still have to use Comparator&lt;Book&gt;.

To reuse your code, the correction you need is:

Comparator&lt;Book&gt; comp = 
    (Book book1, Book book2) -&gt; (book1.getTitle().compareTo(book2.getTitle()));

Which can be rewritten as

Comparator&lt;Book&gt; comp = 
    (book1, book2) -&gt; (book1.getTitle().compareTo(book2.getTitle()));

Or

Comparator&lt;Book&gt; comp = Comparator.comparing(Book::getTitle);

You can then pass comp as argument to TreeSet's constructor

答案3

得分: 1

**书籍类**

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

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

  public String getIsbn() {
    return isbn;
  }

  public void setIsbn(String isbn) {
    this.isbn = isbn;
  }

  public String getTitle() {
    return title;
  }

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

  public String getAuthor() {
    return author;
  }

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

  @Override
  public String toString() {
    return "Book{" +
           "isbn='" + isbn + '\'' +
           ", title='" + title + '\'' +
           ", author='" + author + '\'' +
           '}';
  }
}

**主类**

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class Main {
  public static void main(String[] args) {
    Book book1 = new Book("isbn1", "第一本书", "作者1");
    Book book2 = new Book("isbn2", "第二本书", "作者2");
    Book book3 = new Book("isbn3", "第三本书", "作者3");

    // 方法一
    Set<Book> books1 = new TreeSet<>((book, thisBook) -> book.getTitle().compareTo(thisBook.getTitle()));

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

    // 方法二
    Set<Book> books2 = new TreeSet<>(Comparator.comparing(Book::getTitle));

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

    System.out.println(books1);
    System.out.println(books2);
  }
}

由于您将值插入到set中,因此我建议您在Book类中添加hashCodeequals方法,以便识别唯一的书籍对象。

英文:

Book:

public class Book {
private String isbn;
private String title;
private String author;
public Book(String isbn, String title, String author) {
this.isbn = isbn;
this.title = title;
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return &quot;Book{&quot; + &quot;isbn=&#39;&quot; + isbn + &#39;\&#39;&#39; + &quot;, title=&#39;&quot; + title + &#39;\&#39;&#39; + &quot;, author=&#39;&quot; + author + &#39;\&#39;&#39; + &#39;}&#39;;
}
}

Main class:

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Book book1 = new Book(&quot;isbn1&quot;, &quot;First Book&quot;, &quot;Author 1&quot;);
Book book2 = new Book(&quot;isbn2&quot;, &quot;Second Book&quot;, &quot;Author 2&quot;);
Book book3 = new Book(&quot;isbn3&quot;, &quot;Third Book&quot;, &quot;Author 3&quot;);
//Method first
Set&lt;Book&gt; books1 = new TreeSet&lt;&gt;((book, thisBook) -&gt; book.getTitle().compareTo(thisBook.getTitle()));
books1.add(book3);
books1.add(book2);
books1.add(book1);
//Method second
Set&lt;Book&gt; books2 = new TreeSet&lt;&gt;(Comparator.comparing(Book::getTitle));
books2.add(book2);
books2.add(book1);
books2.add(book3);
System.out.println(books1);
System.out.println(books2);
}
}

As you are inserting values into a set so I recommend you to add hashcode and equals method in Book class as well to identify the unique book object.

huangapple
  • 本文由 发表于 2020年4月10日 20:40:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61140444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定