如何在 HashMap 中打印特定值的键?
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) {
}
使用流:
yourMap.entrySet().stream().filter(e -> e.getValue() == 1)
.forEach(element -> System.out.println(element.getKey()));
过滤地图的所有条目,从地图的键创建一个列表,这些是您的书名。将该列表传递给 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
方法编辑为 return 地图
return createBooksCounter;
您的方法 createBooksCounter 可能应该 return 地图而不是 null
。请注意,通常您将成员声明为提供所需合同的最高通用接口。因此,即使您可以实例化 HashMap,您也将字段声明为 Map。这同样适用于方法参数和 return 类型。关于名称,我将其命名为 bookCounts,因为地图不计算任何东西,只代表书籍及其 'counts'.
public static Map<String, Integer> createBookCounts() {
Map<String, Integer> bookCounts = new HashMap<>();
bookCounts .put("Chicken Abroad",5);
// ...
return bookCounts;
}
方法findWhiteRavens 需要查找地图中count==1 的所有书籍。这与 Map 的正常用法相反,在 Map 中,您通过键而不是值来查找事物。我建议要么使用一个地图,其中计数是关键,值是书籍列表(具有该计数),要么使用带有标题和计数字段的 Book objects 列表。后一种方式在我看来是最简单也是最object-oriented的设计(使用map来收集相关字段是代码味):
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));
您可以像这样一次打印 count==1 的书籍:
books.stream()
.filter(book -> book.getCount() == 1)
.map(book -> book.getTitle());
forEach(book -> System.out.println(book));
如果您更喜欢使用地图,那么这会更有意义:
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);
}
问题是如何使用 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) {
}
使用流:
yourMap.entrySet().stream().filter(e -> e.getValue() == 1)
.forEach(element -> System.out.println(element.getKey()));
过滤地图的所有条目,从地图的键创建一个列表,这些是您的书名。将该列表传递给 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
方法编辑为 return 地图
return createBooksCounter;
您的方法 createBooksCounter 可能应该 return 地图而不是 null
。请注意,通常您将成员声明为提供所需合同的最高通用接口。因此,即使您可以实例化 HashMap,您也将字段声明为 Map。这同样适用于方法参数和 return 类型。关于名称,我将其命名为 bookCounts,因为地图不计算任何东西,只代表书籍及其 'counts'.
public static Map<String, Integer> createBookCounts() {
Map<String, Integer> bookCounts = new HashMap<>();
bookCounts .put("Chicken Abroad",5);
// ...
return bookCounts;
}
方法findWhiteRavens 需要查找地图中count==1 的所有书籍。这与 Map 的正常用法相反,在 Map 中,您通过键而不是值来查找事物。我建议要么使用一个地图,其中计数是关键,值是书籍列表(具有该计数),要么使用带有标题和计数字段的 Book objects 列表。后一种方式在我看来是最简单也是最object-oriented的设计(使用map来收集相关字段是代码味):
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));
您可以像这样一次打印 count==1 的书籍:
books.stream()
.filter(book -> book.getCount() == 1)
.map(book -> book.getTitle());
forEach(book -> System.out.println(book));
如果您更喜欢使用地图,那么这会更有意义:
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);
}