use of java.util.Spliterator in project streamex by amaembo.
the class DoubleStreamExTest method testBasics.
@Test
public void testBasics() {
assertFalse(DoubleStreamEx.of(1).isParallel());
assertTrue(DoubleStreamEx.of(1).parallel().isParallel());
assertFalse(DoubleStreamEx.of(1).parallel().sequential().isParallel());
AtomicInteger i = new AtomicInteger();
try (DoubleStreamEx s = DoubleStreamEx.of(1).onClose(i::incrementAndGet)) {
assertEquals(1, s.count());
}
assertEquals(1, i.get());
assertEquals(6, IntStreamEx.range(0, 4).asDoubleStream().sum(), 0);
assertEquals(3, IntStreamEx.range(0, 4).asDoubleStream().max().getAsDouble(), 0);
assertEquals(0, IntStreamEx.range(0, 4).asDoubleStream().min().getAsDouble(), 0);
assertEquals(1.5, IntStreamEx.range(0, 4).asDoubleStream().average().getAsDouble(), 0.000001);
assertEquals(4, IntStreamEx.range(0, 4).asDoubleStream().summaryStatistics().getCount());
assertArrayEquals(new double[] { 1, 2, 3 }, IntStreamEx.range(0, 5).asDoubleStream().skip(1).limit(3).toArray(), 0.0);
assertArrayEquals(new double[] { 1, 2, 3 }, DoubleStreamEx.of(3, 1, 2).sorted().toArray(), 0.0);
assertArrayEquals(new double[] { 1, 2, 3 }, DoubleStreamEx.of(1, 2, 1, 3, 2).distinct().toArray(), 0.0);
assertArrayEquals(new int[] { 2, 4, 6 }, IntStreamEx.range(1, 4).asDoubleStream().mapToInt(x -> (int) x * 2).toArray());
assertArrayEquals(new long[] { 2, 4, 6 }, IntStreamEx.range(1, 4).asDoubleStream().mapToLong(x -> (long) x * 2).toArray());
assertArrayEquals(new double[] { 2, 4, 6 }, IntStreamEx.range(1, 4).asDoubleStream().map(x -> x * 2).toArray(), 0.0);
assertArrayEquals(new double[] { 1, 3 }, IntStreamEx.range(0, 5).asDoubleStream().filter(x -> x % 2 == 1).toArray(), 0.0);
assertEquals(6.0, DoubleStreamEx.of(1.0, 2.0, 3.0).reduce(Double::sum).getAsDouble(), 0.0);
assertEquals(Long.MAX_VALUE, LongStreamEx.rangeClosed(1, Long.MAX_VALUE).asDoubleStream().spliterator().getExactSizeIfKnown());
assertArrayEquals(new double[] { 4, 2, 0, -2, -4 }, DoubleStreamEx.zip(new double[] { 5, 4, 3, 2, 1 }, new double[] { 1, 2, 3, 4, 5 }, (a, b) -> a - b).toArray(), 0.0);
assertEquals("1.0; 0.5; 0.25; 0.125", DoubleStreamEx.of(1.0, 0.5, 0.25, 0.125).mapToObj(String::valueOf).joining("; "));
List<Double> list = new ArrayList<>();
DoubleStreamEx.of(1.0, 0.5, 0.25, 0.125).forEach(list::add);
assertEquals(Arrays.asList(1.0, 0.5, 0.25, 0.125), list);
list = new ArrayList<>();
DoubleStreamEx.of(1.0, 0.5, 0.25, 0.125).parallel().forEachOrdered(list::add);
assertEquals(Arrays.asList(1.0, 0.5, 0.25, 0.125), list);
assertFalse(DoubleStreamEx.of(1.0, 2.0, 2.5).anyMatch(x -> x < 0.0));
assertTrue(DoubleStreamEx.of(1.0, 2.0, 2.5).anyMatch(x -> x >= 2.5));
assertTrue(DoubleStreamEx.of(1.0, 2.0, 2.5).noneMatch(x -> x < 0.0));
assertFalse(DoubleStreamEx.of(1.0, 2.0, 2.5).noneMatch(x -> x >= 2.5));
assertEquals(5.0, DoubleStreamEx.of(1.0, 2.0, 2.5).reduce(1, (a, b) -> a * b), 0.0);
assertTrue(DoubleStreamEx.of(1, 2, 3).spliterator().hasCharacteristics(Spliterator.ORDERED));
assertFalse(DoubleStreamEx.of(1, 2, 3).unordered().spliterator().hasCharacteristics(Spliterator.ORDERED));
OfDouble iterator = DoubleStreamEx.of(1.0, 2.0, 3.0).iterator();
assertEquals(1.0, iterator.next(), 0.0);
assertEquals(2.0, iterator.next(), 0.0);
assertEquals(3.0, iterator.next(), 0.0);
assertFalse(iterator.hasNext());
}
use of java.util.Spliterator in project streamex by amaembo.
the class DoubleStreamExTest method testDropWhile.
@Test
public void testDropWhile() {
assertArrayEquals(new double[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, LongStreamEx.range(100).asDoubleStream().dropWhile(i -> i % 10 < 5).limit(10).toArray(), 0.0);
assertEquals(100, LongStreamEx.range(100).asDoubleStream().sorted().dropWhile(i -> i % 10 < 0).count());
assertEquals(0, LongStreamEx.range(100).asDoubleStream().dropWhile(i -> i % 10 < 10).count());
assertEquals(OptionalDouble.of(0), LongStreamEx.range(100).asDoubleStream().dropWhile(i -> i % 10 < 0).findFirst());
assertEquals(OptionalDouble.empty(), LongStreamEx.range(100).asDoubleStream().dropWhile(i -> i % 10 < 10).findFirst());
java.util.Spliterator.OfDouble spltr = LongStreamEx.range(100).asDoubleStream().dropWhile(i -> i % 10 < 1).spliterator();
assertTrue(spltr.tryAdvance((double x) -> assertEquals(1, x, 0.0)));
Builder builder = DoubleStream.builder();
spltr.forEachRemaining(builder);
assertArrayEquals(LongStreamEx.range(2, 100).asDoubleStream().toArray(), builder.build().toArray(), 0.0);
}
use of java.util.Spliterator in project streamex by amaembo.
the class IntStreamExTest method testBasics.
@Test
public void testBasics() {
assertFalse(IntStreamEx.of(1).isParallel());
assertTrue(IntStreamEx.of(1).parallel().isParallel());
assertFalse(IntStreamEx.of(1).parallel().sequential().isParallel());
AtomicInteger i = new AtomicInteger();
try (IntStreamEx s = IntStreamEx.of(1).onClose(i::incrementAndGet)) {
assertEquals(1, s.count());
}
assertEquals(1, i.get());
assertEquals(6, IntStreamEx.range(0, 4).sum());
assertEquals(3, IntStreamEx.range(0, 4).max().getAsInt());
assertEquals(0, IntStreamEx.range(0, 4).min().getAsInt());
assertEquals(1.5, IntStreamEx.range(0, 4).average().getAsDouble(), 0.000001);
assertEquals(4, IntStreamEx.range(0, 4).summaryStatistics().getCount());
assertArrayEquals(new int[] { 1, 2, 3 }, IntStreamEx.range(0, 5).skip(1).limit(3).toArray());
assertArrayEquals(new int[] { 1, 2, 3 }, IntStreamEx.of(3, 1, 2).sorted().toArray());
assertArrayEquals(new int[] { 1, 2, 3 }, IntStreamEx.of(1, 2, 1, 3, 2).distinct().toArray());
assertArrayEquals(new int[] { 2, 4, 6 }, IntStreamEx.range(1, 4).map(x -> x * 2).toArray());
assertArrayEquals(new long[] { 2, 4, 6 }, IntStreamEx.range(1, 4).mapToLong(x -> x * 2).toArray());
assertArrayEquals(new double[] { 2, 4, 6 }, IntStreamEx.range(1, 4).mapToDouble(x -> x * 2).toArray(), 0.0);
assertArrayEquals(new int[] { 1, 3 }, IntStreamEx.range(0, 5).filter(x -> x % 2 == 1).toArray());
assertEquals(6, IntStreamEx.of(1, 2, 3).reduce(Integer::sum).getAsInt());
assertEquals(Integer.MAX_VALUE, IntStreamEx.rangeClosed(1, Integer.MAX_VALUE).spliterator().getExactSizeIfKnown());
assertTrue(IntStreamEx.of(1, 2, 3).spliterator().hasCharacteristics(Spliterator.ORDERED));
assertFalse(IntStreamEx.of(1, 2, 3).unordered().spliterator().hasCharacteristics(Spliterator.ORDERED));
OfInt iterator = IntStreamEx.of(1, 2, 3).iterator();
assertEquals(1, iterator.nextInt());
assertEquals(2, iterator.nextInt());
assertEquals(3, iterator.nextInt());
assertFalse(iterator.hasNext());
List<Integer> list = new ArrayList<>();
IntStreamEx.range(10).parallel().forEachOrdered(list::add);
assertEquals(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), list);
assertTrue(IntStreamEx.empty().noneMatch(x -> true));
assertFalse(IntStreamEx.of(1).noneMatch(x -> true));
assertTrue(IntStreamEx.of(1).noneMatch(x -> false));
}
use of java.util.Spliterator in project streamex by amaembo.
the class LongStreamExTest method testDropWhile.
@Test
public void testDropWhile() {
assertArrayEquals(new long[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, LongStreamEx.range(100).dropWhile(i -> i % 10 < 5).limit(10).toArray());
assertEquals(100, LongStreamEx.range(100).dropWhile(i -> i % 10 < 0).count());
assertEquals(0, LongStreamEx.range(100).dropWhile(i -> i % 10 < 10).count());
assertEquals(OptionalLong.of(0), LongStreamEx.range(100).dropWhile(i -> i % 10 < 0).findFirst());
assertEquals(OptionalLong.empty(), LongStreamEx.range(100).dropWhile(i -> i % 10 < 10).findFirst());
java.util.Spliterator.OfLong spltr = LongStreamEx.range(100).dropWhile(i -> i % 10 < 1).spliterator();
assertTrue(spltr.tryAdvance((long x) -> assertEquals(1, x)));
Builder builder = LongStream.builder();
spltr.forEachRemaining(builder);
assertArrayEquals(LongStreamEx.range(2, 100).toArray(), builder.build().toArray());
}
use of java.util.Spliterator in project myexcel by liaochong.
the class DefaultExcelReader method getAllPictures.
private void getAllPictures(Sheet sheet) {
if (sheet instanceof XSSFSheet) {
isXSSFSheet = true;
XSSFDrawing xssfDrawing = ((XSSFSheet) sheet).getDrawingPatriarch();
if (xssfDrawing == null) {
return;
}
xssfPicturesMap = xssfDrawing.getShapes().stream().filter(s -> s instanceof XSSFPicture).map(s -> (XSSFPicture) s).collect(Collectors.toMap(s -> {
XSSFClientAnchor anchor = (XSSFClientAnchor) s.getAnchor();
return anchor.getRow1() + "_" + anchor.getCol1();
}, s -> s));
} else if (sheet instanceof HSSFSheet) {
HSSFPatriarch hssfPatriarch = ((HSSFSheet) sheet).getDrawingPatriarch();
if (hssfPatriarch == null) {
return;
}
Spliterator<HSSFShape> spliterator = hssfPatriarch.spliterator();
hssfPictureMap = new HashMap<>();
spliterator.forEachRemaining(shape -> {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
HSSFAnchor anchor = picture.getAnchor();
if (anchor instanceof HSSFClientAnchor) {
int row = ((HSSFClientAnchor) anchor).getRow1();
int col = ((HSSFClientAnchor) anchor).getCol1();
hssfPictureMap.put(row + "_" + col, picture);
}
}
});
}
}
Aggregations