Example usage for java.util.stream Collectors collectingAndThen

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

Introduction

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

Prototype

public static <T, A, R, RR> Collector<T, A, RR> collectingAndThen(Collector<T, A, R> downstream,
        Function<R, RR> finisher) 

Source Link

Document

Adapts a Collector to perform an additional finishing transformation.

Usage

From source file:org.keycloak.models.jpa.JpaRealmProvider.java

@Override
public List<GroupModel> getGroups(RealmModel realm) {
    RealmEntity ref = em.getReference(RealmEntity.class, realm.getId());

    return ref.getGroups().stream().map(g -> session.realms().getGroupById(g.getId(), realm))
            .sorted(Comparator.comparing(GroupModel::getName))
            .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}

From source file:org.keycloak.models.jpa.JpaRealmProvider.java

@Override
public List<GroupModel> getTopLevelGroups(RealmModel realm) {
    RealmEntity ref = em.getReference(RealmEntity.class, realm.getId());

    return ref.getGroups().stream().filter(g -> g.getParent() == null)
            .map(g -> session.realms().getGroupById(g.getId(), realm))
            .sorted(Comparator.comparing(GroupModel::getName))
            .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}

From source file:org.kuali.rice.core.impl.security.PropertySuppressionServiceImpl.java

private static <T, K, U> Collector<T, ?, Map<K, U>> nullSafeToMap(Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper) {
    return Collectors.collectingAndThen(Collectors.toList(), list -> {
        Map<K, U> result = new HashMap<>();
        for (T item : list) {
            K key = keyMapper.apply(item);
            if (result.putIfAbsent(key, valueMapper.apply(item)) != null) {
                throw new IllegalStateException(String.format("Duplicate key %s", key));
            }//  w  w  w .j a  v a 2s .c  om
        }
        return result;
    });
}