List of usage examples for java.util.stream Collectors groupingBy
public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
From source file:eu.cloudwave.wp5.feedbackhandler.aggregations.strategies.RequestAggregationStrategyImpl.java
/** * {@inheritDoc}/*from w w w .ja v a 2 s. com*/ */ @Override public RequestAggregationValues aggregate(RequestCollector requests) { double expectedCount = getExpectedNumberOfMeasurementValueGroups(); /* * Group by aggregation interval and create summary statistics with min, avg, max and count */ Collection<Long> groupedByAggregationInterval = requests.getReqTimestamps().stream() .collect(Collectors.groupingBy( timestamp -> DateUtils.round(new Date(timestamp), timestampAggregation), Collectors.counting())) .values(); int calculatedCount = groupedByAggregationInterval.size(); try { if (calculatedCount != 0) { // use integer summaryStatistics to get min, avg, max IntSummaryStatistics stats = groupedByAggregationInterval.stream().mapToInt(p -> toInt(p)) .summaryStatistics(); // no time range selected, just return int summary if (expectedCount == 0) { return new RequestAggregationValuesImpl(stats.getMin(), stats.getMax(), stats.getAverage(), stats.getSum(), stats.getCount()); } else { // if calculated count != expected count --> adjust minimum, average and count value if (Double.compare(calculatedCount, expectedCount) != 0) { double newAverage = (double) (stats.getSum() / expectedCount); return new RequestAggregationValuesImpl(0, stats.getMax(), newAverage, stats.getSum(), (int) expectedCount); } else { return new RequestAggregationValuesImpl(stats.getMin(), stats.getMax(), stats.getAverage(), stats.getSum(), (int) expectedCount); } } } else { return new RequestAggregationValuesImpl(0, 0, 0, 0, 0); } } catch (ArithmeticException e) { System.out.println(e.getMessage()); return new RequestAggregationValuesImpl(0, 0, 0, 0, 0); } }
From source file:pl.edu.icm.comac.vis.server.service.AtomicGraphServiceImpl.java
@Override public Graph constructGraphs(String[] ids) throws OpenRDFException { List<NodeCacheEntry> favCacheNodes = fetchNodes(ids); //build link map Map<String, Set<String>> links = favCacheNodes.parallelStream().filter(x -> !x.isOverflow()) .map(x -> x.getRelations()).flatMap(x -> x.stream()) .flatMap(x -> Arrays.stream( new String[][] { { x.getSubject(), x.getObject() }, { x.getObject(), x.getSubject() } })) .collect(Collectors.groupingBy(x -> x[0], Collectors.mapping(x -> x[1], Collectors.toSet()))); Set<String> large = favCacheNodes.stream().filter(x -> x.isOverflow()).map(x -> x.getId()) .collect(Collectors.toSet()); Set<String> normal = favCacheNodes.stream().filter(x -> !x.isOverflow()).map(x -> x.getId()) .collect(Collectors.toSet()); Set<String> unfav = graphToolkit.calculateAdditions(normal, large, links, MAX_RETURNED_RELATIONS); //now fetch the unfavs: List<NodeCacheEntry> unfavCacheNodes = fetchNodes(unfav.toArray(new String[unfav.size()])); List<NodeCacheEntry> allNodes = new ArrayList<NodeCacheEntry>(); allNodes.addAll(favCacheNodes);//from w ww . java 2 s . com allNodes.addAll(unfavCacheNodes); List<NodeCacheEntry> largeNodes = allNodes.stream().filter(x -> x.isOverflow()) .collect(Collectors.toList()); List<RelationCacheEntry> largeRelations = calculateRelations(largeNodes); //now build the graph: List<Node> nodes = new ArrayList<>(); List<Node> fnodes = favCacheNodes.stream().map(cached -> { Node res = new Node(cached.getId(), cached.getType(), cached.getName(), 1.0); res.setFavourite(true); return res; }).collect(Collectors.toList()); nodes.addAll(fnodes); List<Node> ufnodes = unfavCacheNodes.stream().map(cached -> { Node res = new Node(cached.getId(), cached.getType(), cached.getName(), 1.0); res.setFavourite(false); return res; }).collect(Collectors.toList()); nodes.addAll(ufnodes); Set<String> nodeIdSet = nodes.stream().map(x -> x.getId()).collect(Collectors.toSet()); Set<Link> graphRelations = allNodes.parallelStream().filter(x -> !x.isOverflow()) .flatMap(x -> x.getRelations().stream()) .filter(x -> nodeIdSet.contains(x.subject) && nodeIdSet.contains(x.object)) .map(x -> new Link(x.getPredicate(), x.getSubject(), x.getObject())).collect(Collectors.toSet()); Graph res = new Graph(); res.setNodes(nodes); res.setLinks(new ArrayList<Link>(graphRelations)); return res; }
From source file:com.firewallid.util.FIUtils.java
public static Vector sumVector(Vector v1, Vector v2) { if (v1.size() != v2.size()) { return null; }/* w w w . j a v a2 s . c o m*/ List<Tuple2<Integer, Double>> v1List = vectorToList(v1); List<Tuple2<Integer, Double>> v2List = vectorToList(v2); List<Tuple2<Integer, Double>> sumList = combineList(v1List, v2List); /* Sum value of same key-pair */ List<Tuple2<Integer, Double>> collect = sumList.parallelStream() .collect(Collectors.groupingBy(t -> t._1, Collectors.mapping((Tuple2<Integer, Double> t) -> t._2, Collectors.toList()))) .entrySet().parallelStream() .map((Map.Entry<Integer, List<Double>> t) -> new Tuple2<Integer, Double>(t.getKey(), t.getValue().parallelStream().mapToDouble(Double::doubleValue).sum())) .collect(Collectors.toList()); return Vectors.sparse(v1.size(), collect); }
From source file:de.mg.stock.server.model.Stock.java
/** * @return list of all day prices filled up with instant prices, in case of missing day prices *//*from www .j a v a 2 s. c o m*/ public Set<SimpleDayPrice> getAllPricesDaily() { getInstantPrices().stream().forEach(ip -> logger.info(ip.toString())); List<SimpleDayPrice> prices = Stream .concat(getDayPrices().stream().map(dp -> new SimpleDayPrice(dp.getDay(), dp.getAverage())), getInstantPrices().stream() .map(ip -> new SimpleDayPrice(ip.getTime().toLocalDate(), ip.getAverage()))) .collect(Collectors.toList()); Set<SimpleDayPrice> result = prices.stream() .collect(Collectors.groupingBy(SimpleDayPrice::getDate, Collectors.toSet())).entrySet().stream() .map(e -> new SimpleDayPrice(e.getKey(), (long) e.getValue().stream().mapToLong(SimpleDayPrice::getAverage).average().getAsDouble())) .collect(Collectors.toSet()); return result; }
From source file:de.whs.poodle.repositories.EvaluationWorksheetRepository.java
public Map<EvaluationQuestion, List<String>> getQuestionToTextsMapForEvaluation(int evaluationWorksheetId) { List<EvaluationStatistic> stats = em .createQuery("FROM EvaluationStatistic WHERE question.section.worksheet.id = :worksheetId " + "AND text IS NOT NULL", EvaluationStatistic.class) .setParameter("worksheetId", evaluationWorksheetId).getResultList(); return stats.stream().collect( // group the list... Collectors.groupingBy( // by question EvaluationStatistic::getQuestion, // and map each question to the list of texts Collectors.mapping(EvaluationStatistic::getText, Collectors.toList()))); }
From source file:org.trustedanalytics.datasetpublisher.boundary.MetadataMapper.java
private void checkDuplicates(List<String> strings, String exceptionMessagePrefix) { strings.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet() .stream().filter(group -> group.getValue() > 1).map(Map.Entry::getKey).sorted() .reduce((acc, column) -> acc + ", " + column).ifPresent(duplicates -> { throw new IllegalStateException(exceptionMessagePrefix + ": " + duplicates); });/*from w ww. j a va 2s . c o m*/ }
From source file:com.gs.collections.impl.jmh.AggregateByTest.java
@Warmup(iterations = 20) @Measurement(iterations = 10)//from ww w . j av a2s . c o m @Benchmark public Map<Product, DoubleSummaryStatistics> aggregateByProduct_serial_lazy_jdk() { Map<Product, DoubleSummaryStatistics> result = this.jdkPositions.stream().collect(Collectors .groupingBy(Position::getProduct, Collectors.summarizingDouble(Position::getMarketValue))); Assert.assertNotNull(result); return result; }
From source file:com.firewalld.sentimentanalysis.IDSentiWordNet.java
private Map<String, Double> createDictionary() throws IOException { Map<String, Double> dict = IOUtils .readLines(getClass().getClassLoader().getResourceAsStream(firewallConf.get(SWN_FILE))) .parallelStream()//from w w w. ja v a2 s. com /* If it's a comment, skip this line */ .filter(line -> !line.trim().startsWith("#")).flatMap(line -> { String[] data = line.split("\t"); String wordTypeMarker = data[0]; // Example line: // POS ID PosS NegS SynsetTerm#sensenumber Desc // a 00009618 0.5 0.25 spartan#4 austere#3 ascetical#2 ascetic#2 practicing great self-denial;...etc // Is it a valid line? Otherwise, through exception. if (data.length != 6) { throw new IllegalArgumentException( String.format("Incorrect tabulation format in file, line: %s", line)); } // Calculate synset score as score = PosS - NegS Double synsetScore = Double.parseDouble(data[2]) - Double.parseDouble(data[3]); // Get all Synset terms String[] synTermsSplit = data[4].split(" "); // Go through all terms of current synset. Stream<Tuple2<String, Tuple2<Double, Double>>> synSets = Arrays.asList(synTermsSplit) .parallelStream().map(synTermSplit -> { // Get synterm and synterm rank String[] synTermAndRank = synTermSplit.split("#"); String synTerm = synTermAndRank[0] + "#" + wordTypeMarker; double synTermRank = Double.parseDouble(synTermAndRank[1]); // What we get here is a (term, (rank, score)) return new Tuple2<>(synTerm, new Tuple2<>(synTermRank, synsetScore)); }); return synSets; }) // What we get here is a map of the type: // term -> {score of synset#1, score of synset#2...} .collect(Collectors.groupingBy(synSet -> synSet._1, Collectors.mapping(synSet -> synSet._2, Collectors.toList()))) .entrySet().parallelStream().map(synSet -> { String word = synSet.getKey(); List<Tuple2<Double, Double>> synSetScoreList = synSet.getValue(); // Calculate weighted average. Weigh the synsets according to // their rank. // Score= 1/2*first + 1/3*second + 1/4*third ..... etc. // Sum = 1/1 + 1/2 + 1/3 ... Tuple2<Double, Double> scoreSum = synSetScoreList.parallelStream() .reduce(new Tuple2<>(0.0, 0.0), (s1, s2) -> new Tuple2<>( ((s1._1 == 0.0) ? 0.0 : s1._2 / s1._1) + ((s2._1 == 0.0) ? 0.0 : s2._2 / s2._1), ((s1._1 == 0.0) ? 0.0 : 1 / s1._1) + ((s2._1 == 0.0) ? 0.0 : 1 / s2._1))); double score = scoreSum._1 / scoreSum._2; return new Tuple2<>(word, score); }).collect(Collectors.toMap(synSet -> synSet._1, synSet -> synSet._2)); return dict; }
From source file:com.thoughtworks.go.server.service.plugins.builder.DefaultPluginInfoFinder.java
public Collection<CombinedPluginInfo> allPluginInfos(String type) { if (isBlank(type)) { return builders.values().stream() .map((Function<MetadataStore, Collection<? extends PluginInfo>>) MetadataStore::allPluginInfos) .flatMap(//ww w. ja va2 s . c om (Function<Collection<? extends PluginInfo>, Stream<? extends PluginInfo>>) Collection::stream) .collect(Collectors.groupingBy(pluginID(), toCollection(CombinedPluginInfo::new))).values(); } else if (builders.containsKey(type)) { Collection<PluginInfo> pluginInfosForType = builders.get(type).allPluginInfos(); return pluginInfosForType.stream().map(CombinedPluginInfo::new).collect(toList()); } else { throw new InvalidPluginTypeException(); } }
From source file:hr.diskobolos.persistence.impl.EvaluationAnswerPersistenceImpl.java
@Override public Map<MemberRegister, Integer> fetchTotalPointsPerMemberRegister(QuestionnaireType questionnaireType) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = cb.createTupleQuery(); Root<EvaluationAnswer> evaluationAnswer = cq.from(EvaluationAnswer.class); Join<EvaluationAnswer, QuestionChoicesDef> choiceDef = evaluationAnswer.join(EvaluationAnswer_.answer); Join<QuestionChoicesDef, EvaluationQuestionDef> questionDef = choiceDef .join(QuestionChoicesDef_.evaluationQuestionDef); ParameterExpression<QuestionnaireType> questionnaireTypeParam = cb.parameter(QuestionnaireType.class, "questionnaireType"); Expression<Integer> value = evaluationAnswer.get(EvaluationAnswer_.answer).get(QuestionChoicesDef_.value) .as(Integer.class); cq.multiselect(evaluationAnswer.get(EvaluationAnswer_.memberRegister).alias("memberRegister"), value.alias("value")); cq.where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireTypeParam)); TypedQuery<Tuple> query = entityManager.createQuery(cq); query.setParameter("questionnaireType", questionnaireType); List<Tuple> result = query.getResultList(); Map<MemberRegister, Integer> pointsPerMemberRegister = result.stream().collect(Collectors.groupingBy(t -> { return t.get("memberRegister", MemberRegister.class); }, Collectors.summingInt(t -> t.get("value", Integer.class)))); return pointsPerMemberRegister; }