@Override public Comparator<Integer> getComparator() { return Comparator.reverseOrder(); } }
private static List<String> reverseSort(String[] strings) { final String[] copy = new String[strings.length]; System.arraycopy(strings, 0, copy, 0, strings.length); Arrays.sort(copy, Comparator.reverseOrder()); return Arrays.asList(copy); }
Map<K,V> topTen = map.entrySet().stream() .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())) .limit(10) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
@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); }
@Benchmark public int parallel_lazy_reverse_jdk() { return this.integersJDK.parallelStream().min(Comparator.<Integer>reverseOrder()).get(); }
@Benchmark public int serial_lazy_reverse_streams_gsc() { return this.integersGSC.stream().max(Comparator.<Integer>reverseOrder()).get(); }
@Benchmark public int parallel_lazy_reverse_streams_gsc() { return this.integersGSC.parallelStream().max(Comparator.<Integer>reverseOrder()).get(); }
@Benchmark public int serial_lazy_reverse_streams_gsc() { return this.integersGSC.stream().min(Comparator.<Integer>reverseOrder()).get(); }
@Benchmark public int parallel_lazy_reverse_streams_gsc() { return this.integersGSC.parallelStream().min(Comparator.<Integer>reverseOrder()).get(); }
@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); } }
@BeforeAll public static void beforeAll() throws Exception { if (BASE_DIR.exists()) { // clean up all subdirs & files Files.walk(BASE_DIR.toPath(),FileVisitOption.FOLLOW_LINKS) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .peek(System.out::println) .forEach(File::delete); } // created directory is needed for tests BASE_DIR.mkdirs(); }
@BeforeAll static void beforeAll() throws Exception { if (BASE_DIR.exists()) { // clean up all subdirs & files Files.walk(BASE_DIR.toPath(), FileVisitOption.FOLLOW_LINKS) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .peek(System.out::println) .forEach(File::delete); } // created directory is needed for tests BASE_DIR.mkdirs(); }
@BeforeAll public static void beforeAll() throws Exception { if (BASE_DIR.exists()) { // clean up all subdirs & files Files.walk(BASE_DIR.toPath(), FileVisitOption.FOLLOW_LINKS) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .peek(System.out::println) .forEach(File::delete); } // created directory is needed for tests BASE_DIR.mkdirs(); }
@Benchmark public int serial_lazy_reverse_gsc() { return this.integersGSC.asLazy().max(Comparator.<Integer>reverseOrder()); }
@Benchmark public int serial_lazy_reverse_gsc() { return this.integersGSC.asLazy().min(Comparator.<Integer>reverseOrder()); }
@Benchmark public int parallel_lazy_reverse_gsc() { return this.integersGSC.asParallel(this.executorService, BATCH_SIZE).max(Comparator.<Integer>reverseOrder()); } }
@Test(dataProvider = "snapshot") public void snapshot(boolean ascending, int limit, long nanos, Function<Long, Long> transformer) { int count = 21; timerWheel.nanos = nanos; int expected = Math.min(limit, count); Comparator<Long> order = ascending ? Comparator.naturalOrder() : Comparator.reverseOrder(); List<Long> times = IntStream.range(0, count).mapToLong(i -> { long time = nanos + TimeUnit.SECONDS.toNanos(2 << i); timerWheel.schedule(new Timer(time)); return time; }).boxed().sorted(order).collect(toList()).subList(0, expected); when(transformer.apply(anyLong())).thenAnswer(invocation -> invocation.getArgument(0)); assertThat(snapshot(ascending, limit, transformer), is(times)); verify(transformer, times(expected)).apply(anyLong()); }
@Test public void aFluxCanBeSorted2(){ List<Integer> vals = Flux.just(1, 2, 3, 4) .collectSortedList(Comparator.reverseOrder()) .block(); assertThat(vals).containsExactly(4,3,2,1); }
@Test public void aFluxCanBeSorted3(){ StepVerifier.create(Flux.just(43, 32122, 422, 321, 43, 443311) .sort(Comparator.reverseOrder())) .expectNext(443311, 32122, 422, 321, 43, 43) .verifyComplete(); }