当前位置: 首页 > 知识库问答 >
问题:

如何在HashMap中打印特定值的键?

彭鸿文
2023-03-14

问题是如何使用printWhiterAvens(arrayList whiteRavens) 方法在HashMap中pritn值=1的图书?在代码中,ive删除了数组中的书籍,这样代码就可以更短。

 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) {
    }

共有1个答案

常元章
2023-03-14

使用流:

yourMap.entrySet().stream().filter(e -> e.getValue() == 1)
                .forEach(element -> System.out.println(element.getKey()));
 类似资料:
  • 公共静态地图>hillsByCounty(列表山){ }我的hashmap的值为(列表中的山名、国家名、高度、经度和纬度)。但是,我只需要打印3个县,每个县的前3个山及其高度,我该怎么做?在这里输入图像描述

  • 我有一个哈希图,其中一些键指向相同的值。我想找到所有相等的值并打印相应的键。 这是我拥有的当前代码: 当前代码将重复项两次添加到列表中,但它也将每个键添加一次。 谢谢。

  • 我试图调用一个值在jsp页面,但有错误和错误说 出现意外错误(类型=内部服务器错误,状态=500)。对于输入字符串:“res” 我的代码; 这就是我从控制器设置hashmap的方法 同样的东西印在java页面上 如何在jsp页面中打印hashmap值?

  • 问题内容: 我正在尝试修复这段代码,该代码是从具有车牌号和车主列表(该格式)的哈希图进行打印的。我试图通过printOwners()仅打印出所有者;但是我不能不打印重复的东西。 我已经玩了一段时间,但似乎无法跳过重复项。 这是我的代码: 问题答案: 要删除重复项,请使用: 或者使用Java 8 和方法:

  • 我有这个JSON: 我想打印键值对,其中键匹配状态和选项,以及时间和它的值。我可以通过使用下面的命令打印时间和所有键值对,但不确定如何提取特定的键值对。 这将产生以下输出: 但我想要的结果是:

  • 我有一个如下数组: 我想做的是找到重复的值,并打印它们。 所以我这样做的方法是转换到,然后转换到,并在上使用。 然后,我使用一个循环通过它,并使用。 这当然会把它们全部打印出来,即使计数是一。 我想加入