List of usage examples for java.util.stream Collectors maxBy
public static <T> Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator)
From source file:Main.java
public static void main(String[] args) { Stream<String> s = Stream.of("1", "2", "3"); Optional<String> o = s.collect(Collectors.maxBy(Comparator.reverseOrder())); if (o.isPresent()) { System.out.println(o.get()); } else {//from w w w .ja va 2 s .co m System.out.println("no value"); } }
From source file:Main.java
public static void main(String... args) { Map<Boolean, Food> o = Food.menu.stream().collect(Collectors.partitioningBy(Food::isVegetarian, Collectors .collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Food::getCalories)), Optional::get))); System.out.println(o);//from w w w. j a v a2 s . com }
From source file:delfos.rs.trustbased.WeightedGraph.java
public boolean hasAnyEdge() { Optional<PathBetweenNodes<Node>> longestPath = allNodes().parallelStream() .flatMap(user1 -> allNodes().parallelStream().map(user2 -> shortestPath(user1, user2))) .filter(optionalPath -> optionalPath.isPresent()).map(optionalPath -> optionalPath.get()) .filter(path -> !path.isSelf()).collect(Collectors.maxBy(PathBetweenNodes.FURTHEST_PATH())); return longestPath.isPresent(); }
From source file:com.formkiq.core.service.generator.pdfbox.PdfEditorServiceImpl.java
/** * Remove {@link PdfFieldMatch} {@link PdfTextFieldMatch} that occur in * the {@link PdAnnotationWidgetMatch}.//from w w w. j av a 2 s .c o m * * @param fm {@link PdfFieldMatch} */ private void updatePDCheckBoxPdfTextFieldMatch(final PdfFieldMatch fm) { Optional<Float> maxX = fm.getWidgets().stream() .map(s -> Float.valueOf(s.getWidget().getRectangle().getUpperRightX())) .collect(Collectors.maxBy(Float::compareTo)); List<PdfTextFieldMatch> list = fm.getWidgets().stream().flatMap(s -> s.getMatches().stream()) .collect(Collectors.toList()); Set<PdfTextField> set = list.stream().map(s -> s.getTextField()).collect(Collectors.toSet()); Iterator<PdfTextFieldMatch> itr = fm.getMatches().iterator(); while (itr.hasNext()) { PdfTextFieldMatch m = itr.next(); if (m.getMatch() < 0 || set.contains(m.getTextField())) { itr.remove(); } else if (maxX.isPresent()) { if (maxX.get().floatValue() < m.getTextField().getRectangle().getLowerLeftX()) { itr.remove(); } } } Collections.sort(list, this.matchComparator); }
From source file:org.apache.james.blob.cassandra.CassandraBlobsDAO.java
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); }
From source file:org.opencb.commons.utils.CommandLineUtils.java
public static void printCommandUsage(JCommander commander, PrintStream printStream) { Integer paramNameMaxSize = Math.max(commander.getParameters().stream().map(pd -> pd.getNames().length()) .collect(Collectors.maxBy(Comparator.naturalOrder())).orElse(20), 20); Integer typeMaxSize = Math.max(commander.getParameters().stream().map(pd -> getType(pd).length()) .collect(Collectors.maxBy(Comparator.naturalOrder())).orElse(10), 10); int nameAndTypeLength = paramNameMaxSize + typeMaxSize + 8; int maxLineLength = nameAndTypeLength + DESCRIPTION_LENGTH; //160 Comparator<ParameterDescription> parameterDescriptionComparator = Comparator .comparing((ParameterDescription p) -> p.getDescription().contains("DEPRECATED")) .thenComparing(ParameterDescription::getLongestName); commander.getParameters().stream().sorted(parameterDescriptionComparator).forEach(parameterDescription -> { if (parameterDescription.getParameter() != null && !parameterDescription.getParameter().hidden()) { String type = getType(parameterDescription); String defaultValue = ""; if (parameterDescription.getDefault() != null) { if (parameterDescription.isDynamicParameter()) { Object def = parameterDescription.getDefault(); if (def instanceof Map && !((Map) def).isEmpty()) { defaultValue = " [" + def + "]"; }// w w w . ja v a2 s . c om } else { defaultValue = " [" + parameterDescription.getDefault() + "]"; } } String usage = String.format("%5s %-" + paramNameMaxSize + "s %-" + typeMaxSize + "s %s%s\n", (parameterDescription.getParameterized().getParameter() != null && parameterDescription.getParameterized().getParameter().required()) ? "*" : "", parameterDescription.getNames(), type, parameterDescription.getDescription(), defaultValue); // if lines are longer than the maximum they are trimmed and printed in several lines List<String> lines = new LinkedList<>(); while (usage.length() > maxLineLength + 1) { int splitPosition = Math.min(1 + usage.lastIndexOf(" ", maxLineLength), usage.length()); if (splitPosition <= nameAndTypeLength + DESCRIPTION_INDENT) { splitPosition = Math.min(1 + usage.indexOf(" ", maxLineLength), usage.length()); } lines.add(usage.substring(0, splitPosition) + "\n"); usage = String.format("%" + (nameAndTypeLength + DESCRIPTION_INDENT) + "s", "") + "" + usage.substring(splitPosition); } // this is empty for short lines and so no prints anything lines.forEach(printStream::print); // in long lines this prints the last trimmed line printStream.print(usage); } }); }
From source file:org.opensingular.form.wicket.SValidationFeedbackHandler.java
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())); }