[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

help! a vector of hashtables???

Posted on 2004-09-18
10
Medium Priority
?
349 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 420 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 360 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 360 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 360 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 420 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

649 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