Reputation: 400
I have a list gotitems
.
ArrayList<String> gotitems = new ArrayList<String>();
i need to put that list in a hashmap called map
.
Map<String,String> map = new HashMap<String,String>();
i had tried this :
for(String s:gotitems){
map.put("a",s);
}
gotitems contains :
First
Second
Third
But the output of :
System.out.println(map.values());
gives :
Third
Third
Third
i had even tried this :
for(String s:gotitems){
for(int j=0;j<gotitems.size();j++){
map.put("a"+j,s);
}
}
but this is also not working.
What am i doing wrong here ?
Upvotes: 0
Views: 13120
Reputation: 113
I have reproduce your codes. The problem is that you are assigning the same key to different value. This should work.
import java.util.*;
public class testCollection{
public static void main(String[] args){
ArrayList<String> gotitems = new ArrayList<String>();
gotitems.add("First");
gotitems.add("Second");
gotitems.add("Third");
Map<String,String> map = new HashMap<String,String>();
String x = "a";
int i = 1;
for(String s:gotitems){
map.put(x+i,s);
i++;
}
System.out.println(map);
}
}
Upvotes: 0
Reputation: 7692
This is wrong:
for(String s:gotitems){
map.put("a",s);
}
Since you are using "a"
common key for all values, last inserted key-value pair would be preserved, all previous ones would be overridden.
This is also not correct:
for(String s:gotitems){
for(int j=0;j<gotitems.size();j++){
map.put("a"+j,s);
}
}
you are putting n*n times into map, though you want only n (gotitems.size()
) items into map.
First decide on key which you want to use in map, copying List
into Map
one approach could be use index as key
:
for(int j=0;j<gotitems.size();j++){
map.put("KEY-"+j,gotitems.get(j));
}
Output should be:
KEY-0 First
KEY-1 Second
KEY-2 Third
Upvotes: 0
Reputation: 878
Please note that in a map, a key can point to at most one value. In your case, you are doing the following mappings:
"a" -> "one"
then you overwrite it as
"a" -> "two"
then you overwrite it as
"a" -> "three"
remember: a key can point to at most one value. However, a value can be pointed at by multiple keys.
Upvotes: 0
Reputation: 234715
When you write
for(String s:gotitems){
map.put("a",s);
}
you will trash any existing entry in the map held against the key "a". So after your iteration, your map will contain just one entry corresponding to the last iterated value in gotitems
.
To use a map effectively you need to consider what your keys will be. Then use map.put(myKeyForThisItem, s)
instead. If you don't have an effective scheme for the keys then using a map is pointless as one tends to use the keys to extract the corresponding values.
As for your second approach, it would be helpful if you could define "it is not working" a little clearer: perhaps iterate through the map and print the keys and values.
Upvotes: 0
Reputation: 48404
You're putting all your items in the Map
with the same key: "a".
You should have a unique String
key for each value.
For instance:
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
Map<String, String> map = new LinkedHashMap<String, String>();
for (String s: list) {
map.put(s, s);
}
System.out.println(map);
Output:
{one=one, two=two, three=three}
Note the LinkedHashMap
here: it maintains the order in which you put your key/value pairs.
Edit Of course if your List
does not have unique values, moving its values as keys to a Map
will overwrite some of the Map
's values. In that case you want to ensure your List
has unique keys first, or maybe use a Map<Integer, String>
with the index of the List
's value as key to the Map
, and the actual List
value as value to the Map
.
Upvotes: 0
Reputation: 1047
You are trying to put three Strings in the map under the same key "a"
. Try to use unique keys for your values.
Upvotes: 0
Reputation: 121998
As per Map put(K,V) method docs
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value.
You are ovverriding the key each time here .
for(String s:gotitems){
map.put("a",s);
}
change the key each time and try like
for(String s:gotitems){
map.put(s,s);
}
Upvotes: 2
Reputation: 68715
This is because you are putting all the items in the map against the same key "a"
map.put("a");
You need to store each element against a unique key so add something like this:
int count = 0;
for(String s:gotitems){
map.put("a" + count,s);
count++;
}
Upvotes: 0