Hashtable hw
How can I convert its values to:
ArrayList <Word> arr
thanks.
An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.
You can convert HashMap keys into ArrayList or you can convert HashMap values into ArrayList or you can convert key-value pairs into ArrayList.
HashTable is a Collection of Key Value Pair. Each object in the HashTable is defined by a Key and Value. Generally the ArrayList is quicker than the HashTable to insert elements in some cases. But when you have to lookup for an element the HashTable (using the key to search) is faster than the ArrayList.
The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
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() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With