是否可以通过 Stream API 从 HashMap 生成 String[][]?
我尝试得到这样的东西:
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3"); =>
=> String[][] 2dArray => [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]
我用简单的java做了同样的事情:
Set<Map.Entry<String, String>> entrySet = map.entrySet();
String[][] result = new String[3][2];
int i = 0;
for (Map.Entry<String,String> entry : entrySet){
result[i][0] = entry.getKey();
result[i][1] = entry.getValue();
++i;
}
慕后森
相关分类