Convert Keys of Map to List in Java

1. Introduction

Converting the keys of a Map to a List is a common operation in Java when you need to process or manipulate the keys independently from their values. This operation is particularly useful in scenarios where you need to sort keys, filter them, or perform any collection-based operations that are not directly supported by the Map interface. Java's Collections Framework along with its Stream API provides efficient ways to perform this conversion. This blog post will explore how to convert Map keys to a List in Java.

2. Program Steps

1. Create a Map and populate it with some key-value pairs.

2. Convert the Map's keySet to a List using the ArrayList constructor.

3. Convert the Map's keySet to a List using Java Stream API.

4. Display the original Map and the Lists containing the keys.

3. Code Program

import java.util.*;
import java.util.stream.Collectors;

public class MapKeysToList {
    public static void main(String[] args) {
        // Creating and populating a Map
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Apple");
        map.put(2, "Banana");
        map.put(3, "Cherry");

        // Method 1: Converting Map keys to List using the ArrayList constructor
        List<Integer> keyList1 = new ArrayList<>(map.keySet());

        // Method 2: Converting Map keys to List using Java Stream API
        List<Integer> keyList2 = map.keySet().stream().collect(Collectors.toList());

        // Displaying the original Map
        System.out.println("Original Map: " + map);

        // Displaying the Lists containing the keys
        System.out.println("Key List using ArrayList constructor: " + keyList1);
        System.out.println("Key List using Stream API: " + keyList2);
    }
}

Output:

Original Map: {1=Apple, 2=Banana, 3=Cherry}
Key List using ArrayList constructor: [1, 2, 3]
Key List using Stream API: [1, 2, 3]

Explanation:

1. The program begins by importing necessary classes from the java.util package for Map and List implementations and the java.util.stream.Collectors class for collecting stream results.

2. A HashMap named map is created and populated with integer keys and String values representing fruit names.

3. To convert the keys of the map to a list, the first method demonstrated uses the ArrayList constructor, passing map.keySet() as an argument. This constructor creates a new ArrayList containing all elements in the specified collection, which in this case are the keys of the map.

4. The second method leverages the Stream API. The keySet().stream() method call converts the set of keys into a Stream, and the collect(Collectors.toList()) terminal operation collects the elements of the stream into a new List.

5. The original map and the Lists created using both methods are then printed to the console, illustrating that the keys have been successfully extracted and stored in Lists.

6. These examples showcase two straightforward ways to convert the keys of a map to a list in Java, highlighting the versatility of Java's collections and Stream API.

Comments