Create a Map out of a List of String(s) with streams

Viewed 5564

I have a List of String(s), but I want to convert it into a Map<String, Boolean> from the List<String>, making all of the boolean mappings set to true. I have the following code.

import java.lang.*;
import java.util.*;
class Main {
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("ab");
    list.add("bc");
    list.add("cd");
    Map<String, Boolean> alphaToBoolMap = new HashMap<>();
    for (String item: list) {
      alphaToBoolMap.put(item, true);
    }
    //System.out.println(list); [ab, bc, cd]
    //System.out.println(alphaToBoolMap);  {ab=true, bc=true, cd=true}
  }
} 

Is there a way to reduce this using streams?

3 Answers
Related