Java 如何使用<;在hashmap中存储值;字符串,列表<;整数>&燃气轮机;
我有以下数组 我正在尝试将数组信息保存在hashmap中Java 如何使用<;在hashmap中存储值;字符串,列表<;整数>&燃气轮机;,java,hashmap,Java,Hashmap,我有以下数组 我正在尝试将数组信息保存在hashmap中 String[][] students = {{"Bobby", 87}, {"Charles", 100}, {"Eric", 64}, {"Charles", 22}}; Map<String, List<Integer>> map = new HashMap<>(); List<Integer> score1 = new
String[][] students = {{"Bobby", 87}, {"Charles", 100}, {"Eric", 64},
{"Charles", 22}};
Map<String, List<Integer>> map = new HashMap<>();
List<Integer> score1 = new ArrayList<>();
for(int i=0; i<students.length; i++) {
score1.add(students[i][1]);
map.put(students[i][0], score1);
}
实际产量:
{Charles=[87, 100, 64, 22], Eric=[87, 100, 64, 22], Bobby=[87, 100, 64, 22]}
如何执行此操作?您需要区分现有阵列和新阵列:
List<Integer> currScore = map.get(students[i][0])
if (currScore != null) {
currScore.add(students[i][1]);
} else {
List<Integer> newScore = new ArrayList<>();
newScore.add(students[i][1]);
map.put(students[i][0], newScore);
}
List currScore=map.get(学生[i][0])
如果(currScore!=null){
加上(学生[i][1]);
}否则{
List newScore=new ArrayList();
newScore.add(学生[i][1]);
map.put(学生[i][0],新闻核心);
}
还可以将变量名称更改为有意义的名称您可以检查原始代码为何无法在此处编译: 对代码进行多次修复后,这是一种正确的方法(遵循您的逻辑):
//值应该是这样的{“Bobby”,“87”},因为您将其声明为
//字符串数组(字符串[][]),这个{“Bobby”,87}是一个字符串和一个int
字符串[][]学生={{“Bobby”,“87”},{“Charles”,“100”},{“Eric”,“64”},{“Charles”,“22”};
//由于前面的注释,内部列表必须为字符串
Map Map=newhashmap();
//由于上一条注释而导致的字符串列表
表1;
for(int i=0;i
此处演示:由于您初始化了循环外部的列表并将其用于所有附录,因此
hashmap
中的所有条目都引用了相同的实例。对于学生中的每个条目,您需要检查是否已经有该学生的列表。如果是这样,请检索该特定列表并附加到它。如果没有,请创建一个新列表,然后追加。for循环中的代码如下所示:
String name = students[i][0];
List<Integer> scores = map.get(name);
if (scores == null) {
scores = new ArrayList<>();
}
scores.add(students[i][1]);
map.put(name, scores);
String name=students[i][0];
列表分数=map.get(名称);
如果(分数==null){
分数=新的ArrayList();
}
分数。添加(学生[i][1]);
地图。放置(姓名、分数);
使用java-8,您可以在一行中使用以下所有内容:
Map<String, List<Integer>> collect1 =
Arrays.stream(students).collect(Collectors.groupingBy(arr -> arr[0],
Collectors.mapping(arr -> Integer.parseInt(arr[1]), Collectors.toList())));
Map collect1=
Arrays.stream(students).collect(collector.groupingBy(arr->arr[0]),
Collectors.mapping(arr->Integer.parseInt(arr[1]),Collectors.toList());
在这里,我们按照第0个索引对学生的姓名进行分组,第1个索引将保留学生的分数。String[]]学生={{{“Bobby”,“87”},{“Charles”,“100”},{“Eric”,“64”},{“Charles”,“22”};
String[][] students = { { "Bobby", "87" }, { "Charles", "100" }, { "Eric", "64" }, { "Charles", "22" } };
Map<String, List<Integer>> map = new HashMap<>();
Stream.of(students).forEach(student -> map.computeIfAbsent(student[0], s -> new ArrayList<>()).add(Integer.parseInt(student[1])));
Map Map=newhashmap();
Stream.of(students).forEach(student->map.computeifassent(student[0],s->newarraylist()).add(Integer.parseInt(student[1]);
Map<String, List<Integer>> collect1 =
Arrays.stream(students).collect(Collectors.groupingBy(arr -> arr[0],
Collectors.mapping(arr -> Integer.parseInt(arr[1]), Collectors.toList())));
String[][] students = { { "Bobby", "87" }, { "Charles", "100" }, { "Eric", "64" }, { "Charles", "22" } };
Map<String, List<Integer>> map = new HashMap<>();
Stream.of(students).forEach(student -> map.computeIfAbsent(student[0], s -> new ArrayList<>()).add(Integer.parseInt(student[1])));