Refine search
canonical example by Tabnine
canonical example by Tabnine
private static void testLimit() { System.out.println(); System.out.println("Test limit start"); Collection<String> collection = Arrays.asList("a1", "a2", "a3", "a1"); // Get the first two elements List<String> limit = collection.stream().limit(2).collect(Collectors.toList()); System.out.println("limit = " + limit); // print limit = [a1, a2] // Get two elements from second element List<String> fromTo = collection.stream().skip(1).limit(2).collect(Collectors.toList()); System.out.println("fromTo = " + fromTo); // print fromTo = [a2, a3] // Get the last element String last = collection.stream().skip(collection.size() - 1).findAny().orElse("1"); System.out.println("last = " + last ); // print last = a1 }
/** This method generates "random double, integer" pairs, sort them, gets first "testSize" elements and returns appropriate indices */ @NotNull private TreeSet<Integer> getSortedIndices(int datasetSize, int testSize) { Random rnd = new Random(); TreeMap<Double, Integer> randomIdxPairs = new TreeMap<>(); for (int i = 0; i < datasetSize; i++) randomIdxPairs.put(rnd.nextDouble(), i); final TreeMap<Double, Integer> testIdxPairs = randomIdxPairs.entrySet().stream() .limit(testSize) .collect(TreeMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), Map::putAll); return new TreeSet<>(testIdxPairs.values()); }
@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 static String computeMessage(String dataWording, Set<Component> filesWithErrors) { if (filesWithErrors.size() == 1) { Component file = filesWithErrors.iterator().next(); return format("Inconsistent %s data detected on file '%s'. " + "File source may have been modified while analysis was running.", dataWording, file.getName()); } String lineHeader = "\n ° "; return format("Inconsistent %s data detected on some files (%s in total). " + "File source may have been modified while analysis was running.", dataWording, filesWithErrors.size()) + filesWithErrors.stream() .sorted(COMPONENT_COMPARATOR) .limit(5) .map(Component::getName) .collect(joining(lineHeader, lineHeader, "")); }
/** * Build the diagnostic message. * * <p>Examples: * * <ul> * <li>Non-exhaustive switch, expected cases for: FOO * <li>Non-exhaustive switch, expected cases for: FOO, BAR, BAZ, and 42 others. * </ul> */ private String buildMessage(Set<String> unhandled) { StringBuilder message = new StringBuilder( "Non-exhaustive switch; either add a default or handle the remaining cases: "); int numberToShow = unhandled.size() > MAX_CASES_TO_PRINT ? 3 // if there are too many to print, only show three examples. : unhandled.size(); message.append(unhandled.stream().limit(numberToShow).collect(Collectors.joining(", "))); if (numberToShow < unhandled.size()) { message.append(String.format(", and %d others", unhandled.size() - numberToShow)); } return message.toString(); } }
/** * Get the last N elements as a new list. * * @param list list to get * @param count element count to get * @return the first element. null if list is null. * elements in a new list may be less than count if there're not enough elements in the list. */ public static <T> List<T> takeLast(List<T> list, int count) { if (list == null) { return null; } if (list.size() <= count) { return list; } else { return list.stream() .skip(list.size() - count) .limit(count) .collect(Collectors.toList()); } }
private static List<Map.Entry<String, MetricStatsInt>> fiveBiggest(DistributedMetricStatsInt distributedMetricStatsInt, ToIntFunction<MetricStatsInt> biggerCriteria) { Comparator<Map.Entry<String, MetricStatsInt>> comparator = Comparator.comparingInt(a -> biggerCriteria.applyAsInt(a.getValue())); return distributedMetricStatsInt.getForLabels() .entrySet() .stream() .sorted(comparator.reversed()) .limit(5) .collect(MoreCollectors.toList(5)); }