This example shows how to convert LinkedHashMap to a List (ArrayList, LinkedList) in Java. This example also shows how to convert map keys, values, or entries to a List.
How to convert LinkedHashMap to List in Java (ArrayList or LinkedList)?
The LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements.
How to convert all keys of LinkedHashMap to a List?
You can convert all keys of the map object to a List using the keySet
method as given below. The keySet
method returns a Set view of all keys contained in the map object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; public class LinkedHashMapToListExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); /* * To convert LinkedHashMap keys to a List, * use the keySet method and ArrayList constructor */ List<Integer> listKeys = new ArrayList<Integer>( lhmap.keySet() ); System.out.println("List contains:"); for(Integer key : listKeys){ System.out.println(key); } } } |
Output
1 2 3 4 |
List contains: 1 2 3 |
How to convert all map values to a List?
You can convert all values of the LinkedHashMap to a List using the values
method. The values
method of the LinkedHashMap class returns a Collection view of all the values contained in the map object. You can then use this collection to convert it to a List object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); /* * Get all the values of LinkedHashMap using the * values method and then add all of them to a List */ List<String> listValues = new ArrayList<String>( lhmap.values() ); System.out.println("List contains:"); for(String value : listValues){ System.out.println(value); } |
Output
1 2 3 4 |
List contains: One Two Three |
How to convert all map entries to a List?
You can also convert all the entries (i.e. key-value pairs or mappings) to a List using the entrySet
method. The entrySet
method returns a Set view of all the entries contained in the LinkedHashMap object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); /* * Get all the entries of LinkedHashMap using the * entrySet method and then add all of them to a List */ List<Map.Entry<Integer, String>> listEntries = new ArrayList<Map.Entry<Integer, String>>( lhmap.entrySet() ); System.out.println("List contains:"); for(Map.Entry<Integer, String> entry : listEntries){ System.out.println(entry.getKey() + " => " + entry.getValue()); } |
Output
1 2 3 4 |
List contains: 1=>One 2=>Two 3=>Three |
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap