@SuppressWarnings("unchecked") @Override public Array<T> sorted(Comparator<? super T> comparator) { final Object[] arr = toArray(this); Arrays.sort(arr, (o1, o2) -> comparator.compare((T) o1, (T) o2)); return wrap(arr); }
private static int testHash() { int errors = 0; Object[] data = new String[] {"perfect", "ham", "THC"}; errors += ((Objects.hash((Object[]) null) == 0) ? 0 : 1); errors += (Objects.hash("perfect", "ham", "THC") == Arrays.hashCode(data)) ? 0 : 1; return errors; }
@Override public int[] toArray() { return performOperation( TerminalFunctions.toArrayIntFunction(), false, (v1, v2) -> { int[] array = Arrays.copyOf(v1, v1.length + v2.length); System.arraycopy(v2, 0, array, v1.length, v2.length); return array; }, null, false); }
/** This uses the public API collect which uses scan under the covers. */ @Test public void testSeedFactory() { Observable<List<Integer>> o = Observable.range(1, 10) .collect( new Supplier<List<Integer>>() { @Override public List<Integer> get() { return new ArrayList<>(); } }, new BiConsumer<List<Integer>, Integer>() { @Override public void accept(List<Integer> list, Integer t2) { list.add(t2); } }) .takeLast(1); assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.toBlocking().single()); assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.toBlocking().single()); }
public class TodaysSales { public static final List<Sale> sales = Arrays.asList( new Sale( Store.KANSAS_CITY, new Date(), Optional.of("Sarah"), Arrays.asList(new Item("carrot", 12.00))), new Sale( Store.ST_LOUIS, new Date(), Optional.empty(), Arrays.asList( new Item("carrot", 12.00), new Item("lizard", 99.99), new Item("cookie", 1.99))), new Sale( Store.ST_LOUIS, new Date(), Optional.of("Jamie"), Arrays.asList(new Item("banana", 3.49), new Item("cookie", 1.49)))); static Stream<Sale> saleStream() { return RandomSale.streamOf(10000); } public static void main(String[] args) { // how many sales? long saleCount = saleStream().count(); System.out.println("Count of sales: " + saleCount); // any sales over $100? Supplier<DoubleStream> totalStream = () -> saleStream().mapToDouble(Sale::total); boolean bigSaleDay = totalStream.get().anyMatch(total -> total > 100.00); System.out.println("Big sale day? " + bigSaleDay); // maximum sale amount? DoubleSummaryStatistics stats = totalStream.get().summaryStatistics(); System.out.println("Max sale amount: " + stats.getMax()); System.out.println("Stats on total: " + stats); // how many items were sold today? Supplier<Stream<Item>> itemStream = () -> saleStream().flatMap(sale -> sale.items.stream()); long itemCount = itemStream.get().count(); System.out.println("Count of items: " + itemCount); // which different items were sold today? String uniqueItems = itemStream.get().map(item -> item.identity).distinct().collect(Collectors.joining(" & ")); System.out.println("Distinct items: " + uniqueItems); // summarize sales by store ConcurrentMap<String, DoubleSummaryStatistics> summary = saleStream() .parallel() .collect( Collectors.groupingByConcurrent( sale -> Thread.currentThread().getName(), Collectors.summarizingDouble(Sale::total))); System.out.println("Summary by thread: " + summary); summary .keySet() .stream() .sorted() .forEach(store -> System.out.println(store + " stats: " + summary.get(store))); } }
/** * Creates a Array of the given elements. * * @param <T> Component type of the Array. * @param elements Zero or more elements. * @return A Array containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SuppressWarnings("varargs") @SafeVarargs public static <T> Array<T> of(T... elements) { Objects.requireNonNull(elements, "elements is null"); return wrap(Arrays.copyOf(elements, elements.length)); }
@Override public Array<T> sorted() { final Object[] arr = toArray(this); Arrays.sort(arr); return wrap(arr); }