Example usage for java.util.stream Collectors groupingBy

List of usage examples for java.util.stream Collectors groupingBy

Introduction

In this page you can find the example usage for java.util.stream Collectors groupingBy.

Prototype

public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
        Collector<? super T, A, D> downstream) 

Source Link

Document

Returns a Collector implementing a cascaded "group by" operation on input elements of type T , grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector .

Usage

From source file:org.eclipse.collections.impl.jmh.AnagramSetTest.java

@Benchmark
public void serial_lazy_streams_ec() {
    Map<Alphagram, Set<String>> groupBy = this.ecWords.stream()
            .collect(Collectors.groupingBy(Alphagram::new, Collectors.toSet()));
    groupBy.entrySet().stream().map(Map.Entry::getValue).filter(list -> list.size() >= SIZE_THRESHOLD)
            .sorted(Comparator.<Set<String>>comparingInt(Set::size).reversed())
            .map(list -> list.size() + ": " + list).forEach(e -> Assert.assertFalse(e.isEmpty()));
}

From source file:org.eclipse.collections.impl.jmh.AnagramSetTest.java

@Benchmark
public void parallel_lazy_jdk() {
    Map<Alphagram, Set<String>> groupBy = this.jdkWords.parallelStream()
            .collect(Collectors.groupingBy(Alphagram::new, Collectors.toSet()));
    groupBy.entrySet().parallelStream().map(Map.Entry::getValue).filter(list -> list.size() >= SIZE_THRESHOLD)
            .sorted(Comparator.<Set<String>>comparingInt(Set::size).reversed()).parallel()
            .map(list -> list.size() + ": " + list).forEach(e -> Assert.assertFalse(e.isEmpty()));
}

From source file:org.eclipse.collections.impl.jmh.AnagramSetTest.java

@Benchmark
public void parallel_lazy_streams_ec() {
    Map<Alphagram, Set<String>> groupBy = this.ecWords.parallelStream()
            .collect(Collectors.groupingBy(Alphagram::new, Collectors.toSet()));
    groupBy.entrySet().parallelStream().map(Map.Entry::getValue).filter(list -> list.size() >= SIZE_THRESHOLD)
            .sorted(Comparator.<Set<String>>comparingInt(Set::size).reversed()).parallel()
            .map(list -> list.size() + ": " + list).forEach(e -> Assert.assertFalse(e.isEmpty()));
}

From source file:org.efaps.esjp.accounting.Period_Base.java

/**
 * Gets the label definition./*from w  w w .ja  v  a  2  s.c  om*/
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @return the label definition
 * @throws EFapsException on error
 */
@SuppressWarnings("unchecked")
public Return getTargetDocInfo4PaymentFieldValue(final Parameter _parameter) throws EFapsException {
    final Return ret = new Return();
    final String key = Period.class.getName() + ".RequestKey4TargetDocInfo4PaymentFieldValue";
    final Map<Instance, String> values;
    if (Context.getThreadContext().containsRequestAttribute(key)) {
        values = (Map<Instance, String>) Context.getThreadContext().getRequestAttribute(key);
    } else {
        values = new HashMap<>();
        Context.getThreadContext().setRequestAttribute(key, values);
        final List<Instance> instances = (List<Instance>) _parameter.get(ParameterValues.REQUEST_INSTANCES);

        final MultiPrintQuery print = new MultiPrintQuery(instances);
        final SelectBuilder selTargetInsts = SelectBuilder.get().linkfrom(CISales.Payment.TargetDocument)
                .linkto(CISales.Payment.CreateDocument).instance();
        print.addSelect(selTargetInsts);
        print.execute();
        while (print.next()) {
            final List<String> labels = new ArrayList<>();
            final Object obj = print.getSelect(selTargetInsts);
            if (obj != null) {
                final List<Instance> targetInsts;
                if (obj instanceof Instance) {
                    targetInsts = new ArrayList<>();
                    targetInsts.add((Instance) obj);
                } else {
                    targetInsts = (List<Instance>) obj;
                }
                for (final Instance targetInst : targetInsts) {
                    final SelectBuilder selActName;
                    if (InstanceUtils.isType(targetInst, CISales.PaymentOrder)) {
                        selActName = SelectBuilder.get()
                                .linkfrom(CISales.ActionDefinitionPaymentOrder2Document.ToLinkAbstract)
                                .linkto(CISales.ActionDefinitionPaymentOrder2Document.FromLinkAbstract)
                                .attribute(CISales.ActionDefinitionPaymentOrder.Name);

                    } else if (InstanceUtils.isType(targetInst, CISales.CollectionOrder)) {
                        selActName = SelectBuilder.get()
                                .linkfrom(CISales.ActionDefinitionCollectionOrder2Document.ToLinkAbstract)
                                .linkto(CISales.ActionDefinitionCollectionOrder2Document.FromLinkAbstract)
                                .attribute(CISales.ActionDefinitionCollectionOrder.Name);

                    } else if (InstanceUtils.isType(targetInst, CISales.IncomingExchange)) {
                        selActName = SelectBuilder.get()
                                .linkfrom(CISales.ActionDefinitionIncomingExchange2Document.ToLinkAbstract)
                                .linkto(CISales.ActionDefinitionIncomingExchange2Document.FromLinkAbstract)
                                .attribute(CISales.ActionDefinitionIncomingExchange.Name);

                    } else {
                        selActName = null;
                    }

                    if (selActName != null) {
                        final PrintQuery print2 = new PrintQuery(targetInst);
                        print2.addSelect(selActName);
                        print2.execute();

                        final String actname = print2.getSelect(selActName);
                        if (actname == null) {
                            labels.add(targetInst.getType().getLabel());
                        } else {
                            labels.add(targetInst.getType().getLabel() + " - " + actname);
                        }
                    } else {
                        labels.add(targetInst.getType().getLabel());
                    }
                }
                final Map<String, Long> map = labels.stream()
                        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
                final StringBuilder bldr = new StringBuilder();
                for (final Entry<String, Long> entry : map.entrySet()) {
                    if (bldr.length() > 0) {
                        bldr.append(", ");
                    }
                    bldr.append(entry.getValue()).append(" x ").append(entry.getKey());
                }
                values.put(print.getCurrentInstance(), bldr.toString());
            }
        }
    }
    ret.put(ReturnValues.VALUES, values.get(_parameter.getInstance()));
    return ret;
}

From source file:org.gradoop.flink.model.impl.operators.matching.common.query.QueryHandler.java

/**
 * Initializes a cache for the given elements where every key maps to multiple elements.
 * Key selector will be called on every element to extract the caches key.
 * Value selector will be called on every element to extract the value.
 * Returns a cache of the form//from   ww  w.j  ava  2 s . c  o  m
 * KT -> Set<VT>
 *
 * @param elements elements the cache will be build from
 * @param keySelector key selector function extraction cache keys from elements
 * @param valueSelector value selector function extraction cache values from elements
 * @param <EL> the element type
 * @param <KT> the cache key type
 * @param <VT> the cache value type
 * @return cache KT -> Set<VT>
 */
private <EL, KT, VT> Map<KT, Set<VT>> initSetCache(Collection<EL> elements, Function<EL, KT> keySelector,
        Function<EL, VT> valueSelector) {

    return elements.stream()
            .collect(Collectors.groupingBy(keySelector, Collectors.mapping(valueSelector, Collectors.toSet())));
}

From source file:org.languagetool.rules.spelling.SpellingCheckRule.java

private void updateIgnoredWordDictionary() {
    wordsToBeIgnoredDictionary = wordsToBeIgnored.stream()
            .collect(Collectors.groupingBy(s -> s.substring(0, 1), Collectors.toSet()));
    wordsToBeIgnoredDictionaryIgnoreCase = wordsToBeIgnored.stream().map(String::toLowerCase)
            .collect(Collectors.groupingBy(s -> s.substring(0, 1), Collectors.toSet()));
}

From source file:org.onosproject.faultmanagement.impl.AlarmManager.java

@Override
public Map<Alarm.SeverityLevel, Long> getAlarmCounts(DeviceId deviceId) {
    return getAlarms(deviceId).stream().collect(Collectors.groupingBy(Alarm::severity, Collectors.counting()));
}

From source file:org.onosproject.faultmanagement.impl.AlarmManager.java

@Override
public Map<Alarm.SeverityLevel, Long> getAlarmCounts() {
    return getAlarms().stream().collect(Collectors.groupingBy(Alarm::severity, Collectors.counting()));
}

From source file:org.onosproject.faultmanagement.impl.AlarmsManager.java

@Override
public Map<Alarm.SeverityLevel, Long> getAlarmCounts(DeviceId deviceId) {

    return getAlarms(deviceId).stream().collect(Collectors.groupingBy(Alarm::severity, Collectors.counting()));

}

From source file:org.onosproject.faultmanagement.impl.AlarmsManager.java

@Override
public Map<Alarm.SeverityLevel, Long> getAlarmCounts() {

    return getAlarms().stream().collect(Collectors.groupingBy(Alarm::severity, Collectors.counting()));
}