2

i am a newbie in Java (coming from JavaScript developing on Adobe LiveCycle) and facing the following problem:

I have a String array with several items. I want to put only the items with the value "a" to a HashMap. But instead of 3 "a" values in the HashMap i get 1 null value there. Why is that?

    String[] s = {"a", "a", "b", "a"};

    Map m = new HashMap();

    for (int i = 0; i < s.length; i++) {
        if (s[i].equals("a")) {
            m.put(i, s[i]);
        }
    }

    for (int i = 0; i < m.size(); i++) {
        System.out.println(m.get(i));
    }

    // Prints
    //a
    //a
    //null
3
  • You have a Map<Integer, String> where key is the index of the string in the array. You shouldn't iterate from 0 to map size, but over map's actual entries. Use for (Integer key : map.keySet()) Commented Oct 7, 2016 at 13:38
  • You put the items with their corresponding index in the Map, i.e. you have a Map with content {0=a, 1=a, 3=a}. Therefore, if you try to access the map with 2 in the m.get(...), you get a null since key 2 is not found in the map. On a sidenote: you are using raw types. You should bind the types of the Map and HashMap properly: Map<Integer, String> m = new HashMap<Integer, String>(); Commented Oct 7, 2016 at 13:39
  • m.get(i) Returns the value for keyi. there you have no index like an array Commented Oct 7, 2016 at 13:41

3 Answers 3

2

You are putting the items in the map with key 0, 1 and 3.

You are taking them out with key 0, 1, an 2.

Use:

    for (Object o : m.keySet()) {
        System.out.println(m.get(o));
    }

or - better:

    Map<Integer, String> m = new HashMap<>();

    ...

    for (Integer i : m.keySet()) {
        System.out.println(i + " -> " + m.get(i));
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You put the items with their corresponding index in array s in the Map, i.e. you have a Map with content {0=a, 1=a, 3=a}. Therefore if you try to access the map with key 2 (m.get(2)), you get a null since key 2 is not found in m.

Instead of using a for-loop over m's size, I recommend iteration over m's keySet() via a foreach-loop:

for (Object key : m.keySet()) {
    System.out.println("key: " + key + ", value: " + m.get(key));
}

On a sidenote: you are using raw types. You should bind the types of the Map and HashMap properly (see the Javadoc of Map for details): Map<Integer, String> m = new HashMap<Integer, String>();. With properly bound types, key in the for-loop can be of type int or Integer. I recommend type Integer to avoid unnecessary Auto(un)boxing.

Comments

1

Your code is working correctly, but you are accessing it not correctly.

String[] s = {"a", "a", "b", "a"};
for (int i = 0; i < s.length; i++) {
    if (s[i].equals("a")) {
        m.put(i, s[i]);
    }
}

This puts it like this

First iteration : m.put(0, "a");
Second iteration : m.put(1, "a");
Third iteration : "b" doest not equal "a" but still counts the index i up
Fourth iteration: m.put(3, "a");

Apart from the other answers you can still use your range based loop and access it with an Iterator

Iterator<String> it = m.values().iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.