java 如何使用扫描仪从用户那里获取Hashmap的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43478039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:27:37  来源:igfitidea点击:

How to get input from user for Hashmap using Scanner

javacollectionshashmap

提问by Sainath Pawar

How to get input from user for Hashmap using scanner and print the respective hashmap

如何使用扫描仪从用户那里获取哈希图的输入并打印相应的哈希图

import java.util.HashMap;

public class Map {

public static void main(String[] args) {
 // TODO Auto-generated method stub

  HashMap <Integer,Integer> hmap = new HashMap<Integer,Integer>();
    Scanner in = new Scanner(System.in);

          for(int i=0;i<3;i++){

            Integer a=in.nextInt();

           Integer b=in.nextInt();
           hmap.put(a, b);

           System.out.println(hmap.put(a,b));


        }

    }


  }

I am not getting the desired output. I want to print what is inserted in hmap.

我没有得到所需的输出。我想打印插入的内容hmap

回答by ProgrammerBoy

Change your System.out.printlnstatement to,

将您的System.out.println声明更改为,

System.out.println(hmap.get(a));

回答by Saideep Sambaraju

What do you expect this to do? It is wrong, why would you print hmap.put(a,b)

你希望这能做什么?错了,为什么要打印hmap.put(a,b)

System.out.println(hmap.put(a,b));

Just take it out of the for loop and print it separately

只需将其从for循环中取出并单独打印即可

 for (Map.Entry<Integer, Integer> pair: hmap.entrySet()) {
  System.out.println(pair.getKey() + "->" + pair.getValue());
}

回答by JosephCW

Javadoc for the HashMap util can be found here.

可以在此处找到 HashMap 实用程序的 Javadoc 。

To add a key/value pair to a HashMap you use hashmap.put(x, y).

要将键/值对添加到 HashMap,您可以使用hashmap.put(x, y).

Using hashmap.get(x)at a later time will return y.

使用hashmap.get(x)在稍后的时间将返回年。

Edit: It looks like from other comments you are trying to have it print out both the key and the value. In the case that you are trying to print out all keys and values something like this may work.

编辑:从其他评论看来,您试图让它打印出键和值。在您尝试打印出所有键和值的情况下,这样的事情可能会奏效。

hashMap.forEach((e, k) -> {
    System.out.println("E: " + e + " K: " + k);
})

If this is not your intended purpose I would strongly recommend reading over the method summary section of the javadocs.

如果这不是您的预期目的,我强烈建议您阅读 javadocs 的方法摘要部分。

回答by Kaustav Pallob

This is another way by which we can insert data in Hashmapin Run time:

这是我们可以Hashmap在运行时插入数据的另一种方式:

import java.util.*;

public class Maap {

     public static void main(String[] args) {

         HashMap <Integer,String> hmap = new HashMap<Integer,String>();
         Scanner in = new Scanner(System.in);

         for(int i=0;i<3;i++){

             Integer a=in.nextInt();
             String b=in.next();

             hmap.put(a,b);
  //         System.out.println(hmap.get(b)+" "+hmap.get(a)); 
         }

         for(Map.Entry m:hmap.entrySet()) {    
             System.out.println(m.getKey()+" "+m.getValue());    
         }  
     }
}