Solved

help! a vector of hashtables???

Posted on 2004-09-18
10
347 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

696 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