BlueShark
BlueShark

Reputation: 547

How to retrive and store the individual element of a map in java

I have a map object where I have a list of values. Now I want to iterate the values and store individual object items to individual variables so that I can insert the variables into database. I am successful to iterate the values from the map. But now I want to store individual values to individual elements. Here is my code...

for (int i = 1; i <= 3; i++) {
    Map rec = d.getRecord();
    for (int j = 0; j < rec.size(); j++) {
        Collection c = rec.values();
        Iterator itr = c.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
        System.out.println();
    }
}

the output is

Fri Jan 18 23:45:07 IST 2013
7118492
Mon Dec 31 14:25:43 IST 2012
+919748675483
+919748183326

Fri Jan 18 23:45:07 IST 2013
7118492
Mon Dec 31 14:25:43 IST 2012
+919748675483
+919748183326

Now I want to store Fri Jan 18 23:45:07 IST 2013 to some variable, 7118492 to some variable and so on.

Upvotes: 0

Views: 390

Answers (2)

Morgan J
Morgan J

Reputation: 115

I'm not sure why you need to do this, as you can access each item from the map as you need.

But if you simply prefer a list of items, you could add the values to a list, and then use each item from the list as you require.

e.g.

List<Value> list = new ArrayList<Value>(rec.values());
for (String s : list) {
    // do something with s
}

Upvotes: 0

Devolus
Devolus

Reputation: 22084

To iterate over the map you can do this:

    Map<String, String>map = new HashMap<String, String>();
    for(Entry<String, String>entry : map.entrySet())
    {
        entry.getKey();
        entry.getValue();
    }

getKey() returns the keyvalue from your map, and getValue() returns the value which is associated to that key in your map. It is the same as if you would do value = map.get(key);

Upvotes: 2

Related Questions