将映射的值分配给输入字符串

时间:2019-01-26 09:11:49

标签: java hashmap

我无法获取根据映射值输入的字符串的值?? 我想要我想要获得的价值8118198920

import java.util.*;

class maptable1 {
    public static void main(String args[]) {
        HashMap<String, Integer> hm =
                new HashMap<String, Integer>();
        hm.put("A", Integer.valueOf(1));
        hm.put("B", Integer.valueOf(2));
        hm.put("C", Integer.valueOf(3));
        hm.put("D", Integer.valueOf(4));
        hm.put("E", Integer.valueOf(5));
        hm.put("F", Integer.valueOf(6));
        hm.put("G", Integer.valueOf(7));
        hm.put("H", Integer.valueOf(8));
        hm.put("I", Integer.valueOf(9));
        hm.put("J", Integer.valueOf(10));
        hm.put("K", Integer.valueOf(11));
        hm.put("L", Integer.valueOf(12));
        hm.put("M", Integer.valueOf(13));
        hm.put("N", Integer.valueOf(14));
        hm.put("O", Integer.valueOf(15));
        hm.put("P", Integer.valueOf(16));
        hm.put("Q", Integer.valueOf(17));
        hm.put("R", Integer.valueOf(18));
        hm.put("S", Integer.valueOf(19));
        hm.put("T", Integer.valueOf(20));
        hm.put("U", Integer.valueOf(21));
        hm.put("V", Integer.valueOf(22));
        hm.put("W", Integer.valueOf(23));
        hm.put("X", Integer.valueOf(24));
        hm.put("Y", Integer.valueOf(25));
        hm.put("Z", Integer.valueOf(26));

        System.out.println("The Value is: " + hm.get("HARSHIT"));
    }
}

2 个答案:

答案 0 :(得分:1)

哈希图不能那样工作。

要产生所需的内容,您需要使用字符串中的每个字符调用String myString = "HARSHIT"; String result = Arrays.stream(myString.split("")) .map(hm::get) .map(String::valueOf) .reduce("", String::concat); System.out.println("The Value is: " + result); ,将获得的整数转换为字符串,然后将所有这些字符串连接在一起。

一种实现方法是使用流:

websocket_server::connection_ptr con = wsServer->get_con_from_hdl(hdl);
con->append_header("access-control-allow-origin", "*");

还要注意,您不需要hm.get()。您可以只使用整数本身。

答案 1 :(得分:-1)

HashMap是基于Map的{​​{1}},它存储collectionkey对,即value(key,value)对。 如果将(K,V) 1放入键“ A”,则可以从value“ A”获得value 1。 因此,在您的情况下,您必须分别获取每个键“ H”,“ A”,“ R”,“ S”,“ H”,“ I”,“ T”的值,并将{{1} }以获得所需的结果。

key