从 HashSet 中的 objects 返回连接某些字段的字符串
Returning String of joined certain fields from objects in HashSet
我正在尝试为 HashSet 编写 getter,其中 Object 包含字符串和整数的多个属性。
对于存储在 HashSet 中的每个 object,Getter 应该 return 加入仅两个字符串字段的字符串,用逗号分隔。
我可以使用循环来编写它,这样并不难,但我想找到更短、更优雅、更聪明的解决方案。
我尝试使用 String.join(),但它不接受 Object 的 HashSet。
使用 Object.toString() 不是一个选项,因为 toString() 应该 return 更多 Object 的属性,而这个 getter 应该 return 只有两个.
让我们想象一下带有 Book object 和 Borrower object 的简化图书馆,其中 Borrower 借来的每一本书都存储在 Borrower object.
的 HashSet 中
import java.util.HashSet;
public class Main{
public static void main(String []args){
Borrower john = new Borrower("John");
Book book1 = new Book("Title1", "Author1", "Isbn1", 100);
Book book2 = new Book("Title2", "Author2", "Isbn2", 200);
Book book3 = new Book("Title3", "Author3", "Isbn3", 300);
john.borrowBook(book1);
john.borrowBook(book2);
john.borrowBook(book3);
System.out.println(john.getBorrowed());
// It should print
// Title1, Author1
// Title2, Author2
// Title3, Author3
}
}
class Book {
String title;
String author;
String isbn;
int pageNum;
Book(String t, String a, String i, int p){
this.title = t;
this.author = a;
this.isbn = i;
this.pageNum = p;
}
public String getAuthor() { return author; }
public String getTitle() { return title; }
}
class Borrower {
String name;
HashSet <Book> onLoan = new HashSet<Book>();
Borrower(String n){
this.name = n;
}
public boolean borrowBook(Book b) {
return onLoan.add(b);
}
public String getBorrowed(){
if(onLoan.isEmpty())
return "No books borrowed";
else{
// It should return joined string of only title and author for every book in HashSet,
//title and author separated with coma, each object in new line, like "title, author \n"
return "---";
}
}
}
Borrower的getBorrowed()应该return加入HashSet中每本书的唯一标题和作者的String。
这是一个使用流的简单解决方案:
public String getBorrowed(){
if(onLoan.isEmpty())
return "No books borrowed";
else{
return onLoan.stream().map(b -> b.getTitle() + ", " + b.getAuthor()).collect(Collectors.joining("\n"));
}
}
输出:
Title3, Author3
Title2, Author2
Title1, Author1
您可以使用 Java 8 流 api 以及 map 和 reduce 方法的组合。
这是我的解决方案:
String output = onLoan.stream()
.map(book -> book.getTitle() + ", " + book.getAuthor())
.reduce("", (value, current) -> value + "\n" + current);
因为是HashSet所以输出的顺序可能跟加法顺序不一样
我正在尝试为 HashSet 编写 getter,其中 Object 包含字符串和整数的多个属性。 对于存储在 HashSet 中的每个 object,Getter 应该 return 加入仅两个字符串字段的字符串,用逗号分隔。
我可以使用循环来编写它,这样并不难,但我想找到更短、更优雅、更聪明的解决方案。 我尝试使用 String.join(),但它不接受 Object 的 HashSet。 使用 Object.toString() 不是一个选项,因为 toString() 应该 return 更多 Object 的属性,而这个 getter 应该 return 只有两个.
让我们想象一下带有 Book object 和 Borrower object 的简化图书馆,其中 Borrower 借来的每一本书都存储在 Borrower object.
的 HashSet 中import java.util.HashSet;
public class Main{
public static void main(String []args){
Borrower john = new Borrower("John");
Book book1 = new Book("Title1", "Author1", "Isbn1", 100);
Book book2 = new Book("Title2", "Author2", "Isbn2", 200);
Book book3 = new Book("Title3", "Author3", "Isbn3", 300);
john.borrowBook(book1);
john.borrowBook(book2);
john.borrowBook(book3);
System.out.println(john.getBorrowed());
// It should print
// Title1, Author1
// Title2, Author2
// Title3, Author3
}
}
class Book {
String title;
String author;
String isbn;
int pageNum;
Book(String t, String a, String i, int p){
this.title = t;
this.author = a;
this.isbn = i;
this.pageNum = p;
}
public String getAuthor() { return author; }
public String getTitle() { return title; }
}
class Borrower {
String name;
HashSet <Book> onLoan = new HashSet<Book>();
Borrower(String n){
this.name = n;
}
public boolean borrowBook(Book b) {
return onLoan.add(b);
}
public String getBorrowed(){
if(onLoan.isEmpty())
return "No books borrowed";
else{
// It should return joined string of only title and author for every book in HashSet,
//title and author separated with coma, each object in new line, like "title, author \n"
return "---";
}
}
}
Borrower的getBorrowed()应该return加入HashSet中每本书的唯一标题和作者的String。
这是一个使用流的简单解决方案:
public String getBorrowed(){
if(onLoan.isEmpty())
return "No books borrowed";
else{
return onLoan.stream().map(b -> b.getTitle() + ", " + b.getAuthor()).collect(Collectors.joining("\n"));
}
}
输出:
Title3, Author3
Title2, Author2
Title1, Author1
您可以使用 Java 8 流 api 以及 map 和 reduce 方法的组合。
这是我的解决方案:
String output = onLoan.stream()
.map(book -> book.getTitle() + ", " + book.getAuthor())
.reduce("", (value, current) -> value + "\n" + current);
因为是HashSet所以输出的顺序可能跟加法顺序不一样