树图中重复键的值会发生什么变化
What happens to the values of duplicate keys in a tree map
我有一个文本文件,它被读入树状图。文本文件包含学生列表及其分数。
Harry 10
Sam 8
John 7
Harry 8
Sam 9
我试过写这段代码,它读取一个文件,将它存储在树图中并显示在控制台上。
Scanner scanner = new Scanner(new FileReader("marks.txt"));
TreeMap<String, Integer> students = new TreeMap<String, Integer>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\s+");
students.put(columns[0], Integer.parseInt(columns[1]));
}
System.out.println("Alpha Order");
// Iterate over TreeMap
for (String key : students.keySet()) {
System.out.println(key + " :: " + students.get(key));
}
执行代码后我在控制台上得到的输出是
Alpha Order
Harry :: 10
John :: 7
Sam :: 9
我需要其他值进行进一步计算。是否有可能像
这样的输出
Harry :: 10 8
John :: 7
Sam :: 9 8
地图正是这样做的;它从一个键映射到一个值。
因此,当您读取 Harry 的第二个输入时,它会替换之前的值。
如果您需要映射到多个值,您可以将映射设置为从名称到整数集合的映射。
例如:
Map<String, List<Integer>> map = new TreeMap<>();
//...
if(map.containsKey(name) == false)
map.put(name, new LinkedList<Integer>());
map.get(name).add(score);
What happens to the values of duplicate keys in a tree map
重复键的旧值被替换。如果 Map
发现该键已经存在,则它会替换该值。例如
"Harry" => 10
变成
"Harry" => 8
is it possible to have an output like
是的,但您需要维护一个 List<Integer>
作为您的值。
与其盲目地 put
设置 key/value 对,不如使用 containsKey
测试密钥是否已经存在。如果键已经存在,get
列表和 add
它的值。如果键不存在,则创建一个新的 List
,将值添加到它,并将 put
和 key/value 放入 Map
.
例如
"Harry" => [10]
到
"Harry" => [10, 8]
正如其他人指出的那样,值将被替换,因为 Map 只允许一个键 -> 值映射。
将多个值映射到一个键的最简单方法是使用多(值)映射。为此,我更喜欢 Guava:
Scanner scanner = new Scanner(new FileReader("marks.txt"));
TreeMultimap<String, Integer> students = TreeMultimap.create();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\s+");
students.put(columns[0], Integer.parseInt(columns[1]));
}
System.out.println("Alpha Order");
// Iterate over TreeMap
for (String key : students.keySet()) {
System.out.println(key + " :: " + String.join(", ", students.get(key)));
}
但是如果您不能添加依赖项,您可以使用 TreeMap<String, TreeSet<Integer>>
:
Scanner scanner = new Scanner(new FileReader("marks.txt"));
TreeMap<String, TreeSet<Integer>> students = new TreeMap<>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\s+");
String student = columns[0];
TreeSet<Integer> set;
if(students.containsKey(student)) {
// A set already exists in the map for this student,
// append to it
set = students.get(student);
} else {
// No set exists in the map for this student,
// create a new one and put it in the map
set = new TreeSet<>();
students.put(student, set);
}
set.add(Integer.parseInt(columns[1]));
}
System.out.println("Alpha Order");
// Iterate over TreeMap
for (String key : students.keySet()) {
System.out.println(key + " :: " + String.join(", ", students.get(key)));
}
请注意,这两个使用集合,因此不允许重复的键 -> 值映射。 IE。你不能得到这个输出:
Harry :: 10 8 8
John :: 7 7
Sam :: 9 9 8
后一种解决方案很容易实现,只需将 TreeSet
替换为 ArrayList
。
我有一个文本文件,它被读入树状图。文本文件包含学生列表及其分数。
Harry 10
Sam 8
John 7
Harry 8
Sam 9
我试过写这段代码,它读取一个文件,将它存储在树图中并显示在控制台上。
Scanner scanner = new Scanner(new FileReader("marks.txt"));
TreeMap<String, Integer> students = new TreeMap<String, Integer>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\s+");
students.put(columns[0], Integer.parseInt(columns[1]));
}
System.out.println("Alpha Order");
// Iterate over TreeMap
for (String key : students.keySet()) {
System.out.println(key + " :: " + students.get(key));
}
执行代码后我在控制台上得到的输出是
Alpha Order
Harry :: 10
John :: 7
Sam :: 9
我需要其他值进行进一步计算。是否有可能像
这样的输出Harry :: 10 8
John :: 7
Sam :: 9 8
地图正是这样做的;它从一个键映射到一个值。
因此,当您读取 Harry 的第二个输入时,它会替换之前的值。
如果您需要映射到多个值,您可以将映射设置为从名称到整数集合的映射。
例如:
Map<String, List<Integer>> map = new TreeMap<>();
//...
if(map.containsKey(name) == false)
map.put(name, new LinkedList<Integer>());
map.get(name).add(score);
What happens to the values of duplicate keys in a tree map
重复键的旧值被替换。如果 Map
发现该键已经存在,则它会替换该值。例如
"Harry" => 10
变成
"Harry" => 8
is it possible to have an output like
是的,但您需要维护一个 List<Integer>
作为您的值。
与其盲目地 put
设置 key/value 对,不如使用 containsKey
测试密钥是否已经存在。如果键已经存在,get
列表和 add
它的值。如果键不存在,则创建一个新的 List
,将值添加到它,并将 put
和 key/value 放入 Map
.
例如
"Harry" => [10]
到
"Harry" => [10, 8]
正如其他人指出的那样,值将被替换,因为 Map 只允许一个键 -> 值映射。
将多个值映射到一个键的最简单方法是使用多(值)映射。为此,我更喜欢 Guava:
Scanner scanner = new Scanner(new FileReader("marks.txt"));
TreeMultimap<String, Integer> students = TreeMultimap.create();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\s+");
students.put(columns[0], Integer.parseInt(columns[1]));
}
System.out.println("Alpha Order");
// Iterate over TreeMap
for (String key : students.keySet()) {
System.out.println(key + " :: " + String.join(", ", students.get(key)));
}
但是如果您不能添加依赖项,您可以使用 TreeMap<String, TreeSet<Integer>>
:
Scanner scanner = new Scanner(new FileReader("marks.txt"));
TreeMap<String, TreeSet<Integer>> students = new TreeMap<>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\s+");
String student = columns[0];
TreeSet<Integer> set;
if(students.containsKey(student)) {
// A set already exists in the map for this student,
// append to it
set = students.get(student);
} else {
// No set exists in the map for this student,
// create a new one and put it in the map
set = new TreeSet<>();
students.put(student, set);
}
set.add(Integer.parseInt(columns[1]));
}
System.out.println("Alpha Order");
// Iterate over TreeMap
for (String key : students.keySet()) {
System.out.println(key + " :: " + String.join(", ", students.get(key)));
}
请注意,这两个使用集合,因此不允许重复的键 -> 值映射。 IE。你不能得到这个输出:
Harry :: 10 8 8
John :: 7 7
Sam :: 9 9 8
后一种解决方案很容易实现,只需将 TreeSet
替换为 ArrayList
。