Optional<ServerName> getLargestQueueFromServersNotCompacting() { lock.readLock().lock(); try { return compactionQueues.entrySet().stream() .filter(entry -> !compactingServers.contains(entry.getKey())) .max(Map.Entry.comparingByValue( (o1, o2) -> Integer.compare(o1.size(), o2.size()))).map(Map.Entry::getKey); } finally { lock.readLock().unlock(); } }
@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); }
protected Map filterBySize(Map<String, Integer> values, Integer limit, List<String> baseKeys) { Map<String, Integer> toFilter = values.entrySet().stream() .filter(e -> !baseKeys.contains(e.getKey())) .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); Map<String, Integer> retVal = values.entrySet().stream() .filter((e -> baseKeys.contains(e.getKey()))) .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); List<Map.Entry<String, Integer>> _flat = new ArrayList<>(toFilter.entrySet()); _flat.sort(Map.Entry.comparingByValue()); _flat = Lists.reverse(_flat); for (int index = 0; index < _flat.size() && index < limit; index++) { retVal.put(_flat.get(index).getKey(), _flat.get(index).getValue()); } return retVal; } }
Comparator<Map.Entry<Integer, String>> com2 = Map.Entry.comparingByValue(Comparator.reverseOrder());
Comparator<Map.Entry<Integer, String>> com2 = Map.Entry.<Integer, String>comparingByValue().reversed();
map.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .forEach(entry -> ... );
import static java.util.Map.Entry.comparingByValue; import static java.util.stream.Collectors.toList; <K, V> List<Entry<K, V>> sort(Map<K, V> map, Comparator<? super V> comparator) { return map.entrySet().stream().sorted(comparingByValue(comparator)).collect(toList()); }
/** * Perform Roulette Wheel selection * * @return Chromosome parent chosen after 'spinning' the wheel. */ @Override public Chromosome execute() throws IgniteException { IgniteCache<Long, Chromosome> populationCache = ignite.cache(GAGridConstants.POPULATION_CACHE); int value = spintheWheel(this.totalFitnessScore); double partialSum = 0; boolean notFound = true; //sort map in ascending order by fitness score Map<Long, Double> sortedAscendingMap = map.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); Iterator<Entry<Long, Double>> entries = sortedAscendingMap.entrySet().iterator(); Long chromosomeKey = (long)-1; while (entries.hasNext() && notFound) { Entry<Long, Double> entry = entries.next(); Long key = entry.getKey(); Double fitnessScore = entry.getValue(); partialSum = partialSum + fitnessScore; if (partialSum >= value) { notFound = false; chromosomeKey = key; } } return populationCache.get(chromosomeKey); }
public void addChild(final CommandNode<S> node) { if (node instanceof RootCommandNode) { throw new UnsupportedOperationException("Cannot add a RootCommandNode as a child to any other CommandNode"); } final CommandNode<S> child = children.get(node.getName()); if (child != null) { // We've found something to merge onto if (node.getCommand() != null) { child.command = node.getCommand(); } for (final CommandNode<S> grandchild : node.getChildren()) { child.addChild(grandchild); } } else { children.put(node.getName(), node); if (node instanceof LiteralCommandNode) { literals.put(node.getName(), (LiteralCommandNode<S>) node); } else if (node instanceof ArgumentCommandNode) { arguments.put(node.getName(), (ArgumentCommandNode<S, ?>) node); } } children = children.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); }
/** * Transforms frequencies to the encoding values. * * @param frequencies Frequencies of categories for the specific feature. * @return Encoding values. */ private Map<String, Integer> transformFrequenciesToEncodingValues(Map<String, Integer> frequencies) { Comparator<Map.Entry<String, Integer>> comp; if (encoderSortingStgy.equals(EncoderSortingStrategy.FREQUENCY_DESC)) comp = Map.Entry.comparingByValue(); else comp = Collections.reverseOrder(Map.Entry.comparingByValue()); final HashMap<String, Integer> resMap = frequencies.entrySet() .stream() .sorted(comp) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); int amountOfLabels = frequencies.size(); for (Map.Entry<String, Integer> m : resMap.entrySet()) m.setValue(--amountOfLabels); return resMap; }
/** {@inheritDoc} */ @Override public List<Entry<BytesRef, Integer>> getPopularityData(final int page, final int pageSize) { if (page < 0) { throw new IllegalArgumentException("Cannot retrieve popularity data for negative page: " + page); } if (pageSize < 0) { throw new IllegalArgumentException("Cannot retrieve negative number of results: " + pageSize); } List<Entry<BytesRef, Integer>> list = new ArrayList<>(map.entrySet()); list.sort(Entry.<BytesRef, Integer>comparingByValue().reversed()); int startIndex = page * pageSize; if (startIndex >= list.size()) { return Collections.emptyList(); } int endIndex = startIndex + pageSize; if (endIndex > list.size()) { endIndex = list.size(); } return list.subList(startIndex, endIndex); }
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()); } }
map.entrySet().stream() .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) .limit(10) .forEach(System.out::println); // or any other terminal method
/** */ protected double getClassWithMaxVotes(Map<Double, Double> clsVotes) { return Collections.max(clsVotes.entrySet(), Map.Entry.comparingByValue()).getKey(); }
@GuardedBy("this") private String getAdditionalFailureInfo(long allocated, long delta) { Map<String, Long> queryAllocations = memoryPool.getTaggedMemoryAllocations().get(queryId); String additionalInfo = format("Allocated: %s, Delta: %s", succinctBytes(allocated), succinctBytes(delta)); // It's possible that a query tries allocating more than the available memory // failing immediately before any allocation of that query is tagged if (queryAllocations == null) { return additionalInfo; } String topConsumers = queryAllocations.entrySet().stream() .sorted(comparingByValue(Comparator.reverseOrder())) .limit(3) .collect(toImmutableMap(Entry::getKey, e -> succinctBytes(e.getValue()))) .toString(); return format("%s, Top Consumers: %s", additionalInfo, topConsumers); } }
@GuardedBy("this") private String getAdditionalFailureInfo(long allocated, long delta) { Map<String, Long> queryAllocations = memoryPool.getTaggedMemoryAllocations().get(queryId); String additionalInfo = format("Allocated: %s, Delta: %s", succinctBytes(allocated), succinctBytes(delta)); // It's possible that a query tries allocating more than the available memory // failing immediately before any allocation of that query is tagged if (queryAllocations == null) { return additionalInfo; } String topConsumers = queryAllocations.entrySet().stream() .sorted(comparingByValue(Comparator.reverseOrder())) .limit(3) .collect(toImmutableMap(Entry::getKey, e -> succinctBytes(e.getValue()))) .toString(); return format("%s, Top Consumers: %s", additionalInfo, topConsumers); } }
Stream<Map.Entry<K,V>> sorted = map.entrySet().stream() .sorted(Map.Entry.comparingByValue());
map.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .forEach(System.out::println);
Stream<Map.Entry<K,V>> sorted = map.entrySet().stream() .sorted(Map.Entry.comparingByValue(comparator));
Stream<Map.Entry<K,V>> sorted = map.entrySet().stream() .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));