简体   繁体   English

如何使用循环从StringBuilder将键/值放入HashMap? -Java

[英]How to put key/values into a HashMap from StringBuilder using loop? - Java

I'm using several techniques here, so it's hard to find help online. 我在这里使用了几种技术,因此很难在线找到帮助。 I need to populate a HashMap<String, String> with values I take from part of a StringBuilder , then take the keys and add them into an ArrayList<String> , then print the list. 我需要使用从StringBuilder一部分获取的值填充HashMap<String, String> ,然后获取键并将它们添加到ArrayList<String> ,然后打印列表。 But when I print, I get a list full of nulls . 但是当我打印时,我得到的列表中充满了nulls I don't know why, I thought it would print the values I got from the StringBuilder . 我不知道为什么,我认为它将打印从StringBuilder获得的值。 It should print: Values taken from the hashmap keys: ABCDEFGHI . 它应该打印: Values taken from the hashmap keys: ABCDEFGHI (The reason I used StringBuilder is because String is immutable, is this correct thinking too?) (我之所以使用StringBuilder是因为String是不可变的,这也是正确的想法吗?)

Also, I figured using a loop to print the list is okay, since my keys are actually numbers. 另外,我发现使用循环来打印列表是可以的,因为我的键实际上是数字。

I've never created a HashMap before, so maybe I'm missing something. 我以前从未创建过HashMap,所以也许我缺少了一些东西。 Thanks for your help. 谢谢你的帮助。

Here is my code: 这是我的代码:

    // create HashMap from String
    StringBuilder alphaNum = new StringBuilder("1A2B3C4D5E6F7G8H9I");

    Map<String, String> myAlphaNum = new HashMap<String, String>(9);

    // for loop puts key and values in map, taken from String
    for (int i = 0; i < alphaNum.length();)
    {
        myAlphaNum.put(alphaNum.substring(i, ++i), alphaNum.substring(i, ++i));
    }
    if (myAlphaNum.containsKey(1))
        System.out.println("Key is there.");
    else
        System.out.println("Key is null.");

    // create ArrayList, add values to it using map keys
    ArrayList<String> arrayList = new ArrayList<String>();

    // for loop gets the "number" keys from HashMap to get the "letter" values
    for (int j = 1; j <= myAlphaNum.size(); j++)
        arrayList.add(myAlphaNum.get(j));


    System.out.print("Values taken from the hashmap keys: ");
    for (String list : arrayList)
        System.out.print(list);

Console: 安慰:

Key is null.

Values taken from the hashmap keys: nullnullnullnullnullnullnullnullnull

myAlphaNum has keys of type String, so passing an int to get ( myAlphaNum.get(j) ) will always return null. myAlphaNum具有类型为String的键,因此将int传递给getmyAlphaNum.get(j) )将始终返回null。

There are several ways to iterate over the values (or keys or entries) of the map. 有几种方法可以迭代映射的值(或键或条目)。

For example (assuming you only care about the values) : 例如(假设您只关心值):

for (String value : myAlphaNum.values())
    arrayList.add(value);
 // create HashMap from String
        StringBuilder alphaNum = new StringBuilder("1A2B3C4D5E6F7G8H9I");

        Map<String, String> myAlphaNum = new HashMap<String, String>(9);

        // for loop puts key and values in map, taken from String
        for (int i = 0; i < alphaNum.length();)
        {
            myAlphaNum.put(alphaNum.substring(i, ++i), alphaNum.substring(i, ++i));
        }

        System.out.println(myAlphaNum);
        if (myAlphaNum.containsKey(1))
            System.out.println("Key is there.");
        else
            System.out.println("Key is null.");

        // create ArrayList, add values to it using map keys
        ArrayList<String> arrayList = new ArrayList<String>();

        // for loop gets the "number" keys from HashMap to get the "letter" values
        for (int j = 1; j <= myAlphaNum.size(); j++)
            arrayList.add(myAlphaNum.get(j+""));


        System.out.print("Values taken from the hashmap keys: ");
        for (String list : arrayList)
            System.out.print(list);

You can try the above code. 您可以尝试上面的代码。 You have used string key bit while retriving Integer so it wont return anything. 您在检索Integer时使用了字符串键位,因此它不会返回任何内容。

You are using containsKey/get with an Integer as parameter, while your map keys are defined as String. 您将containsKey / get与Integer作为参数一起使用,而映射键被定义为String。 That's why you got null. 这就是为什么你没有null的原因。

I would recommend to use a Map<Integer, String> myAlphaNum = new HashMap<Integer, String>(9); 我建议使用Map<Integer, String> myAlphaNum = new HashMap<Integer, String>(9); and in your loop myAlphaNum.put(Integer.parseInt(alphaNum.substring(i, ++i)), alphaNum.substring(i, ++i)); 并且在您的循环中myAlphaNum.put(Integer.parseInt(alphaNum.substring(i, ++i)), alphaNum.substring(i, ++i)); . Then you'll get your desired output. 然后,您将获得所需的输出。

Also you could the ArrayList constructor that takes a Collection as parameter (or just sysout myAlphaNum.values() ) directly. 您也可以直接将Collection作为参数的ArrayList构造函数(或仅将sysout myAlphaNum.values()用作参数)。

// create ArrayList, add values to it using map keys
ArrayList<String> arrayList = new ArrayList<String>(myAlphaNum.values());
System.out.println(arrayList); //[A, B, C, D, E, F, G, H, I]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM