我有String
(S)的List
,但我想它轉換成一個Map<String, Boolean>
從List<String>
,使所有設置爲真boolean
映射。我有以下代碼。創建映射出字符串(S)與流的列表
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}
}
}
有沒有辦法減少這種使用流?
你爲什麼需要這個? – shmosel