ITERATOR AND HASHTABLE

please help me guys,

I don't know if I am doing this a long winded way or not, basically, I want to be able to store stuff like

( (a,1), (b,7), (f,8) ) etc, you guys get the picture.

So at the moment I have created a Hashtable to store this.

Now I want to be able to go through this whole hashtable, and pick up all the values (not the keys).

So what I have done is use a for loop like below:

for (Iterator i = myHash.values().iterator(); i.hasNext(); )

{

//etc

}

This just seems long winded to me. Surely there must be a better way guys, is there? maybe I need to use something else? Any help appreciated.

[793 byte] By [Zizoua] at [2007-11-27 2:37:46]
# 1

there are three methods :

1 - values() method :http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html#values()

which returns a Collection of the values

2 - elements :http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html#elements()

which returns an Enumeration of the values

3 - keys() and get(Object key): http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html#keys()

loop your keys set and get all values

java_2006a at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 2
You can use the for each syntax if you're on 5 or later:HashMap<String, Object> map;for (String s : map.values()) {}
hunter9000a at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 3
Please don't feed the troll. goldie is not interested in the answer, it justwants the attention. It must've started its preadolescent trolling act again.Jos
JosAHa at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 4
He is asking a legit question for once.I don't see any evidence of trolling yet so give him the beneft of the doubt.
maple_shafta at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 5

Hello!

If you are using java jdk 1.5 or higher you can use the following syntax:

<code>

Hashtable <String, Integer> ht = new Hashtable<String, Integer>();

ht.put("a", new Integer(1));

ht.put("b", new Integer(2));

ht.put("c", new Integer(3));

System.out.println(ht);

</code>

In java 1.5 there is a enchanced for loop

<code>

for(String a: ht.keySet()){

}

</code>

This foor loop is only useful when you want to iterate through the elements, if you want to modify the values or the keys, it's useful.

Best regards

skalstera at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 6

> He is asking a legit question for once.

>

> I don't see any evidence of trolling yet so give him the beneft of the doubt.

Nope, the little twit has done that before multiple times; I for one don't

want that little twit here. It's just a preadolescent troll.

Jos

JosAHa at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 7

> > He is asking a legit question for once.

> >

> > I don't see any evidence of trolling yet so give

> him the beneft of the doubt.

>

> Nope, the little twit has done that before multiple

> times; I for one don't

> want that little twit here. It's just a preadolescent

> troll.

>

> Jos

You know, I did wonder why someone who's been around so long would ask such a relatively simple question...

hunter9000a at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 8

> > > He is asking a legit question for once.

> > >

> > > I don't see any evidence of trolling yet so give

> > him the beneft of the doubt.

> >

> > Nope, the little twit has done that before

> multiple

> > times; I for one don't

> > want that little twit here. It's just a

> preadolescent

> > troll.

> >

> > Jos

>

> You know, I did wonder why someone who's been around

> so long would ask such a relatively simple question...

I am sorry Jos if you're still angry at my past behaviour. You know, despite the things I did, I never have and never will ask a question for trolling purposes. NEVER. And on that you have my word (not that it probably means a lot to you). Getting free help from someone is something I appreciate very much, and I would never waste their time.

I have been around, but I am not a Java developer byu career, I only write bits of code now and then when I need to. I don't know the best way to approach the problem I described, which is why I asked. If I offended someone, I am sorry.

Zizoua at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...
# 9
Just shut up little lying twit.Jos
JosAHa at 2007-7-12 2:58:17 > top of Java-index,Java Essentials,Java Programming...