The input I've tested is:
Peterson 2
Smith 6
Baxter 1.35
Peterson 3.1
McDonald 5.4
Baxter 3.4
McDonald 0.6
Baxter 1.25
The output is:
Baxter
McDonald
Smith
I made sure to test with duplicate highest values.
The TreeMap made sense to me as we can easily look at the last Key using the lastKey() method and work from there.
package com.codegym.task.task19.task1920;
/*
The richest
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader(args[0]);
//FileReader file = new FileReader("C:\\Users\\Angus\\Desktop\\Temp\\data.txt");
BufferedReader fileReader = new BufferedReader(file);
TreeMap<String, Double> map = new TreeMap();
String data;
while ((data = fileReader.readLine()) != null) {
String[] person = data.split("\\s");
String name = person[0];
Double value = Double.parseDouble(person[1]);
if (map.containsKey(name)) {
map.replace(name, map.get(name) + value);
}
else map.put(name, value);
}
Double highest = map.get(map.lastKey());
ArrayList<String> sums = new ArrayList<>();
for (Map.Entry<String, Double> high : map.entrySet()) {
if (high.getValue().equals(highest)) {
sums.add(high.getKey());
}
}
Collections.sort(sums);
for (String name : sums) {
System.out.println(name);
}
file.close();
fileReader.close();
}
}