[英]How to print keys of specific values in HashMap?
问题是如何使用printWhiteRavens(ArrayList<String> whiteRavens)
方法打印 HashMap 中 value = 1 的书籍? 在代码中,我删除了带有书籍的数组,因此代码可以更短。
public static HashMap<String, Integer> createBooksCounter() {
HashMap<String,Integer> createBooksCounter = new HashMap<>();
createBooksCounter.put("Chicken Abroad",5);
createBooksCounter.put("Lord With Sins",3);
createBooksCounter.put("Fish And Horses",4);
createBooksCounter.put("Mistress Of Devotion",2);
createBooksCounter.put("Guardians And Gangsters",3);
createBooksCounter.put("Bandit Of The Void",1);
createBooksCounter.put("Lions Of Dread",1);
createBooksCounter.put("Future Of Fire",2);
createBooksCounter.put("Driving Into The Depths",1);
createBooksCounter.put("Starting The Demons",1);
createBooksCounter.put("Maid With Blue Eyes",2);
createBooksCounter.put("Lovers In The Forest",1);
createBooksCounter.put("Destruction Of The Faceless Ones",1);
return null;
}
public static void countBook(HashMap<String, Integer> booksCounter, String book) {
}
public static ArrayList<String> findWhiteRavens(HashMap<String, Integer> booksCounter) {
return null;
}
public static void printWhiteRavens(ArrayList<String> whiteRavens) {
return null;
}
public static void main(String[] args) {
}
使用 Stream:
yourMap.entrySet().stream().filter(e -> e.getValue() == 1)
.forEach(element -> System.out.println(element.getKey()));
过滤 map 的所有条目,从地图的键中创建一个列表,这些是您的书名。 将该列表传递给printWhiteRavens
,然后打印标题:
public static void main(String[] args) {
Map<String,Integer> map = createBooksCounter();
List<String> list = map.entrySet().stream().
filter(e->e.getValue()==1).
map(e->e.getKey()).
collect(Collectors.toList());
printWhiteRavens(list);
}
public static void printWhiteRavens(List<String> whiteRavens) {
whiteRavens.stream().forEach(System.out::println);
}
您还必须编辑您的createBooksCounter
方法以使用返回 map
return createBooksCounter;
您的方法 createBooksCounter 可能应该返回 map 而不是null
。 请注意,通常您将成员声明为提供所需合同的最高通用接口。 因此,即使您可以实例化 HashMap,您也可以将该字段声明为 Map。 方法参数和返回类型也是如此。 关于名称,我将其命名为 bookCounts,因为 map 不计算任何内容,仅代表书籍及其“计数”。
public static Map<String, Integer> createBookCounts() {
Map<String, Integer> bookCounts = new HashMap<>();
bookCounts .put("Chicken Abroad",5);
// ...
return bookCounts;
}
findWhiteRavens 方法需要查找 map 中 count==1 的所有书籍。 这与 Map 的正常用法相反,在这种用法中,您通过键而不是值来查找内容。 我建议要么使用 map ,其中 count 是键,值是书籍列表(具有该计数),或者使用带有字段标题和计数的 Book 对象的 List。 后一种方法在我看来是最简单和最面向对象的设计(使用地图收集相关字段是一种代码味道):
class Book {
String title;
int count;
Book(String title, int count) {
this.title = title;
this.count = count;
}
// accessors to be added
}
// ...
List<Book> books = new ArrayList<>();
books.add(new Book("Chicken Abroad", 5));
您可以像这样在 go 中打印 count==1 的书籍:
books.stream()
.filter(book -> book.getCount() == 1)
.map(book -> book.getTitle());
forEach(book -> System.out.println(book));
如果您更喜欢使用 map,那么这将更有意义:
private static Map<Integer, List<String>> bookTitlesByCount = new HashMap<>();
public static void main(String[] args) {
addBookTitle("Chicken Abroad", 5);
List<String> whiteRavens = bookTitlesByCount.get(1);
// print them
}
private static void addBookTitle(String title, int count) {
List<String> bookTitles = bookTitlesByCount.get(count);
if (bookTitles == null) {
bookTitles = new ArrayList<>();
bookTitlesByCount.put(count, bookTitles);
}
bookTitles.add(title);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.