File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes retrive Vector from HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "retrive Vector from HashMap" Watch "retrive Vector from HashMap" New topic
Author

retrive Vector from HashMap

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
public HashMap setHashMapValues() {
HashMap hmap = new HashMap();
HashMap hmap1 = new HashMap();

String hval = "test";
hamp.put(hval,hamp1);

Vector vec = new Vector();
vec.add("vone");
vec.add("vtwo");
vec.add("vthree");

String val="hai";
hamp1.put(val,vec)
return hmap;
}

I called this setHashMapValues() in another class, then i did
Iterator iterator = hmap.values().iterator();
String Result = "";
while (iterator.hasNext()) {
Result = (iterator.next()).toString();
System.out.println("Result "+Result);

}

I need only vector object within this??? How can i retrive only vector within this HashMap??
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
Originally posted by kri shan:
public HashMap setHashMapValues() {
HashMap hmap = new HashMap();
HashMap hmap1 = new HashMap();

String hval = "test";
hamp.put(hval,hamp1);

Vector vec = new Vector();
vec.add("vone");
vec.add("vtwo");
vec.add("vthree");

String val="hai";
hamp1.put(val,vec)
return hmap;
}

I called this setHashMapValues() in another class, then i did
Iterator iterator = hmap.values().iterator();
String Result = "";
while (iterator.hasNext()) {
Result = (iterator.next()).toString();
System.out.println("Result "+Result);

}

I need only vector object within this??? How can i retrive only vector within this HashMap??



[ April 15, 2005: Message edited by: Sripathi Krishnamurthy ]
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
My class looks like this... when i try to run this class i am getting... Exception in thread "main" java.lang.ClassCastException: java.util.HashMap
at hmapvec1.main(hmapvec1.java:17)
i need only vector out of this HashMap....


import java.util.*;

public class hmapvec1 {
public static void main(String[] args) {
HashMap mp = new HashMap();
HashMap mp1 = new HashMap();
Vector vec=new Vector();
vec.add("11111");
vec.add("22222");
vec.add("33333");
mp1.put("aaa",vec);
mp.put("hai",mp1);

Iterator itr=mp.values().iterator();
Vector vector=null;
while (itr.hasNext()) {
vector=(Vector)itr.next();
}
System.out.println(vector);
}
}
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Thats because mp is a Map of Maps, not Vectors.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
Originally posted by kri shan:
My class looks like this... when i try to run this class i am getting... Exception in thread "main" java.lang.ClassCastException: java.util.HashMap
at hmapvec1.main(hmapvec1.java:17)
i need only vector out of this HashMap....


import java.util.*;

public class hmapvec1 {
public static void main(String[] args) {
HashMap mp = new HashMap();
HashMap mp1 = new HashMap();
Vector vec=new Vector();
vec.add("11111");
vec.add("22222");
vec.add("33333");
mp1.put("aaa",vec);
mp.put("hai",mp1);

Iterator itr=mp.values().iterator();
Vector vector=null;
while (itr.hasNext()) {
vector=(Vector)itr.next();
}
System.out.println(vector);
}
}


Paul is right. For the above case you should use,


Understand the concept first. Read more about Classcastexception.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
Hi Sripathi, when i run the above code, i got this result
{aaa=[11111, 22222, 33333]}.
But is it possible to take only vector elements like this [11111, 22222, 33333} ?

Here you used one more HashMap, is it possible to avoid?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Here you used one more HashMap, is it possible to avoid?

He has only use one more Map because that is how you have written your code. If you don't want it to behave that way, put your Vector directly into mp, rather then into mp1 - which you then put into mp.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
public class hmapvec {
public static void main(String[] args) {
HashMap mp = new HashMap();

for(int y=0;y<=2;y++) {
HashMap mp1 = new HashMap();
Vector vec=new Vector();
vec.add("11111"+y);
vec.add("22222"+y);
vec.add("33333"+y);
mp1.put("aaa"+y,vec);
mp.put("hai"+y,mp1);
}

System.out.println("**mp**"+mp);

Iterator itr=mp.values().iterator();
HashMap newMap=null;
while (itr.hasNext()) {
newMap=(HashMap)itr.next();
}

System.out.println("**newMap**"+newMap);
}
}

It prints the values like this:
**mp**{hai0={aaa0=[111110, 222220, 333330]}, hai2={aaa2=[111112, 222222, 333332]}, hai1={aaa1=[111111, 222221, 333331]}}
**newMap**{aaa1=[111111, 222221, 333331]}
newMap prints only last value.
But i need all three values like this
** {aaa0=[111110, 222220, 333330]} {aaa1=[111111, 222221, 333331]} {aaa2=[111112, 222222, 333332]}.
How can i iterate HashMap??
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by kri shan:
newMap prints only last value.
You have one outer HashMap. It contains three inner HashMaps. Each inner HashMap contains a single Vector.

Then, when iterating and displaying the data, you print the entire outer HashMap and then iterate the inner HashMaps. But because you print newMap after the while loop, it only prints the last one it held. Move the println inside the while loop.

You might also want to switch to ArrayList instead of Vector if you don't need synchronization. ArrayList is to Vector like HashMap is to Hashtable.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
import java.util.*;

public class hmapvec {
public static void main(String[] args) {
HashMap mp = new HashMap();

for(int y=0;y<=2;y++) {
HashMap mp1 = new HashMap();
Vector vec=new Vector();
vec.add("11111"+y);
vec.add("22222"+y);
vec.add("33333"+y);
mp1.put("aaa"+y,vec);
mp.put("hai"+y,mp1);
}

System.out.println("**mp**"+mp);

Iterator itr=mp.values().iterator();
HashMap newMap=null;
while (itr.hasNext()) {
newMap=(HashMap)itr.next();
System.out.println("**newMap**"+newMap);
}

Iterator itr1=newMap.values().iterator();
Vector vec1 = null;
while (itr1.hasNext()) {
vec1 = (Vector)itr1.next();
System.out.println("inside vec1**"+vec1);
}
}
}

My newMap returns
**newMap**{aaa0=[111110, 222220, 333330]}
**newMap**{aaa2=[111112, 222222, 333332]}
**newMap**{aaa1=[111111, 222221, 333331]}

But my vector returns only lst one..
inside vec1**[111111, 222221, 333331]

How can i get all three, thru iteration ?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by kri shan:
How can i get all three, thru iteration ?
Very similar to my previous answer: you must pull up the second iteration loop into the first loop. You iterate the outer map to get the inner maps, and you iterate each inner map to get the vectors.

If you need to collect the vectors into a single collection, do so in the iteration. Otherwise your code will print all the vectors simply by moving the closing brace of the first while loop immediately after the closing brace of the second while loop so that they're nested.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1382
Hi David, i am not getting this meaning. If you need to collect the vectors into a single collection, do so in the iteration. Otherwise your code will print all the vectors simply by moving the closing brace of the first while loop immediately after the closing brace of the second while loop so that they're nested.
I tried your way...
while (itr.hasNext()) {
newMap=(HashMap)itr.next();
Iterator itr1=newMap.values().iterator();
while (itr1.hasNext()) {
vec1 = (Vector)itr1.next();

System.out.println("inside vec1**"+vec1);
}
System.out.println("inside second while vec1**"+vec1);
System.out.println("**newMap**"+newMap);
}
System.out.println("**outside vec1**"+vec1);
i got the following result:
inside vec1**[111110, 222220, 333330]
inside second while vec1**[111110, 222220, 333330]
**newMap**{aaa0=[111110, 222220, 333330]}
inside vec1**[111112, 222222, 333332]
inside second while vec1**[111112, 222222, 333332]
**newMap**{aaa2=[111112, 222222, 333332]}
inside vec1**[111111, 222221, 333331]
inside second while vec1**[111111, 222221, 333331]
**newMap**{aaa1=[111111, 222221, 333331]}
**outside vec1**[111111, 222221, 333331]
But i need like this
[111110, 222220, 333330, 111112, 222222, 333332, 111111, 222221, 333331]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by kri shan:
But i need like this
[111110, 222220, 333330, 111112, 222222, 333332, 111111, 222221, 333331]
That's what I meant by collecting the Vectors, or in this case the values inside the Vectors, into a single Collection, for example a new Vector.

First, just before you start the outer while loop, create a new Vector (I'll call it vecAll). Next, inside the inner loop, after you get the Vector from the inner Map iterator, you can add all of vec1's elements to vecAll using Collection.addAll(Collection). Note that Vector is an implementation of Collection.

Check out the JavaDocs for Vector and Collection for some more enlightenment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrive Vector from HashMap