Forums Register Login

Please Help: Putting Vector objects into HashMap

+Pie Number of slices to send: Send
Hi,

The problem I have is about passing one Collection object into another, such as passing a Vector object into a Hashmap. I've tried to do this in the two files included below: VectorToHash.java and VectorToHashTest.java.
The problem specification is: I put values into a Vector object and then passed it to a Hashmap and then I want to retieve those values out. I also want to associate each Vector object passed to the Hashmap with a Key object. I've made an attempt to do this below in the two .java files mensioned, but I'm stuck. Although the code does compile, I need help to correct it to work as specified. Could you help me correct my code, and help me understand what problem situations would require a Vector object to be passed to a Hashmap?

Many Thanks,
Mark Yadav.
VectorToHash.java
// VectorToHash.java - the purpose of theis class is to create methods
// that allow a Vector containing Strings to be added to a HashMap.
// The Test program: VectorToHashTest then returns the vector from
// the HashMap and then extracts the values from the vector.
import java.util.*;
public class VectorToHash {

// HashMap to store vector
HashMap book = new HashMap();
Vector v = new Vector();
String[] fruit = {"apple","banana","orange","tomato","pear"};
Integer[] id = new Integer[5];
// Method to add fruit to vector.
// A vector is then returned
public Vector addFruit(Vector v){
for(int i = 0; i < fruit.length; i++)
v.add(fruit[i]);
return v;
}
// method to add vector to HashMap. The retuned Vector
// from the previous method is added to the HashMap
public void addVector(Vector v, HashMap m){
for(int i = 0; i < 5; i++)
m.put(id[i], v);
}
}

VectorToHashTest.java
// VectorToHashTest.java Tests the VectorToHash class
import java.util.*;
class VectorToHashTest{
public static void main(String[] args){
// Vector object
Vector b = new Vector();
Vector c = new Vector();
// HashMap object
HashMap hp = new HashMap();
//
VectorToHash vth = new VectorToHash();
c = vth.addFruit(b);
// add vector to HashMap
vth.addVector(c,hp);
//output contents of HashMap
Set keys = hp.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext())
System.out.println((Integer)keyIter.next());
}
}
+Pie Number of slices to send: Send
Check out this code:


Output :
Size of set is5
[Ljava.lang.Integer;@680a59=[apple, banana, orange, tomato, pear]
[Ljava.lang.Integer;@7f5ea7=[apple, banana, orange, tomato, pear]
[Ljava.lang.Integer;@67ac19=[apple, banana, orange, tomato, pear]
[Ljava.lang.Integer;@53ba3d=[apple, banana, orange, tomato, pear]
[Ljava.lang.Integer;@53c015=[apple, banana, orange, tomato, pear]
Two places where I changed the code:
1.Integer[] id = new Integer[5];
This will create five elements of Integer oject with null value. So, the key for HashMap would be set as null.
m.put(id[i], v);
Changed it to:
m.put(new Integer(i),v);
2.Set keys = hp.keySet();
This will give set views of keys. In order to get set views of Collection(i.e., Vector) use hp.entrySet().
+Pie Number of slices to send: Send
Thanks so much Snigdia,
I'm not extremely expereinced in Java and I've been trying to expereiment with the language to get better skilled at it. Passing a Vector object into a HashMap was one way of expereinting. I've also tried to do the exercises out of various Java textbooks - would you recommend this approach? How did you increase your proficiency in Java?
Mark Yadav
+Pie Number of slices to send: Send
Hi Mark,
Trying exercises from Java books is a good idea. You can also take up a dummy java project for pratice.
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 16306 times.
Similar Threads
few queries with the program
Comparator giving compiler error
Generics <?>
using `instanceof`with an "object" parameter
How to write generic method for this method?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:59:54.