Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

retrive Vector from HashMap

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats because mp is a Map of Maps, not Vectors.
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic