Last active
August 29, 2015 14:17
-
-
Save nomade1999/f191d5fe0dca4c1aa7d2 to your computer and use it in GitHub Desktop.
Modified version of Stats
This file contains 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
/* source file format ; | |
1,"a:1;20|b:2;20|d:3;20" | |
2,"a:2|c:2|c:4" | |
3,"a:2;20|bb:2;20|a:3;20" | |
4,"a:3|e:2|ff:3" | |
*/ | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.*; | |
import java.util.function.Predicate; | |
import java.util.stream.Stream; | |
import static java.util.stream.Collectors.*; | |
public class Stats { | |
public static void main(String[] args) throws IOException { | |
List<Data> list = Files.lines(Paths.get("/Volumes/Development/workspace/file.txt")) | |
.map(s -> s.substring(0, s.length()-1).split(",")) | |
.map(arr -> new Data(Integer.parseInt(arr[0]), arr[1])) | |
.collect(toList()); | |
System.out.println(list); | |
System.out.println(ids(list, "a", i -> i < 3)); | |
Map<String, IntSummaryStatistics> stats = list.stream() | |
.flatMap(d -> d.getPronScoreMap().entrySet().stream()) | |
.collect(toMap(Map.Entry::getKey, | |
e -> e.getValue().stream().mapToInt(i -> i).summaryStatistics(), | |
(i1, i2) -> {i1.combine(i2); return i1;})); | |
stats.forEach((k, v) -> System.out.println(k + " => " + v)); | |
} | |
public static Set<Integer> ids(List<Data> list, String id, Predicate<Integer> predicate) { | |
return list.stream() | |
.filter(d -> d.getPronScoreMap().containsKey(id)) | |
.filter(d -> d.getPronScoreMap().get(id).stream().anyMatch(predicate)) | |
.map(d -> d.getId()) | |
.collect(toSet()); | |
} | |
} | |
class Data { | |
private int id; | |
private Map<String, List<Integer>> mapPronScore; | |
private Map<String, List<Integer>> mapConfScore; | |
public Data(int id, String datas) { | |
this.id = id; | |
this.mapPronScore = new HashMap<>(); | |
this.mapConfScore = new HashMap<>(); | |
for(String s : datas.substring(1, datas.length()-0).split("\\|")) { | |
String[] splitted = s.split(":"); | |
String[] scores = splitted[1].split(";"); | |
this.mapPronScore.merge(splitted[0], Arrays.asList(Integer.parseInt(scores[0])), (l1, l2) -> Stream.concat(l1.stream(), l2.stream()).collect(toList())); | |
if (splitted[1].contains(";")) | |
this.mapConfScore.merge(splitted[0], Arrays.asList(Integer.parseInt(scores[1])), (l1, l2) -> Stream.concat(l1.stream(), l2.stream()).collect(toList())); | |
} | |
} | |
public int getId() { | |
return this.id; | |
} | |
public Map<String, List<Integer>> getPronScoreMap() { | |
return this.mapPronScore; | |
} | |
public Map<String, List<Integer>> getConfScoreMap() { | |
return this.mapConfScore; | |
} | |
@Override | |
public String toString() { | |
return "Data{" + | |
"id=" + id + | |
", mapPronScore=" + mapPronScore + | |
", mapConfScore=" + mapConfScore + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment