Java 如何将元素添加到哈希图中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40857386/
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-08-11 22:57:20  来源:igfitidea点击:

How do you add elements to a hashmap?

javahashmapkeykey-value

提问by Jghorton14

I am new to HashMaps. I was wondering if I could add ask the user what he or she wanted the key to be and what they wanted the value to be. So far I am getting the HashMap to print out but for some reason when I ask for the view the value it returns null and when I try to remove the key it also doesn't remove it.

我是 HashMaps 的新手。我想知道我是否可以添加询问用户他或她想要的密钥是什么以及他们想要的价值是什么。到目前为止,我正在打印 HashMap,但出于某种原因,当我要求查看该值时,它返回 null,而当我尝试删除键时,它也没有删除它。

import java.util.Scanner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapDemo {
  public static void main (String [] args){
    //declaring a hashmap   
    HashMap<Integer, String> hm = new HashMap<Integer, String>();

    //adding elements to the hashmap
    hm.put(1, "Josh");
    hm.put(2, "Max");
    hm.put(3, "Karan");

    //declaring the Scanner
    Scanner sc = new Scanner(System.in);
    System.out.println("Would you like to add Elements to the hashmap?");
    String scYN = sc.nextLine();
    scYN = scYN.toLowerCase();
    if(scYN.equals("yes")) {
        System.out.println("What would the key to be?");
        int key = sc.nextInt();
        sc.nextLine();
        System.out.println("What would the value to be?");
        String val = sc.nextLine();
        hm.put(key, val);           
    }else if (scYN.equals("no")) {
        System.out.println("False");
    }else{
        System.out.println("False");
    }

    //displaying content 
    Set set = hm.entrySet();
    Iterator iterator = set.iterator();
    while(iterator.hasNext()){
        Map.Entry mentry = (Map.Entry)iterator.next();
        System.out.print("Key is: "+ mentry.getKey() + " & Value is: ");
        System.out.println(mentry.getValue());
    }

    /* Get values based on key*/

    System.out.println("What value would you like to find?");
    String fVal = sc.nextLine();        
    String var= hm.get(fVal);
    System.out.println("Value at index " + fVal + " is: "+ var);

    /* Remove values based on key*/

    System.out.println("Would you like to remove a key?");
    String remKey = sc.nextLine();
    hm.remove(remKey);
    System.out.println("Map key and values after removal:");
    Set set2 = hm.entrySet();
    Iterator iterator2 = set2.iterator();
    while(iterator2.hasNext()) {
        Map.Entry mentry2 = (Map.Entry)iterator2.next();
        System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
        System.out.println(mentry2.getValue());
    }

}

}

}

Thank you for your time

感谢您的时间

采纳答案by Salvador Hernandez

It gives you a null value because when you use get(), the key being passed as a paramanter (fVal) is a String, whereas in the Hashmap has its key defined as an Integer.

它给你一个空值,因为当你使用get() 时,作为参数传递的键 ( fVal) 是一个String,而在 Hashmap 中它的键定义为一个Integer

To solve this issue, have the fVal variable be an Integerthat gets the nextInt()

为了解决这个问题,让 fVal 变量是一个获取nextInt()整数

int fVal = sc.nextInt();

You did this when soliciting data-entry, "What would the key to be?". Do the same when prompting for search criterion, "What value would you like to find?", and when prompting for deletion, "Would you like to remove a key?".

您在请求数据输入时这样做了,“关键是什么?”。在提示搜索条件时执行相同的操作,“您想找到什么值?”,以及在提示删除时,“您想删除一个键吗?”。