• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

help! a vector of hashtables???

I want to use this structure for something but I am really confused on how it will work, can someone give me snippets of code to illustrate of to access parts of this thing ...

something like ...

Vector v = new Vector();

Hashtable a = new Hashtable();
a.put("a",1);

Hashtable b = new Hashtable();
a.put("b",1);

v.add(a);
v.add(b);

is there a better way to do this?
0
polkadot
Asked:
polkadot
  • 3
  • 3
  • 2
  • +2
5 Solutions
 
MogalManicCommented:
If you want a List of Sets, then use the HashSet class instead of the HashTable.  I would also put the functionality in the context of the class by building a add() function:

HashSet set1=new HashSet()
set1.put("a");
set1.put("b");

HashSet set2=new HashSet()
set1.put("a");
set1.put("b");

addSetToList(set1);
addSetToList(set2);

...
public void addSetToList(Set theSet)
{
    theList.add(theSet);
}
...
public Set getSet(int index)
{
   return (Set) theList.get(index);
}
0
 
CEHJCommented:
>>but I am really confused on how it will work

This kind of containment structure is useful when you want to keep several lists. Suppose you want to keep several mail boxes, together with the mails sent to individuals in those mailboxes. Will will imagine that the messages are called messageX and the variables have been initiliazed. Using a HashMap will improve performance if synchronization is not required:

Vector mailFolders = new Vector();

HashMap inbox = new HashMap();
HashMap sentItems = new HashMap();

mailFolders.add(inbox);
mailFolders.add(sentItems);

inbox.put("alan@somewhere.com", message1);
inbox.put("bill@somewhere.com", message2);

sentItems.put("carol@somewhere.com", message3);
sentItems.put("david@somewhere.com", message4);

Of course, you may notice that this arrangement would only allow one message per person. Why, and what would you do to allow several?
0
 
CEHJCommented:
Ignore the following sentence:

>>This kind of containment structure is useful when you want to keep several lists.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RoonaanCommented:
To retrieve an item from the list you could do something like

//retrieve hashmap from vector.
HashMap a = (HashMap) v.get(0);
HashMap b = (HashMap) b.get(0);

but this is just simple casting.

I suppose you know how to work with hashmap

-r-
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> is there a better way to do this?

The code which you've written in the question seems to be ok by itself if all you want to do is add hash-tables to a Vector.

If you could tell 'why' you want to add Hash-tables to a Vector and give some more context of the problem, then we could perhaps throw some more light upon whether its the best way to do it or not.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you don't need to use the synchronization features of the Vector, then you could instead use an ArrayList. That would be faster.
0
 
polkadotAuthor Commented:
Questions:

1. What do you mean by synchronization features?

2. I don't know how to use HashMap, your saying to use HashMap instead of HashTable?


... I'm trying to represent an "element" as ("a", 1)
... An "thing" will hold a collection of "elements"  ->hence the hashtable
... There will be a collection "things" ->hence the vecor

I don't want to create a class for the elemnt, i just want to simply want to pair an string with an int

thanks in advance
0
 
MogalManicCommented:
>Questions:
>
>1. What do you mean by synchronization features?
A hashtable has its methods synchronized so that multiple threads cannot modify it at the same time.  Using one may effect performance

>2. I don't know how to use HashMap, your saying to use HashMap instead of HashTable?
Both HashMap and Hashtable impliment the same Map interface, so they both have the same API.  The difference is that tha Hashtable is synchronized and HashMap is not.

>... I'm trying to represent an "element" as ("a", 1)
You can only put Objects in a Hashtable or HashMap.  If you want to associate "a" with the number 1:
    myMap.put("a", new Integer(1));
If you are interested in just the keys (i.e. they ALL are mapped to 1), then use the HashSet.  This class is backed by a hashTable, but only stores distinct keys.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> A hashtable has its methods synchronized so that multiple threads cannot modify it at the same time

Actually, I was talking about synchronization at the Vector ;-) not the Hash-table. What I meant was that if you don't have a case where multiple threads would be accessing the collection, then instead of the Vector, you could use an ArrayList.
0
 
CEHJCommented:
8-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now