Last active
July 15, 2017 19:09
-
-
Save aNNiMON/d8e0ef45651cad1dd49758a49d946669 to your computer and use it in GitHub Desktop.
Stream API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RoachDrom 2 1 1 0 1 | |
LongFlight 3 2 2 1 2 | |
Units2D 2 1 1 0 1 | |
Zombies&Demons2D 2 2 3 1 2 | |
SaveUA 2 0 0 1 0 | |
Inovation 0 0 0 0 0 | |
Night&Day 2 1 1 1 1 | |
Нечто 2 2 2 1 3 | |
SpaceCatcher 1 1 2 1 1 | |
Galaxy 1 0 0 0 0 | |
SonicTime3D 3 2 2 0 3 | |
aPlatformer 1 1 0 1 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RoachDrom 0 1 0 1 1 | |
LongFlight 1 2 0 2 1 | |
Units2D 0 1 0 1 0 | |
Zombies&Demons2D 1 2 0 1 1 | |
SaveUA 1 1 0 0 0 | |
Inovation 1 0 0 0 0 | |
Night&Day 1 0 0 1 1 | |
Нечто 0 3 0 2 1 | |
SpaceCatcher 0 1 0 1 1 | |
Galaxy 0 1 0 0 0 | |
SonicTime3D 0 2 0 1 1 | |
aPlatformer 1 1 0 1 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RoachDrom 0 1 1 1 1 | |
LongFlight 1 2 4 2 2 2 | |
Units2D 0 2 1 2 1 | |
Zombies&Demons2D 1 2 3 2 1 | |
SaveUA 1 2 0 2 1 | |
Inovation 0 0 0 0 0 | |
Night&Day 1 3 4 3 2 | |
Нечто 0 3 1 2 2 | |
SpaceCatcher 1 2 2 1 2 | |
Galaxy 0 3 1 1 1 | |
SonicTime3D 0 3 3 2 1 | |
aPlatformer 1 0 0 1 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.stream; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Ratings { | |
public static void main(String[] args) throws IOException { | |
Files.list(Paths.get("ratings")) | |
.flatMap(path -> readLines(path).stream()) | |
.map(line -> line.split("\\s+")) | |
.map(columns -> IntPair.from(columns[0], calculateSum(columns))) | |
.collect(Collectors.groupingBy(IntPair::getFirst)).entrySet().stream() | |
.map(entry -> IntPair.from( | |
entry.getKey(), | |
entry.getValue().stream() | |
.mapToInt(IntPair::getSecond) | |
.sum() | |
)) | |
.sorted(Comparator.<IntPair>comparingInt(IntPair::getSecond).reversed()) | |
.map(pair -> String.format("%16s: %d", pair.getFirst(), pair.getSecond())) | |
.limit(5) | |
.forEach(System.out::println); | |
} | |
private static int calculateSum(String[] columns) { | |
return Arrays.stream(columns) | |
.skip(1) | |
.mapToInt(Integer::parseInt) | |
.sum(); | |
} | |
private static List<String> readLines(Path p) { | |
try { | |
return Files.readAllLines(p, StandardCharsets.UTF_8); | |
} catch (IOException ex) { | |
return Collections.emptyList(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.stream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class RatingsJ7 { | |
public static void main(String[] args) throws IOException { | |
final File[] files = new File("ratings").listFiles(); | |
Map<String, Integer> projectsGroup = new HashMap<>(); | |
for (File file : files) { | |
List<String> lines = readLines(file.toPath()); | |
for (String line : lines) { | |
IntPair<String> projectInfo = processLine(line); | |
final String key = projectInfo.getFirst(); | |
int rating = projectInfo.getSecond(); | |
if (projectsGroup.containsKey(key)) { | |
rating += projectsGroup.get(key); | |
} | |
projectsGroup.put(key, rating); | |
} | |
} | |
List<IntPair<String>> projects = new ArrayList<>(); | |
for (Map.Entry<String, Integer> entry : projectsGroup.entrySet()) { | |
projects.add(IntPair.from(entry.getKey(), entry.getValue())); | |
} | |
Collections.sort(projects, Comparator.<IntPair>comparingInt(IntPair::getSecond).reversed()); | |
final int size = Math.min(5, projects.size()); | |
for (int i = 0; i < size; i++) { | |
IntPair<String> project = projects.get(i); | |
System.out.format("%16s: %d%n", project.getFirst(), project.getSecond()); | |
} | |
} | |
private static IntPair<String> processLine(String line) { | |
String[] columns = line.split("\\s+"); | |
int sum = 0; | |
boolean isFirst = true; | |
for (String column : columns) { | |
if (isFirst) { | |
isFirst = false; | |
continue; | |
} | |
sum += Integer.parseInt(column); | |
} | |
return IntPair.from(columns[0], sum); | |
} | |
private static List<String> readLines(Path p) { | |
try { | |
return Files.readAllLines(p, StandardCharsets.UTF_8); | |
} catch (IOException ex) { | |
return Collections.emptyList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment