This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Please Help: Putting Vector objects into HashMap  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Mark Yadav
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Snigdha Solanki
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Trying exercises from Java books is a good idea. You can also take up a dummy java project for pratice.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!