public static Artist byCollecting(List<Artist> artists) { return artists.stream() .collect(Collectors.maxBy(byNameLength)) .orElseThrow(RuntimeException::new); }
public Optional<Artist> biggestGroup(Stream<Artist> artists) { Function<Artist,Long> getCount = artist -> artist.getMembers().count(); return artists.collect(maxBy(comparing(getCount))); } // END BIGGEST_GROUP
@Benchmark public Position maxByMarketValue_serial_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.maxBy(MARKET_VALUE_COMPARATOR_LAMBDA)).get(); }
@Benchmark public Position maxByQuantity_serial_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.maxBy(QUANTITY_COMPARATOR_LAMBDA)).get(); }
@Benchmark public Position maxByQuantity_serial_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.maxBy(QUANTITY_COMPARATOR_METHODREF)).get(); }
@Benchmark public Position maxByMarketValue_serial_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.maxBy(MARKET_VALUE_COMPARATOR_METHODREF)).get(); }
/** * Get all failed and cancelled tasks. * for failed tasks - get one for each task reference name(latest failed using seq id) * * @param workflow * @return list of latest failed tasks, one for each task reference reference type. */ @VisibleForTesting List<Task> getFailedTasksToRetry(Workflow workflow) { return workflow.getTasks().stream() .filter(x -> FAILED.equals(x.getStatus())) .collect(groupingBy(Task::getReferenceTaskName, maxBy(comparingInt(Task::getSeq)))) .values().stream() .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); }
@Benchmark public Position maxByQuantity_parallel_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.maxBy(QUANTITY_COMPARATOR_LAMBDA)).get(); }
@Benchmark public Position maxByQuantity_parallel_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.maxBy(QUANTITY_COMPARATOR_METHODREF)).get(); }
@Benchmark public Position maxByMarketValue_parallel_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.maxBy(MARKET_VALUE_COMPARATOR_METHODREF)).get(); }
@Benchmark public Position maxByMarketValue_parallel_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.maxBy(MARKET_VALUE_COMPARATOR_LAMBDA)).get(); }
@GET @RequiresPermissions(RestPermissions.USERS_LIST) @ApiOperation(value = "List all users", notes = "The permissions assigned to the users are always included.") public UserList listUsers() { final List<User> users = userService.loadAll(); final Collection<MongoDbSession> sessions = sessionService.loadAll(); // among all active sessions, find the last recently used for each user //noinspection OptionalGetWithoutIsPresent final Map<String, Optional<MongoDbSession>> lastSessionForUser = sessions.stream() .filter(s -> s.getUsernameAttribute().isPresent()) .collect(groupingBy(s -> s.getUsernameAttribute().get(), maxBy(Comparator.comparing(MongoDbSession::getLastAccessTime)))); final List<UserSummary> resultUsers = Lists.newArrayListWithCapacity(users.size() + 1); final User adminUser = userService.getAdminUser(); resultUsers.add(toUserResponse(adminUser, lastSessionForUser.getOrDefault(adminUser.getName(), Optional.empty()))); for (User user : users) { resultUsers.add(toUserResponse(user, lastSessionForUser.getOrDefault(user.getName(), Optional.empty()))); } return UserList.create(resultUsers); }
public final static <T> Optional<T> max(final Stream<T> stream, final Comparator<? super T> comparator) { return stream.collect(java.util.stream.Collectors.maxBy(comparator)); }
public static void main(String[] args) { List<Project> projects = Project.buildData(); Collection<Project> collect = projects.stream() .collect(toCollection(CopyOnWriteArrayList::new)); System.out.println(collect); Map<String, Project> collect1 = projects.stream() .collect(groupingBy(Project::getAuthor, collectingAndThen( maxBy(Comparator.comparingInt(Project::getStars)), Optional::get ))); System.out.println(collect1); } }
public ImageMetadata getMaxResolutionImage() { return componentMetadata.stream() .filter(metadata -> metadata instanceof ImageMetadata) .map(metadata -> (ImageMetadata) metadata) .collect(Collectors.maxBy(Comparator.comparingInt(ImageMetadata::getRasterWidth))) .get(); }
public int getSceneHeight() { return componentMetadata.stream() .filter(metadata -> metadata instanceof ImageMetadata) .map(metadata -> (ImageMetadata) metadata) .map(ImageMetadata::getRasterHeight) .collect(Collectors.maxBy(Integer::compare)) .get(); }
private String calcLastServiceArrivalTime(String routeId, Map<String, List<GtfsRecord>> tripsOfRoute) { List<GtfsRecord> allTrips = tripsOfRoute.get(routeId); String lastArrival = allTrips.stream() .map(gtfsRecord -> gtfsRecord.getLastStopTime().getArrivalTime()) .collect(Collectors.maxBy(Comparator.naturalOrder())).orElse("23:59"); return lastArrival; }
public Optional<ValidationErrorLevel> findNestedErrorsMaxLevel(FeedbackFence feedbackFence, IPredicate<ValidationError> filter) { return collectNestedErrors(feedbackFence, resolveRootInstances(feedbackFence.getMainContainer()), filter).stream() .map(ValidationError::getErrorLevel) .collect(Collectors.maxBy(Comparator.naturalOrder())); }
public Optional<ValidationErrorLevel> findNestedErrorsMaxLevel(FeedbackFence feedbackFence, IPredicate<IValidationError> filter) { return collectNestedErrors(feedbackFence, resolveRootInstances(feedbackFence.getMainContainer()), filter).stream() .map(IValidationError::getErrorLevel) .collect(Collectors.maxBy(Comparator.naturalOrder())); }
private Mono<Integer> saveBlobParts(byte[] data, BlobId blobId) { Stream<Pair<Integer, ByteBuffer>> chunks = dataChunker.chunk(data, configuration.getBlobPartSize()); return Flux.fromStream(chunks) .publishOn(Schedulers.elastic(), PREFETCH) .flatMap(pair -> writePart(pair.getValue(), blobId, getChunkNum(pair))) .collect(Collectors.maxBy(Comparator.comparingInt(x -> x))) .flatMap(Mono::justOrEmpty) .map(this::numToCount) .defaultIfEmpty(0); }