简体   繁体   中英

how to get HashTable values as Arraylist?

Hashtable hw

How can I convert its values to:

ArrayList <Word> arr

thanks.

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 .

ArrayList<Word> arr = new ArrayList<Word>( hw.values() );

Also you can use

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

for keys as ArrayList

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM