将一组字符串流式传输到 <String, ArrayList()> 的 Map 中
Stream a set of Strings into Map of <String, ArrayList()>
我有一组字符串(它是静态的),我想从中创建一个 Map<String, List<String>>
,其中列表被初始化为一个新的 ArrayList。类似于以下内容,但此代码不正确。
static Set<String> MY_TYPES = Set.of("type1", "type2", "type3");
Map<String, List<String> myMap = MY_TYPES.stream().map(t->() t, new ArrayList<>()).collect(Collectors.toMap());
我正在努力学习如何在我的代码中更好地使用流。我知道我可以对集合进行迭代。
编辑
@shmosel,你是对的。我想要相当于
Map.of("type1", new ArrayList<>(), "type2", new ArrayList<>(), "type3", new ArrayList());
听起来您正在寻找这个:
MY_TYPES.stream()
.collect(Collectors.toMap(s -> s, s -> new ArrayList<>()))
我有一组字符串(它是静态的),我想从中创建一个 Map<String, List<String>>
,其中列表被初始化为一个新的 ArrayList。类似于以下内容,但此代码不正确。
static Set<String> MY_TYPES = Set.of("type1", "type2", "type3");
Map<String, List<String> myMap = MY_TYPES.stream().map(t->() t, new ArrayList<>()).collect(Collectors.toMap());
我正在努力学习如何在我的代码中更好地使用流。我知道我可以对集合进行迭代。
编辑
@shmosel,你是对的。我想要相当于
Map.of("type1", new ArrayList<>(), "type2", new ArrayList<>(), "type3", new ArrayList());
听起来您正在寻找这个:
MY_TYPES.stream()
.collect(Collectors.toMap(s -> s, s -> new ArrayList<>()))