比较 ArrayList 中的两个字符串,找出每个人的最高成绩
Compare two strings in ArrayList to find each person highest grade
我正在尝试在这里实现一个 ArrayList,每个人都有最高分。我需要阅读文本文件并从那里整理出包含人名和年级的行。
排序后 namesWithGrades
输出如下所示:
[David Doe 3, Carol White 3, Bob Brown 4, Bob Brown 4, Alice Smith 4, David Doe 4, Alice Smith 5, Bob Brown 5]
所需的输出应按任何顺序显示如下:
[Carol White 3, David Doe 4, Alice Smith 5, Bob Brown 5]
我的问题是,我如何能够比较 namesWithGrade
ArrayList 中的姓名和成绩以获得 result
ArrayList 所需的结果?
到目前为止我的主要代码:
public List<String> getFullNamesWithHighestGrade() {
readLines();
List<String> namesWithGrade = new ArrayList<>();
List<String> result = new ArrayList<>();
for(String person: dataOrig){
String[] data = person.split("\|");
namesWithGrade.add(data[0] + " " + data[1] + " " + data[3]);
}
namesWithGrade.sort(Comparator.comparing(s -> s.split(" ")[2]));
System.out.println(namesWithGrade);
return result;
}
正在从 grades.txt 文件读取数据:
private static final String FILE_PATH = "src/ex1/grades.txt";
public List<String> dataOrig;
private List<String> readLines() {
try {
dataOrig = Files.readAllLines(Paths.get(FILE_PATH));
return dataOrig;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
grades.txt 文件:
Alice|Smith|math|5
Bob|Brown|english|4
David|Doe|math|3
Bob|Brown|math|4
Bob|Brown|chemistry|5
Alice|Smith|english|4
Carol|White|chemistry|3
David|Doe|chemistry|4
创建一个 HashMap。这是一个可能的例子:
HashMap<String, Integer> results = new HashMap<String, Integer>();
for(String person: dataOrig) {
String[] data = person.split(“\|”);
String currentName = data[0] + “ “ + data[1]; //get the name of the current line
int currentGrade = Integer.parseInt(data[3]); //get the grade of the current line
if(results.containsKey(currentName)) { //if the name is in the HashMap with a grade
if(currentGrade > results.get(currentName)) { //if the current grade is greater than the one that is already in the HashMap, replace it.
results.put(currentName, currentGrade);
}
}
else //if the name hasn’t been added to the HashMap yet, put it in.
results.put(currentName, currentGrade);
}
HashMap 现在应该包含具有正确最高等级的名称。无需使用 2 个 ArrayList。您可以使用此循环打印出结果。要按值对 HashMap 进行排序,请查看此 link:https://www.google.com/amp/s/www.geeksforgeeks.org/sorting-a-hashmap-according-to-values/amp/
for(Map.Entry<String, Integer> entry : results.entrySet()) {
System.out.println(entry.getKey() + “ “ + entry.getValue());
}
我正在尝试在这里实现一个 ArrayList,每个人都有最高分。我需要阅读文本文件并从那里整理出包含人名和年级的行。
排序后 namesWithGrades
输出如下所示:
[David Doe 3, Carol White 3, Bob Brown 4, Bob Brown 4, Alice Smith 4, David Doe 4, Alice Smith 5, Bob Brown 5]
所需的输出应按任何顺序显示如下:
[Carol White 3, David Doe 4, Alice Smith 5, Bob Brown 5]
我的问题是,我如何能够比较 namesWithGrade
ArrayList 中的姓名和成绩以获得 result
ArrayList 所需的结果?
到目前为止我的主要代码:
public List<String> getFullNamesWithHighestGrade() {
readLines();
List<String> namesWithGrade = new ArrayList<>();
List<String> result = new ArrayList<>();
for(String person: dataOrig){
String[] data = person.split("\|");
namesWithGrade.add(data[0] + " " + data[1] + " " + data[3]);
}
namesWithGrade.sort(Comparator.comparing(s -> s.split(" ")[2]));
System.out.println(namesWithGrade);
return result;
}
正在从 grades.txt 文件读取数据:
private static final String FILE_PATH = "src/ex1/grades.txt";
public List<String> dataOrig;
private List<String> readLines() {
try {
dataOrig = Files.readAllLines(Paths.get(FILE_PATH));
return dataOrig;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
grades.txt 文件:
Alice|Smith|math|5
Bob|Brown|english|4
David|Doe|math|3
Bob|Brown|math|4
Bob|Brown|chemistry|5
Alice|Smith|english|4
Carol|White|chemistry|3
David|Doe|chemistry|4
创建一个 HashMap。这是一个可能的例子:
HashMap<String, Integer> results = new HashMap<String, Integer>();
for(String person: dataOrig) {
String[] data = person.split(“\|”);
String currentName = data[0] + “ “ + data[1]; //get the name of the current line
int currentGrade = Integer.parseInt(data[3]); //get the grade of the current line
if(results.containsKey(currentName)) { //if the name is in the HashMap with a grade
if(currentGrade > results.get(currentName)) { //if the current grade is greater than the one that is already in the HashMap, replace it.
results.put(currentName, currentGrade);
}
}
else //if the name hasn’t been added to the HashMap yet, put it in.
results.put(currentName, currentGrade);
}
HashMap 现在应该包含具有正确最高等级的名称。无需使用 2 个 ArrayList。您可以使用此循环打印出结果。要按值对 HashMap 进行排序,请查看此 link:https://www.google.com/amp/s/www.geeksforgeeks.org/sorting-a-hashmap-according-to-values/amp/
for(Map.Entry<String, Integer> entry : results.entrySet()) {
System.out.println(entry.getKey() + “ “ + entry.getValue());
}