Hashtable hw
How can I convert its values to:
ArrayList <Word> arr
thanks.
-
I find this rather amusing :)Andrei Fierbinteanu– Andrei Fierbinteanu2010年05月26日 17:57:18 +00:00Commented May 26, 2010 at 17:57
-
This always happens on easy questions. Personally I think it's classy to delete a duplicate answer you posted if it doesn't contribute anything more than an answer that was submitted before you, but people actually doing that is exceedingly rareMichael Mrozek– Michael Mrozek2010年05月26日 17:59:14 +00:00Commented May 26, 2010 at 17:59
-
@Michael, For me it is fun to race to be the first to answer, while still providing the best content.jjnguy– jjnguy2010年05月26日 18:00:49 +00:00Commented May 26, 2010 at 18:00
-
@Justin Me too, but if you lose the race there's no point leaving your answer around unless it provides extra information the others don't. There are verbatim answers posted on this one -- that adds nothing of valueMichael Mrozek– Michael Mrozek2010年05月26日 18:03:27 +00:00Commented May 26, 2010 at 18:03
-
@Michael I agree. But if I get an upvote or two, I'd like to keep that reputation around. I don't think it ads clutter (too much) if they are all correct.jjnguy– jjnguy2010年05月26日 18:05:17 +00:00Commented May 26, 2010 at 18:05
4 Answers 4
Use the ArrayList
constructor that takes a collection.
ArrayList<Word> arr = new ArrayList<Word>(hw.values());
Then every value that was in the HashTable
will be in the new ArrayList
.
You can find documentation about the constructor in the javadocs.
Comments
ArrayList<Word> arr = new ArrayList<Word>( hw.values() );
Comments
Also you can use
ArrayList<Word> arr = Collections.list(hw.keys());
for keys as ArrayList
1 Comment
use
hw.values();
it will simply return the Collection (like a List) of Word
objects.
from javadocs
values
public Collection values()
Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.