Solved

help! a vector of hashtables???

Posted on 2004-09-18
10
340 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
  • 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
 
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:mayankeagle
mayankeagle 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 52
maven java path setting 5 50
what is a "java.lang.System Property"   ? 20 39
eclipse java  build path 6 21
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now