Iterate over a list and put data in a hashmap
I have a list where I need to loop over it and put its data into a hashmap. I am using this approach
for(int i=0;i<list.size();i++) {
HashMap hMap=new HashMap();
hMap.put("Data", list);
}
But when I need to read the value from hMap
, I do this way
Collection c = hMap.values();
Iterator itr = c.iterator();
while(itr.hasNext()) {
System.out.println("next val is--"+itr.next());
}
next vali is---
is printed in format com.bean.xyz@23032bc[id=1]
, I need exact data, how do I do it?
a source to share
I see several problems here.
-
You will only get the last element of the list in the hashmap. Since you are creating a new hashfile on each iteration and its reference is lost on the next iteration.
-
You need to implement the toString method in the com.bean.xyz class to output the desired item
a source to share
A Map
is a mapping from keys to values. You need to define for each element of the list what the key should be and what the value should be.
Source:
for(int i=0;i<list.size();i++) {
HashMap hMap=new HashMap();
hMap.put("Data", list);
}
This effectively maps the Data key to a value list
, repeating this mapping multiple times, but you only have a record .
Here is an example of taking List<String>
and building a map from letter to string, starting with that letter from the list.
List<String> list = Arrays.asList(
"abc", "def", "ghi", "ijk", "abracadabra"
);
Map<Character,String> map = new HashMap<Character,String>();
for (String s : list) {
map.put(s.charAt(0), s);
}
System.out.println(map); // prints "{g=ghi, d=def, a=abracadabra, i=ijk}"
System.out.println(map.get('i')); // prints "ijk"
System.out.println(map.containsKey('x')); // prints "false"
Note that it is "abc"
"lost" on the map. This is because you can only map one key per value. On the other hand, you can have Map<Character,Set<String>>
, that is, a map from each key to a set of values. This is actually what multimap is, and Guava has an implementation.
Related questions
- Java Hashmaps reference - some examples
- What is a raw type and why shouldn't we use it? - DO NOT use raw types in new code
a source to share
I don't know what type of objects you are trying to print. But if this is one of your objects that you are trying to print, you must make a toString method inside your class that overrides one of the object's class.
public string toString()
{
return "";
}
In the return statement, you can return some attributes of objects.
a source to share
First of all, you know that your hash table will only contain one value, right? If you want to add all of the elements from list
a single hash table, you must call HashMap hMap=new HashMap();
outside of the for loop.
To output the data you want, execute toString()
for your type, or cast it on the correct type.
a source to share
You probably want to customize how you populate your HashMap like this:
HashMap<String,xyz> hMap=new HashMap<String,xyz>();
for(int i=0;i<list.size();i++)
{
hMap.put("Data"+i, list);
}
Where xyz
is the object, com.bean.xyz, that you are working with. This approach allows you to leverage Java Generics capabilities in your Collection objects such as HashMap.
The reason you are seeing com.bean.xyz@23032bc [id = 1] is because your bean probably doesn't have a toString method defined for it. In the absence of a toString method, the Object.toString method is used, which returns the address where your object is on the JVM heap. If you want to see something else, you will have to override the toString method on the xyz object.
public class xyz {
...
public String toString () {
return "hello world"; //put what you want to see here
}
...
}
a source to share