sarah
sarah

Reputation: 2397

Iterate over a list and put data in hashmap

I am having a list where i need to loop over it and put its data in 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 am doing in 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 com.bean.xyz@23032bc[id=1] format, i need the exact data, how will i do this?

Upvotes: 2

Views: 7984

Answers (5)

Chris J
Chris J

Reputation: 9262

You probably want to adjust how you are populating your HashMap as follows:

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 take advantage of Java's Generics capability in its Collection objects such as HashMap.

The reason you see com.bean.xyz@23032bc[id=1] is that your bean probably does not have a toString method defined for it. In the absences of a toString method, the Object.toString method is used which returns the address where your object is located in the JVM heap. If you want to see something else, you will have to override the toString method in your object xyz.

public class xyz {
  ...
  public String toString () {
    return "hello world"; //put what you want to see here
  }
  ...
}

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 384006

A Map is a mapping from keys to values. You need to define, for each element of the list, what should be the key, and what should be the value.

Your original code:

for(int i=0;i<list.size();i++) {
    HashMap hMap=new HashMap();
    hMap.put("Data", list);
}

This effectively maps the key "Data" to the value list, repeating this mapping several times, but you only have entry.

Here's an example of taking a List<String>, and constructing a map from a letter to a string starting from 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 "abc" is "lost" in the map. That's because you can only map one key to one value. On the other hand, you can have a Map<Character,Set<String>>, that is a map from each key to a set of values. This is effectively what a multimap is, and Guava has an implementation.

Related questions

Upvotes: 2

Midhat
Midhat

Reputation: 17840

I see multiple problems here

  1. You will only get the last list item in the hashmap. Because you create a new hashmap on every iteration, and its reference is lost on the next iteration.

  2. You need to implement the toString method in com.bean.xyz class to output the desired data member

Upvotes: 2

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

First of all, you are aware of the fact that your hash table will only contain one value, right? If you want to add all the items from list to one hash table, you must call HashMap hMap=new HashMap(); outside the for loop.

To output the right data, implement toString() for your type or cast it to the correct type.

Upvotes: 0

jweber
jweber

Reputation: 589

I don't know which type of objects you are trying to print. But if it's one of your one objects you are trying to prints, you must make a toString method inside your class that overrides the one from the object class.

public string toString()
{
    return "";
}

In the return statement you could return some of the objects attributes.

Upvotes: 0

Related Questions