Solved

help! a vector of hashtables???

Posted on 2004-09-18
10
346 Views
Last Modified: 2010-03-31
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
Comment
Question by:polkadot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 140 total points
ID: 12094301
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 120 total points
ID: 12094735
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12094878
Ignore the following sentence:

>>This kind of containment structure is useful when you want to keep several lists.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 120 total points
ID: 12095001
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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 120 total points
ID: 12095009
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 12095011
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
 

Author Comment

by:polkadot
ID: 12098549
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
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 140 total points
ID: 12098915
>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
 
LVL 30

Expert Comment

by:Mayank S
ID: 12099287
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12100354
8-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question