@VisibleForTesting static String[] getFrequentLocations(final Stream<String> locations) { final Map<String, Long> locationCountMap = locations.collect( Collectors.groupingBy(location -> location, Collectors.counting()) ); final Comparator<Map.Entry<String, Long>> valueComparator = Map.Entry.comparingByValue(Comparator.reverseOrder()); final Comparator<Map.Entry<String, Long>> keyComparator = Map.Entry.comparingByKey(); return locationCountMap .entrySet().stream() .sorted(valueComparator.thenComparing(keyComparator)) .limit(3) .map(Map.Entry::getKey) .toArray(String[]::new); }
private List<String> packImports() { return mapSimpleNames.entrySet().stream() .filter(ent -> // exclude the current class or one of the nested ones // empty, java.lang and the current packages !setNotImportedNames.contains(ent.getKey()) && !ent.getValue().isEmpty() && !JAVA_LANG_PACKAGE.equals(ent.getValue()) && !ent.getValue().equals(currentPackagePoint) ) .sorted(Map.Entry.<String, String>comparingByValue().thenComparing(Map.Entry.comparingByKey())) .map(ent -> ent.getValue() + "." + ent.getKey()) .collect(Collectors.toList()); } }
@Override public String digestParams(RestInvocation restInvocation) { // Get Parameters Map<String, String> params = restInvocation.getParamsMap().get(FormParam.class).asHttpHeaders(); // TODO: Find More Elegant Solution To Remove Sign // Order By Key Alphabetically, Concancecate Values byte[] unsigned = params .entrySet() .stream() .sorted(Map.Entry.<String, String>comparingByKey()) .filter(e -> !e.getKey().equalsIgnoreCase("sign")) .map(e -> e.getValue()) .collect(Collectors.joining()) .getBytes(); // TODO: Determine Charceter Encoding return String.valueOf(md5.digest(unsigned)); } }
Optional<Map.Entry<LocalDate, List<String>>> first = map .entrySet() .stream() .filter(entry -> entry.getValue().contains("Barry")) .sorted(Map.Entry.comparingByKey()) .findFirst();
@Override public List<HoodieCompactionOperation> orderAndFilter(HoodieWriteConfig writeConfig, List<HoodieCompactionOperation> operations, List<HoodieCompactionPlan> pendingCompactionPlans) { // Iterate through the operations and accept operations as long as we are within the configured target partitions // limit List<HoodieCompactionOperation> filteredList = operations.stream() .collect(Collectors.groupingBy(HoodieCompactionOperation::getPartitionPath)).entrySet().stream() .sorted(Map.Entry.comparingByKey(comparator)).limit(writeConfig.getTargetPartitionsPerDayBasedCompaction()) .flatMap(e -> e.getValue().stream()) .collect(Collectors.toList()); return filteredList; }
@Override public Object execute() throws Exception { serviceDetectorRegistry.getTypes().entrySet().stream() .sorted(Map.Entry.<String, String>comparingByKey()) .forEachOrdered(e -> { System.out.printf("%s: %s\n", e.getKey(), e.getValue()); }); return null; } }
public void dumpHistograms (File dir, String prefix) { metrics.entrySet() .stream() .sorted(Map.Entry.comparingByKey()) .forEach(e -> dumpHistogram (dir, prefix + e.getKey(), e.getValue().copy())); } }
String output = map.entrySet().stream() .sorted(Map.Entry.<String, Integer> comparingByValue() .reversed() .thenComparing(Map.Entry.comparingByKey()) ) .map(entry -> entry.getKey() + " - " + entry.getValue()) .collect(joining());
public Map<String,Histogram> metrics() { return metrics.entrySet() .stream() .sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().copy())); }
@Override public Object execute() throws Exception { serviceDetectorRegistry.getTypes().entrySet().stream() .sorted(Map.Entry.<String, String>comparingByKey()) .forEachOrdered(e -> { System.out.printf("%s: %s\n", e.getKey(), e.getValue()); }); return null; } }
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "code runs in same security context as user who provided input") @Override public void execute(String[] args) throws Exception { Opts opts = new Opts(); opts.parseArgs("accumulo convert-config", args); File xmlFile = new File(opts.xmlPath); if (!xmlFile.exists()) { throw new IllegalArgumentException("xml config file does not exist at " + opts.xmlPath); } Path propsPath = Paths.get(opts.propsPath); if (propsPath.toFile().exists()) { throw new IllegalArgumentException("properties file already exists at " + opts.propsPath); } Configuration xmlConfig = new Configuration(false); xmlConfig.addResource(xmlFile.toURI().toURL()); try (BufferedWriter w = Files.newBufferedWriter(propsPath, Charset.forName("UTF-8"))) { StreamSupport.stream(xmlConfig.spliterator(), false).sorted(Map.Entry.comparingByKey()) .forEach(e -> writeLine(w, e.toString())); } }
Map.Entry<K,V> maxElt = map.entrySet().stream() .max(Map.Entry.comparingByKey()) .orElse(...);
private static List<String> getImportOrder(File importsFile) { try (Stream<String> lines = Files.lines(importsFile.toPath())) { return lines.filter(line -> !line.startsWith("#")) // parse 0=input .map(ImportOrderStep::splitIntoIndexAndName) .sorted(Map.Entry.comparingByKey()) .map(Map.Entry::getValue) .collect(Collectors.toCollection(ArrayList::new)); } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public Collection<ImmutableMetrics> getMetrics() { return positionIndexedMetrics.entrySet().stream().sorted(Map.Entry.comparingByKey()). collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)).values(); }
@Override public String digestParams(RestInvocation restInvocation) { String httpMethod = restInvocation.getHttpMethod(); String host = getHost(restInvocation.getBaseUrl()); String method = "/" + restInvocation.getMethodPath(); String query = Stream.of( restInvocation.getParamsMap().get(FormParam.class), restInvocation.getParamsMap().get(QueryParam.class)) .map(Params::asHttpHeaders) .map(Map::entrySet) .flatMap(Collection::stream) .filter(e -> !"Signature".equals(e.getKey())) .sorted(Map.Entry.comparingByKey()) .map(e -> e.getKey() + "=" + encodeValue(e.getValue())) .collect(Collectors.joining("&")); String toSign = String.format("%s\n%s\n%s\n%s", httpMethod, host, method, query); Mac mac = getMac(); String signature = encodeValue(Base64.getEncoder().encodeToString(mac.doFinal(toSign.getBytes())).trim()); replaceSignatureUrl(restInvocation, signature); return signature; }
public static <K extends Comparable<? super K>, V extends Object> Map<K, V> sortByKeys(Map<K,V> map) { List<Entry<K, V>> sortedEntries = map .entrySet() .stream() .sorted(comparingByKey()) .collect(Collectors.toList()); LinkedHashMap<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : sortedEntries) { Object value = entry.getValue(); if (value instanceof Map) { value = sortByKeys((Map) value); } result.put(entry.getKey(), (V)value); } return result; }
@Override public String digestParams(RestInvocation restInvocation) { String method = restInvocation.getHttpMethod(); String path = stripParams(restInvocation.getPath()); String query = Stream.of( restInvocation.getParamsMap().get(PathParam.class), restInvocation.getParamsMap().get(FormParam.class)) .map(Params::asHttpHeaders) .map(Map::entrySet) .flatMap(Collection::stream) .filter(e -> !"signature".equals(e.getKey())) .sorted(Entry.comparingByKey()) .map(e -> e.getKey() + "=" + e.getValue()) .collect(Collectors.joining("&")); String toSign = String.format("%s|/api/v2/%s|%s", method, path, query); Mac sha256hmac = getMac(); byte[] signed = sha256hmac.doFinal(toSign.getBytes()); String signature = new String(encodeHex(signed)); replaceInvocationUrl(restInvocation, signature); return signature; }
private void printOverriddenProperties(final QueryDescription queryDescription) { final Map<String, Object> overriddenProperties = queryDescription.getOverriddenProperties(); if (overriddenProperties.isEmpty()) { return; } final List<List<String>> rows = overriddenProperties.entrySet().stream() .sorted(Entry.comparingByKey()) .map(prop -> Arrays.asList(prop.getKey(), "", Objects.toString(prop.getValue()))) .collect(Collectors.toList()); new Builder() .withColumnHeaders("Property", "Value") .withRows(rows) .withHeaderLine(String.format( "%n%-20s%n%-20s", "Overridden Properties", "---------------------")) .build() .print(this); }
protected Tuple2<String, Instant> getStatus(List<Instance> instances) { //TODO: Correct is just a second readmodel for groups Map<String, Instant> statusWithTime = instances.stream() .collect(toMap(instance -> instance.getStatusInfo().getStatus(), Instance::getStatusTimestamp, this::getMax )); if (statusWithTime.size() == 1) { Map.Entry<String, Instant> e = statusWithTime.entrySet().iterator().next(); return Tuples.of(e.getKey(), e.getValue()); } if (statusWithTime.containsKey(StatusInfo.STATUS_UP)) { Instant oldestNonUp = statusWithTime.entrySet() .stream() .filter(e -> !StatusInfo.STATUS_UP.equals(e.getKey())) .map(Map.Entry::getValue) .min(naturalOrder()) .orElse(Instant.EPOCH); Instant latest = getMax(oldestNonUp, statusWithTime.getOrDefault(StatusInfo.STATUS_UP, Instant.EPOCH)); return Tuples.of(StatusInfo.STATUS_RESTRICTED, latest); } return statusWithTime.entrySet() .stream() .min(Map.Entry.comparingByKey(StatusInfo.severity())) .map(e -> Tuples.of(e.getKey(), e.getValue())) .orElse(Tuples.of(STATUS_UNKNOWN, Instant.EPOCH)); }
Map<K,V> topTen = map.entrySet().stream() .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())) .limit(10) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));