Class ByteStream.ByteStreamEx

java.lang.Object
com.landawn.abacus.util.stream.ByteStream
com.landawn.abacus.util.stream.ByteStream.ByteStreamEx
All Implemented Interfaces:
Immutable, BaseStream<Byte,byte[],BytePredicate,ByteConsumer,ByteList,u.OptionalByte,IndexedByte,ByteIterator,ByteStream>, Closeable, AutoCloseable
Enclosing class:
ByteStream

public abstract static class ByteStream.ByteStreamEx extends ByteStream
  • Method Details

    • elementAt

      public u.OptionalByte elementAt(long position)
      Specified by:
      elementAt in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      position -
      Returns:
    • rateLimited

      public ByteStream rateLimited(double permitsPerSecond)
      Specified by:
      rateLimited in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      permitsPerSecond -
      Returns:
      See Also:
    • peek

      public ByteStream peek(ByteConsumer action)
      Specified by:
      peek in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      action -
      Returns:
    • sliding

      public Stream<ByteStream> sliding(int windowSize)
      Specified by:
      sliding in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      windowSize -
      Returns:
      See Also:
    • slidingToList

      public Stream<ByteList> slidingToList(int windowSize)
      Specified by:
      slidingToList in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      windowSize -
      Returns:
      See Also:
    • shuffled

      public ByteStream shuffled()
      Description copied from interface: BaseStream

      This method only runs sequentially, even in parallel stream and all elements will be loaded to memory.
      Specified by:
      shuffled in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Returns:
    • throwIfEmpty

      @SequentialOnly @IntermediateOp public ByteStream throwIfEmpty(Supplier<? extends RuntimeException> exceptionSupplier)
      Specified by:
      throwIfEmpty in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      exceptionSupplier -
      Returns:
    • toImmutableList

      public ImmutableList<Byte> toImmutableList()
      Specified by:
      toImmutableList in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Returns:
    • toImmutableSet

      public ImmutableSet<Byte> toImmutableSet()
      Specified by:
      toImmutableSet in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Returns:
    • join

      public String join(CharSequence delimiter)
      Specified by:
      join in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      delimiter -
      Returns:
    • println

      public void println()
      Specified by:
      println in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
    • transform

      public <SS extends BaseStream> SS transform(Function<? super ByteStream,? extends SS> transfer)
      Specified by:
      transform in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Type Parameters:
      SS -
      Parameters:
      transfer -
      Returns:
    • __

      @Deprecated public <SS extends BaseStream> SS __(Function<? super ByteStream,? extends SS> transfer)
      Deprecated.
      Specified by:
      __ in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Type Parameters:
      SS -
      Parameters:
      transfer -
      Returns:
    • isParallel

      public boolean isParallel()
      Specified by:
      isParallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Returns:
    • sequential

      public ByteStream sequential()
      Specified by:
      sequential in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Returns:
    • parallel

      public ByteStream parallel()
      Description copied from interface: BaseStream
      Consider using sps(Function) if only next operation need to be parallelized. For example:
       stream.parallel().map(f).filter(p)...;
      
       // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream.
       stream.sps(s -> s.map(f)).filter(p)...;
       // Or switch the stream back sequential stream if don't use "sps".
       stream.parallel().map(f).sequential().filter(p)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(Function) is recommended in most of scenarios.
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Returns:
      See Also:
    • parallel

      public ByteStream parallel(int maxThreadNum)
      Description copied from interface: BaseStream
      Consider using sps(int, Function) if only next operation need to be parallelized. For example:
       stream.parallel(maxThreadNum).map(f).filter(p)...;
      
       // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream.
       stream.sps(maxThreadNum, s -> s.map(f)).filter(p)...;
       // Or switch the stream back sequential stream if don't use "sps".
       stream.parallel(maxThreadNum).map(f).sequential().filter(p)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(int, Function) is recommended in most of scenarios.
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      maxThreadNum -
      Returns:
      See Also:
      • #maxThreadNumPerOperation()
    • parallel

      public ByteStream parallel(BaseStream.Splitor splitor)
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      splitor -
      Returns:
      See Also:
    • parallel

      public ByteStream parallel(int maxThreadNum, BaseStream.Splitor splitor)
      Description copied from interface: BaseStream
      Returns an equivalent stream that is parallel. May return itself if the stream was already parallel with the same maxThreadNum and splitor as the specified ones.

      When to use parallel Streams?
       
       Profiler.run(1, 1, 3, "sequential", () -> Stream.of(list).operation(F)...).printResult();
       Profiler.run(1, 1, 3, "parallel", () -> Stream.of(list).parallel().operation(F)...).printResult();
       
       
      Here is a sample performance test with computer: CPU Intel i7-3520M 4-cores 2.9 GHz, JDK 1.8.0_101, Windows 7:
       
      
           public void test_perf() {
              final String[] strs = new String[10_000];
              N.fill(strs, N.uuid());
          
              final int m = 10;
              final Function<String, Long> mapper = str -> {
                  long result = 0;
                  for (int i = 0; i < m; i++) {
                      result += N.sum(str.toCharArray()) + 1;
                  }
                  return result;
              };
          
              final MutableLong sum = MutableLong.of(0);
          
              for (int i = 0, len = strs.length; i < len; i++) {
                  sum.add(mapper.apply(strs[i]));
              }
          
              final int threadNum = 1, loopNum = 100, roundNum = 3;
          
              Profiler.run(threadNum, loopNum, roundNum, "For Loop", () -> {
                  long result = 0;
                  for (int i = 0, len = strs.length; i < len; i++) {
                      result += mapper.apply(strs[i]);
                  }
                  assertEquals(sum.longValue(), result);
              }).printResult();
          
              Profiler.run(threadNum, loopNum, roundNum, "JDK Sequential",
                      () -> assertEquals(sum.longValue(), java.util.stream.Stream.of(strs).map(mapper).mapToLong(e -> e).sum())).printResult();
          
              Profiler.run(threadNum, loopNum, roundNum, "JDK Parallel",
                      () -> assertEquals(sum.longValue(), java.util.stream.Stream.of(strs).parallel().map(mapper).mapToLong(e -> e).sum())).printResult();
          
              Profiler.run(threadNum, loopNum, roundNum, "Abcus Sequential", () -> assertEquals(sum.longValue(), Stream.of(strs).map(mapper).mapToLong(e -> e).sum()))
                      .printResult();
          
              Profiler.run(threadNum, loopNum, roundNum, "Abcus Parallel",
                      () -> assertEquals(sum.longValue(), Stream.of(strs).parallel().map(mapper).mapToLong(e -> e).sum())).printResult();
          
              Profiler.run(threadNum, loopNum, roundNum, "Abcus Parallel by chunck", () -> assertEquals(sum.longValue(),
                      Stream.of(strs).splitToList(100).parallel().map(it -> N.sumLong(it, e -> mapper.apply(e))).mapToLong(e -> e).sum())).printResult();
           }
          
       
       
      And test result: Unit is milliseconds. N(the number of elements) is 10_000, Q(cost per element of F, the per-element function (usually a lambda), here is mapper) is calculated by: value of 'For loop' / N(10_000).
      m = 1 m = 10m = 50m = 100m = 500m = 1000
      Q 0.000020.00020.0010.0020.010.02
      For Loop0.232.31122110219
      JDK Sequential0.282.31122114212
      JDK Parallel0.221.361266122
      Abcus Sequential0.321122112212
      Abcus Parallel1111111677128
      Comparison:
      • Again, do NOT and should NOT use parallel Streams if you don't have any performance problem with sequential Streams, because using parallel Streams has extra cost.
      • Again, consider using parallel Streams only when N(the number of elements) * Q(cost per element of F, the per-element function (usually a lambda)) is big enough.
      • The implementation of parallel Streams in Abacus is more than 10 times, slower than parallel Streams in JDK when Q is tiny(here is less than 0.0002 milliseconds by the test):
        • The implementation of parallel Streams in JDK 8 still can beat the sequential/for loop when Q is tiny(Here is 0.00002 milliseconds by the test). That's amazing, considering the extra cost brought by parallel computation. It's well done.
        • The implementation of parallel Streams in Abacus is pretty simple and straight forward. The extra cost(starting threads/synchronization/queue...) brought by parallel Streams in Abacus is too bigger to tiny Q(Here is less than 0.001 milliseconds by the test). But it starts to be faster than sequential Streams when Q is big enough(Here is 0.001 milliseconds by the test) and starts to catch the parallel Streams in JDK when Q is bigger(Here is 0.01 milliseconds by the test).
        • Consider using the parallel Streams in Abacus when Q is big enough, specially when IO involved in F. Because one IO operation(e.g. DB/web service request..., Reading/Writing file...) usually takes 1 to 1000 milliseconds, or even longer. By the parallel Streams APIs in Abacus, it's very simple to specify max thread numbers. Sometimes, it's much faster to execute IO/Network requests with a bit more threads. It's fair to say that the parallel Streams in Abacus is high efficient, may same as or faster than the parallel Streams in JDK when Q is big enough, except F is heavy cpu-used operation. Most of the times, the Q is big enough to consider using parallel Stream is because IO/Network is involved in F.
      • JDK 7 is supported by the Streams in Abacus. It's perfect to work with retrolambda on Android
      • All primitive types are supported by Stream APIs in Abacus except boolean


      A bit more about Lambdas/Stream APIs, you may heard that Lambdas/Stream APIs is 5 time slower than imperative programming. It's true when Q and F is VERY, VERY tiny, like f = (int a, int b) -> a + b;. But if we look into the samples in the article and think about it: it just takes less than 1 milliseconds to get the max value in 100k numbers. There is potential performance issue only if the "get the max value in 100K numbers" call many, many times in your API or single request. Otherwise, the difference between 0.1 milliseconds to 0.5 milliseconds can be totally ignored. Usually we meet performance issue only if Q and F is big enough. However, the performance of Lambdas/Streams APIs is closed to for loop when Q and F is big enough. No matter in which scenario, We don't need and should not concern the performance of Lambdas/Stream APIs.

      Although it's is parallel Streams, it doesn't means all the methods are executed in parallel. Because the sequential way is as fast, or even faster than the parallel way for some methods, or is pretty difficult, if not possible, to implement the method by parallel approach. Here are the methods which are executed sequentially even in parallel Streams.

      splitXXX/splitAt/splitBy/slidingXXX/collapse, distinct, reverse, rotate, shuffle, indexed, cached, top, kthLargest, count, toArray, toList, toList, toSet, toMultiset, toLongMultiset, intersection(Collection c), difference(Collection c), symmetricDifference(Collection c), forEach(identity, accumulator, predicate), findFirstOrLast, findFirstAndLast
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      maxThreadNum -
      splitor -
      Returns:
      See Also:
      • #maxThreadNumPerOperation()
    • parallel

      public ByteStream parallel(int maxThreadNum, Executor executor)
      Description copied from interface: BaseStream
      // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream. stream.sps(SP.create(maxThreadNum, executor), s -> s.map(f)).filter(p)...; // Or switch the stream back sequential stream if don't use "sps". stream.parallel(maxThreadNum, executor).map(f).sequential().filter(p)...;
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      maxThreadNum -
      executor -
      Returns:
      See Also:
    • parallel

      public ByteStream parallel(Executor executor)
      Description copied from interface: BaseStream
       stream.parallel(executor).map(f).filter(p)...;
      
       // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream.
       stream.sps(SP.create(executor), s -> s.map(f)).filter(p)...;
       // Or switch the stream back sequential stream if don't use "sps".
       stream.parallel(executor).map(f).sequential().filter(p)...;
      
       
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      executor -
      Returns:
      See Also:
    • parallel

      public ByteStream parallel(int maxThreadNum, BaseStream.Splitor splitor, Executor executor)
      Description copied from interface: BaseStream
       stream.parallel(maxThreadNum, splitor, executor).map(f).filter(p)...;
      
       // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream.
       stream.sps(SP.create(maxThreadNum, splitor, executor), s -> s.map(f)).filter(p)...;
       // Or switch the stream back sequential stream if don't use "sps".
       stream.parallel(maxThreadNum, splitor, executor).map(f).sequential().filter(p)...;
      
       
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      maxThreadNum -
      splitor -
      executor -
      Returns:
      See Also:
    • parallel

      Description copied from interface: BaseStream
       stream.parallel(parallelSettings).map(f).filter(p)...;
      
       // Replace above line of code with "sps" if only "f" need to be parallelized. And "p" is fast enough to be executed in sequential Stream.
       stream.sps(SP.create(parallelSettings), s -> s.map(f)).filter(p)...;
       // Or switch the stream back sequential stream if don't use "sps".
       stream.parallel(parallelSettings).map(f).sequential().filter(p)...;
      
       
      Specified by:
      parallel in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Parameters:
      ps -
      Returns:
      See Also:
    • sps

      public <SS extends BaseStream> SS sps(Function<? super ByteStream,? extends SS> ops)
      Description copied from interface: BaseStream
      Temporarily switch the stream to parallel stream for operation ops and then switch back to sequence stream.
      stream().parallel().ops(map/filter/...).sequence()
      Specified by:
      sps in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Type Parameters:
      SS -
      Parameters:
      ops -
      Returns:
    • sps

      public <SS extends BaseStream> SS sps(int maxThreadNum, Function<? super ByteStream,? extends SS> ops)
      Description copied from interface: BaseStream
      Temporarily switch the stream to parallel stream for operation ops and then switch back to sequence stream.
      stream().parallel(maxThreadNum).ops(map/filter/...).sequence()
      Specified by:
      sps in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Type Parameters:
      SS -
      Parameters:
      maxThreadNum -
      ops -
      Returns:
    • psp

      public <SS extends BaseStream> SS psp(Function<? super ByteStream,? extends SS> ops)
      Description copied from interface: BaseStream
      Temporarily switch the stream to sequence stream for operation ops and then switch back to parallel stream with same maxThreadNum/splitor/asyncExecutor.
      stream().sequence().ops(map/filter/...).parallel(sameMaxThreadNum, sameSplitor, sameAsyncExecutor)
      Specified by:
      psp in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Type Parameters:
      SS -
      Parameters:
      ops -
      Returns:
    • toArray

      public byte[] toArray()
      Specified by:
      toArray in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Returns:
    • close

      public void close()
      Description copied from interface: BaseStream
      It will be called by terminal operations in final.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface BaseStream<T,A,P,C,PL,OT,IT,ITER extends Iterator<T>,S extends com.landawn.abacus.util.stream.StreamBase<T,A,P,C,PL,OT,IT,ITER,S>>
      Specified by:
      close in interface Closeable