Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get HashTable values as Arraylist?

Hashtable hw

How can I convert its values to:

ArrayList <Word> arr

thanks.

like image 316
Radi Avatar asked May 26 '10 17:05

Radi


People also ask

How do you access values in an ArrayList?

An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.

Can we add HashMap to ArrayList?

You can convert HashMap keys into ArrayList or you can convert HashMap values into ArrayList or you can convert key-value pairs into ArrayList.

Why HashTable is faster than 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.

Can we get key from value in HashTable?

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.


2 Answers

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.

like image 55
jjnguy Avatar answered Sep 28 '22 03:09

jjnguy


ArrayList<Word> arr = new ArrayList<Word>( hw.values() );
like image 20
karoberts Avatar answered Sep 28 '22 04:09

karoberts