Java 将列表与地图进行比较并输出结果
Java compare list with map and output result
我需要创建一个图书和地图奖项列表。我的目标是浏览清单并查看
The book “<book name>” by <book author> which sold <times published> copies,
received <award for this book> award
如果 Book list author 等于 map Shakespeare == Shakespeare 则输出
The book "Romeo and Juliet" by Shakespeare which sold 4500 copies,
received Too much drama award.
否则
The book "Romeo and Juliet" by Shakespeare which sold 4500 copies,
received no award
我是 Java 的新手,我的问题是如何在循环和比较列表时向 toString 发送新的奖励参数
我的书class
public class Book implements Comparable<Book> {
private String author;
private String name;
private int timesPublished;
public Book(String author, String name, int timesPublished) {
this.author = author;
this.name = name;
this.timesPublished = timesPublished;
}
public int getTimesPublished() {
return timesPublished;
}
public String getName() {
return name;
}
//@Override
public int compareTo(Book compareBook) {
if (this.getTimesPublished() == compareBook.getTimesPublished()) {
return this.getName().toLowerCase().compareTo(compareBook.getName().toLowerCase());
} else {
return this.getTimesPublished() - compareBook.getTimesPublished();
}
}
//@Override
public String toString() {
return String.format("The book \"%s\" by %s which sold %s copies", name, author, timesPublished);
}
}
我的主要
public static void main(String[] args) {
Map<String, String> awards = new HashMap<String, String>();
awards.put("Shakespeare", "Too much drama");
awards.put("Swift", "Survival guide");
awards.put("Austen", "Did not read");
awards.put("Dumas", "Sweet revenge");
List<Book> list = new LinkedList<Book>();
list.add(new Book("Dumas", "The Count of Monte Cristo", 1245));
list.add(new Book("Shakespeare", "Romeo and Juliet", 4500));
list.add(new Book("Austen", "Pride", 1000));
list.add(new Book("Swift", "Aulliver", 1000));
list.add(new Book("Tolstoy", "Best", 1000));
Collections.sort(list);
for(Book temp: list) {
System.out.println(temp);
}
}
为本书class中的作者变量做一个getter。您需要此方法,因为该变量是私有的并且在 class 之外不可访问,除非您像这样为变量创建 getter
public String getAuthorName()
{
return author;
}
然后当您打印输出时,您可以执行以下操作来检查地图中的作者,如果 you get back null 您知道奖项不存在,否则您可以获取奖项地图中的内容
for(Book temp: list) {
String awardName = awards.get(temp.getAuthorName());//use the getter to see what the current book's author is
System.out.println(temp + " received " + (awardName == null ? "no" : awardName) + " award");
}
我需要创建一个图书和地图奖项列表。我的目标是浏览清单并查看
The book “<book name>” by <book author> which sold <times published> copies,
received <award for this book> award
如果 Book list author 等于 map Shakespeare == Shakespeare 则输出
The book "Romeo and Juliet" by Shakespeare which sold 4500 copies,
received Too much drama award.
否则
The book "Romeo and Juliet" by Shakespeare which sold 4500 copies,
received no award
我是 Java 的新手,我的问题是如何在循环和比较列表时向 toString 发送新的奖励参数
我的书class
public class Book implements Comparable<Book> {
private String author;
private String name;
private int timesPublished;
public Book(String author, String name, int timesPublished) {
this.author = author;
this.name = name;
this.timesPublished = timesPublished;
}
public int getTimesPublished() {
return timesPublished;
}
public String getName() {
return name;
}
//@Override
public int compareTo(Book compareBook) {
if (this.getTimesPublished() == compareBook.getTimesPublished()) {
return this.getName().toLowerCase().compareTo(compareBook.getName().toLowerCase());
} else {
return this.getTimesPublished() - compareBook.getTimesPublished();
}
}
//@Override
public String toString() {
return String.format("The book \"%s\" by %s which sold %s copies", name, author, timesPublished);
}
}
我的主要
public static void main(String[] args) {
Map<String, String> awards = new HashMap<String, String>();
awards.put("Shakespeare", "Too much drama");
awards.put("Swift", "Survival guide");
awards.put("Austen", "Did not read");
awards.put("Dumas", "Sweet revenge");
List<Book> list = new LinkedList<Book>();
list.add(new Book("Dumas", "The Count of Monte Cristo", 1245));
list.add(new Book("Shakespeare", "Romeo and Juliet", 4500));
list.add(new Book("Austen", "Pride", 1000));
list.add(new Book("Swift", "Aulliver", 1000));
list.add(new Book("Tolstoy", "Best", 1000));
Collections.sort(list);
for(Book temp: list) {
System.out.println(temp);
}
}
为本书class中的作者变量做一个getter。您需要此方法,因为该变量是私有的并且在 class 之外不可访问,除非您像这样为变量创建 getter
public String getAuthorName()
{
return author;
}
然后当您打印输出时,您可以执行以下操作来检查地图中的作者,如果 you get back null 您知道奖项不存在,否则您可以获取奖项地图中的内容
for(Book temp: list) {
String awardName = awards.get(temp.getAuthorName());//use the getter to see what the current book's author is
System.out.println(temp + " received " + (awardName == null ? "no" : awardName) + " award");
}