-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem18.java
More file actions
68 lines (66 loc) · 1.8 KB
/
Problem18.java
File metadata and controls
68 lines (66 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
class MapManager{
private static Map<String,Double> map = new HashMap<String,Double>(){
public String toString() {
List<Entry<String, Double>> entries = new ArrayList<Entry<String,Double>>(map.entrySet());
Collections.sort(entries, new Comparator<Entry<String, Double>>(){
public int compare(Entry<String, Double> o1, Entry<String,Double> o2) {
if(o1.getValue() > o2.getValue()) return 1;
if(o1.getValue() < o2.getValue()) return -1;
return o1.getKey().compareTo(o2.getKey());
}
});
String print = new String();
Iterator<Map.Entry<String,Double>> it = entries.iterator();
while(it.hasNext()) {
Map.Entry<String,Double> e = it.next();
print += e.getKey()+ " " + e.getValue() + "\n";
}
return print.trim();
}
};
public static Map<String, Double> readData(String f){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
}catch (FileNotFoundException e) {
return null;
}
while(true) {
try {
String line = br.readLine();
if(line == null) break;
String[] a = line.split(" ");
map.put(a[0], Double.parseDouble(a[1]));
} catch(IOException e) {
e.printStackTrace();
}
}
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
return map;
}
}
public class Problem18 {
public static void main(String[] args) {
Map<String, Double> map = MapManager.readData("input.txt");
if(map == null) {
System.out.println("Input file not found.");
return;
}
System.out.println(map);
}
}