【问题标题】:How to print keys of specific values in HashMap?如何在HashMap中打印特定值的键?
【发布时间】:2022-01-09 16:46:06
【问题描述】:

问题是如何使用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) {
    }

【问题讨论】:

  • printWhiteRavensArrayList&lt;String&gt; whiteRavens参数的作用是什么?不应该是HashMap&lt;String, Integer&gt;吗?
  • 参数whiteRavens是HashMap中value=1的书籍的ArrayList
  • 在下面查看我的答案。

标签: java arraylist hashmap


【解决方案1】:

您的方法 createBooksCounter 可能应该返回地图而不是 null。请注意,通常您将成员声明为提供所需合同的最高通用接口。因此,即使您可以实例化 HashMap,您也可以将该字段声明为 Map。方法参数和返回类型也是如此。关于名称,我将其命名为 bookCounts,因为地图不计算任何内容,仅代表书籍及其“计数”。

public static Map<String, Integer> createBookCounts() {
    Map<String, Integer> bookCounts = new HashMap<>();
    bookCounts .put("Chicken Abroad",5);
    // ...
    return bookCounts;
}

findWhiteRavens 方法需要查找地图中所有 count==1 的书籍。这与 Map 的正常用法相反,在 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);
}

【讨论】:

    【解决方案2】:

    过滤地图的所有条目,根据地图的键创建一个列表,这些是您的书名。将该列表传递给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 createBooksCounter;
    

    【讨论】:

    • 不能将方法printWhiteRavens 置于public static void main 行之上?
    • 方法的顺序无关紧要。你试过了吗?
    • 谢谢它的工作,但解决方案远高于我的水平:)。请您花点时间看看原始代码中的countBookfindWhiteRavens 方法。这些方法可以用来解决任务吗?感谢您宝贵的时间。
    【解决方案3】:

    使用流:

    yourMap.entrySet().stream().filter(e -> e.getValue() == 1)
                    .forEach(element -> System.out.println(element.getKey()));
    

    【讨论】:

    • 谢谢它确实有效,但此任务中的问题是将 HashMap 转换为 ArrayList,然后打印价值 = 1 的书籍。好吧,我必须结束这个问题 :(
    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    相关资源
    最近更新 更多