last condition is not satisfying...
package com.codegym.task.task19.task1920;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;
import java.util.Map.Entry;
import java.io.*;
/*
The richest
Calculating salaries
Requirements:
1. The program must NOT read data from the console.
2. The program must read the file's contents (use FileReader).
3. The file input stream (FileReader) must be closed.
4. The program should write to the console each name,
sorted in ascending order, and the sum of all its values.
*/
public class Solution {
public static void main(String[] args) {
try{
FileReader reader = new FileReader(args[0]);
BufferedReader br = new BufferedReader(reader);
String data = null;
String[] dataArray;
Map<String, Double> map = new TreeMap<String, Double>();
while((data = br.readLine()) != null){
dataArray = data.split(" ");
if(map.containsKey(dataArray[0])){
Double value = map.get(dataArray[0]);
value = value + Double.parseDouble(dataArray[1]);
map.remove(dataArray[0]);
map.put(dataArray[0],value);
}else{
dataArray = data.split(" ");
map.put(dataArray[0], Double.parseDouble(dataArray[1]));
}
}
/* Map<String, Integer> map = new HashMap<String, Integer>();
map.put("java", 20);
map.put("C++", 45);
map.put("Java2Novice", 2);
map.put("Unix", 67);
map.put("MAC", 26);
map.put("Why this kolavari", 93);*/
Set<Entry<String, Double>> set = map.entrySet();
List<Entry<String, Double>> list = new ArrayList<Entry<String, Double>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Double>>()
{
public int compare( Map.Entry<String, Double> o1, Map.Entry<String, Double> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
int count = 0;
for(Map.Entry<String, Double> entry:list){
/*if(count > 1) break;
count++;*/
System.out.println(entry.getKey());
}
reader.close();
br.close();
}catch(Exception ex){
}
}
}