15

Hashtable hw

How can I convert its values to:

ArrayList <Word> arr

thanks.

Daniel DiPaolo
56.5k14 gold badges120 silver badges116 bronze badges
asked May 26, 2010 at 17:52
6
  • I find this rather amusing :) Commented 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 rare Commented 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. Commented 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 value Commented 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. Commented May 26, 2010 at 18:05

4 Answers 4

49

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.

answered May 26, 2010 at 17:54

Comments

10
ArrayList<Word> arr = new ArrayList<Word>( hw.values() );
answered May 26, 2010 at 17:54

Comments

9

Also you can use

ArrayList<Word> arr = Collections.list(hw.keys());

for keys as ArrayList

answered Jun 14, 2013 at 2:19

1 Comment

keys wanted thx! any idea why they return keys as enumeration and not collection?
0

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.

rogerdpack
67.5k40 gold badges288 silver badges408 bronze badges
answered May 26, 2010 at 17:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.