Skip to content

Instantly share code, notes, and snippets.

@pteacher
Created April 10, 2017 20:15
Show Gist options
  • Save pteacher/ad3a29dea847d9aaccec3266e3aaecd9 to your computer and use it in GitHub Desktop.
Save pteacher/ad3a29dea847d9aaccec3266e3aaecd9 to your computer and use it in GitHub Desktop.
Без сортировки результатов, но все работает.
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
/**
* Created by altairlab on 10.04.2017.
*/
public class Football {
public static void main(String[] args) {
File file = new File("matches.txt");
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
} catch (Exception e) {
e.printStackTrace();
}
Scanner scanner = new Scanner(fin);
TreeMap<String, Integer> teams = new TreeMap<String, Integer>();
while (scanner.hasNext()) {
String matchResult = scanner.nextLine();
String teamHome = matchResult.split(" ")[0];
String teamGuest = matchResult.split(" ")[1];
int resultHome = Integer.parseInt(matchResult
.split(" ")[2]
.split(":")[0]);
int resultGuest = Integer.parseInt(matchResult
.split(" ")[2]
.split(":")[1]);
teams.put(teamHome, (teams.containsKey(teamHome))
? (getMatchResult(resultHome, resultGuest) + teams.get(teamHome))
: getMatchResult(resultHome, resultGuest));
teams.put(teamGuest, (teams.containsKey(teamGuest))
? (getMatchResult(resultGuest, resultHome) + teams.get(teamGuest))
: getMatchResult(resultGuest, resultHome));
}
for (Map.Entry<String, Integer> entry : teams.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
public static int getMatchResult(int home, int guest) {
if (home > guest) return 3;
else if (home == guest) return 1;
return 0;
}
}
@pteacher
Copy link
Author

pteacher commented Apr 11, 2017

Сортировка отображения (Map) по значению

Object[] a = teams.entrySet().toArray();
Arrays.sort(a, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Map.Entry<String, Integer>) o2).getValue()
                        .compareTo(((Map.Entry<String, Integer>) o1).getValue());
            }
        });

for (Object e : a) {
    System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
            + ((Map.Entry<String, Integer>) e).getValue());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment