This example shows how to convert Hashtable to ArrayList in Java. This example also shows how to convert Hashtable keys, values, or entries to the ArrayList object.

## How to covert Hashtable to an ArrayList in Java?

The Hashtable is an implementation of the Map interface that maps keys to values while the ArrayList class in Java is an implementation of the List interface that is a dynamic array of elements. These two are different types of data structures and cannot be converted directly to each other.

However, we can convert Hashtable keys, values, or entries to an ArrayList as given below.

### How to convert Hashtable keys to an ArrayList?

The `keySet`

method of the Hashtable class returns a Set view of all the keys contained in the hashtable object. Once we get the set of keys, we can convert it to an object of the ArrayList class using the constructor as given below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); hashtable.put(4, "Four"); hashtable.put(5, "Five"); /* * Get all the keys using the keySet method * and convert it to ArrayList using the * constructor */ List<Integer> listKeys = new ArrayList<Integer>( hashtable.keySet() ); System.out.println("List of all keys: " + listKeys); |

Output

1 | List of all keys: [5, 4, 3, 2, 1] |

### How to convert Hashtable values to an ArrayList?

The Hashtable `values`

method returns a Collection view of all the values contained in the hash table object. Once we get the Collection object, we can then convert it to an ArrayList using the constructor as given below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); hashtable.put(4, "Four"); hashtable.put(5, "Five"); /* * Get all the values using the values method * and convert it to ArrayList using the * constructor */ List<String> listValues = new ArrayList<String>( hashtable.values() ); System.out.println("List of all values: " + listValues); |

Output

1 | List of all values: [Five, Four, Three, Two, One] |

### How to convert Hashtable entries to an ArrayList?

The Hashtable `entrySet`

method returns a Set view of all the entries or mappings contained in the hash table object. Once we get the entries, we can convert it to an ArrayList using the constructor as given below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); hashtable.put(4, "Four"); hashtable.put(5, "Five"); /* * Get all the entries using the entrySet method * and convert it to ArrayList using the * constructor */ List<Map.Entry<Integer, String>> listEntries = new ArrayList<Map.Entry<Integer, String>>( hashtable.entrySet() ); System.out.println("List of all entries: " + listEntries); |

Output

1 | List of all entries: [5=Five, 4=Four, 3=Three, 2=Two, 1=One] |

This example is a part of the Java Hashtable Tutorial with Examples.

Please let me know your views in the comments section below.

References:

Java 8 Hashtable Documentation

## Add Comment