Example usage for java.util.stream Collectors counting

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

Introduction

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

Prototype

public static <T> Collector<T, ?, Long> counting() 

Source Link

Document

Returns a Collector accepting elements of type T that counts the number of input elements.

Usage

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()));
}

From source file:org.talend.dataprep.schema.xls.XlsSchemaParser.java

/**
 *
 *
 * @param colId the column id.//from   ww w  .  ja v a2 s . c  o m
 * @param columnRows all rows with previously guessed type: key=row number, value= guessed type
 * @param averageHeaderSize
 * @return
 */
private Type guessColumnType(Integer colId, SortedMap<Integer, String> columnRows, int averageHeaderSize) {

    // calculate number per type

    Map<String, Long> perTypeNumber = columnRows.tailMap(averageHeaderSize).values() //
            .stream() //
            .collect(Collectors.groupingBy(w -> w, Collectors.counting()));

    OptionalLong maxOccurrence = perTypeNumber.values().stream().mapToLong(Long::longValue).max();

    if (!maxOccurrence.isPresent()) {
        return ANY;
    }

    List<String> duplicatedMax = new ArrayList<>();

    perTypeNumber.forEach((type1, aLong) -> {
        if (aLong >= maxOccurrence.getAsLong()) {
            duplicatedMax.add(type1);
        }
    });

    String guessedType;
    if (duplicatedMax.size() == 1) {
        guessedType = duplicatedMax.get(0);
    } else {
        // as we have more than one type we guess ANY
        guessedType = ANY.getName();
    }

    LOGGER.debug("guessed type for column #{} is {}", colId, guessedType);
    return Type.get(guessedType);
}

From source file:org.zanata.rest.service.LocalesService.java

@Transactional(readOnly = true)
@Override//from   ww w.  j  a v  a 2s  .  com
public Response get(@QueryParam("filter") String filter, @QueryParam("sort") String fields,
        @DefaultValue("1") @QueryParam("page") int page,
        @DefaultValue("10") @QueryParam("sizePerPage") int sizePerPage) {
    List<HLocale> locales;
    int totalCount;
    int validatedPageSize = validatePageSize(sizePerPage);
    int offset = (validatePage(page) - 1) * validatedPageSize;
    boolean isAdmin = identity != null && identity.hasRole("admin");
    if (isAdmin) {
        locales = localeServiceImpl.getAllLocales(offset, validatedPageSize, filter,
                convertToSortField(fields));
        totalCount = localeServiceImpl.getLocalesTotalCount(filter);
    } else {
        locales = localeServiceImpl.getSupportedLocales(offset, validatedPageSize, filter,
                convertToSortField(fields));
        totalCount = localeServiceImpl.getSupportedLocalesTotalCount(filter);
    }
    List<LanguageTeamSearchResult> localesRefs = locales.stream()
            .map(hLocale -> LocaleService.convertHLocaleToSearchResultDTO(hLocale))
            .collect(Collectors.toList());
    LocalesResults localesResults = new LocalesResults(totalCount, localesRefs);

    if (isAdmin) {
        List<LocaleId> localeIds = localeDAO.findAll().stream().map(hLocale -> hLocale.getLocaleId())
                .collect(Collectors.toList());
        // Map all requests to localeIds, to prevent multiple requests
        Map<LocaleId, Long> allRequests = requestService
                .getPendingLanguageRequests(localeIds.toArray(new LocaleId[localeIds.size()])).stream()
                .collect(Collectors.groupingBy(languageRequest -> languageRequest.getLocale().getLocaleId(),
                        Collectors.counting()));
        for (LanguageTeamSearchResult searchResult : localesRefs) {
            searchResult.setRequestCount(
                    firstNonNull(allRequests.get(searchResult.getLocaleDetails().getLocaleId()), 0L));
        }
    }
    return Response.ok(localesResults).build();
}