如何计算名称出现的次数并为其分配点?
How to count the number of name occurence and assign point to it?
我有 csv 文件。结构是这样的
First Second Third
Alex Peter Max
Peter Alex John
Steven James John
Steven James John
Max John Steven
我想阅读所有行并根据位置分配点,例如亚历克斯每提到第一名将获得 3 分,第二名将获得 2 分,第三名将获得 1 分。所以最终输出应该显示所有名称和分配的分数。应该是这样
Steven - 7 points (3 + 3 + 1)
Alex - 5 points (3 + 2)
Peter - 5 points (3 + 2)
Max - 4 points (3 + 1)
James - 4 points (2 + 2)
John - 4 points (2 + 2)
下面是我的代码
public class Main {
private final static String FILE_PATH = "Table.csv";
private final static String FIRST_PLACE = "First";
private final static String SECOND_PLACE = "Second";
private final static String THIRD_PLACE = "Third";
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(FILE_PATH));
String s;
List<String> firstName = new ArrayList<>();
while ((s = br.readLine()) != null){
firstName.add(s);
}
Map<String, Long> sortedName = firstName.stream()
.collect(groupingBy(chr -> chr, counting()))
.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
));
sortedName.forEach((k, v) -> System.out.println("Name: " + k + " || " + "Count: " + v));
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
但它会逐行计数并计算列名并给出此输出
Name First Second Third || Count 1
Name Alex Peter Max || Count 1
Name Peter Alex John || Count 1
Name Steven James John || Count 2
Name Max John Steven || Count 1
您需要的pseudo-code是:
for each in line (except first) in from file
read line
tokenize line to string array of size 3
for each token
if name is present in map
map.put(name, map.get(name)+(3-i)) //i is index of token travesal loop
else
map.put(name, (3 - i))
map.entrySet().stream()
.sorted((k1, k2) -> -k1.getValue().compareTo(k2.getValue()))
.forEach(k -> System.out.println(k.getKey() + ": " + k.getValue())); // Print while ordering the map by values
我有 csv 文件。结构是这样的
First Second Third
Alex Peter Max
Peter Alex John
Steven James John
Steven James John
Max John Steven
我想阅读所有行并根据位置分配点,例如亚历克斯每提到第一名将获得 3 分,第二名将获得 2 分,第三名将获得 1 分。所以最终输出应该显示所有名称和分配的分数。应该是这样
Steven - 7 points (3 + 3 + 1)
Alex - 5 points (3 + 2)
Peter - 5 points (3 + 2)
Max - 4 points (3 + 1)
James - 4 points (2 + 2)
John - 4 points (2 + 2)
下面是我的代码
public class Main {
private final static String FILE_PATH = "Table.csv";
private final static String FIRST_PLACE = "First";
private final static String SECOND_PLACE = "Second";
private final static String THIRD_PLACE = "Third";
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(FILE_PATH));
String s;
List<String> firstName = new ArrayList<>();
while ((s = br.readLine()) != null){
firstName.add(s);
}
Map<String, Long> sortedName = firstName.stream()
.collect(groupingBy(chr -> chr, counting()))
.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
));
sortedName.forEach((k, v) -> System.out.println("Name: " + k + " || " + "Count: " + v));
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
但它会逐行计数并计算列名并给出此输出
Name First Second Third || Count 1
Name Alex Peter Max || Count 1
Name Peter Alex John || Count 1
Name Steven James John || Count 2
Name Max John Steven || Count 1
您需要的pseudo-code是:
for each in line (except first) in from file
read line
tokenize line to string array of size 3
for each token
if name is present in map
map.put(name, map.get(name)+(3-i)) //i is index of token travesal loop
else
map.put(name, (3 - i))
map.entrySet().stream()
.sorted((k1, k2) -> -k1.getValue().compareTo(k2.getValue()))
.forEach(k -> System.out.println(k.getKey() + ": " + k.getValue())); // Print while ordering the map by values