Class FloatStream.FloatStreamEx

java.lang.Object
com.landawn.abacus.util.stream.FloatStream
com.landawn.abacus.util.stream.FloatStream.FloatStreamEx
All Implemented Interfaces:
Immutable, BaseStream<Float,float[],FloatPredicate,FloatConsumer,FloatList,u.OptionalFloat,IndexedFloat,FloatIterator,FloatStream>, Closeable, AutoCloseable
Enclosing class:
FloatStream

public abstract static class FloatStream.FloatStreamEx extends FloatStream
  • Method Details

    • elementAt

      public u.OptionalFloat 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 - in current stream(not upstream or origin source). It starts from 0.
      Returns:
    • rateLimited

      public FloatStream 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>>
      Returns:
      See Also:
    • peek

      public FloatStream peek(FloatConsumer 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>>
    • sliding

      public Stream<FloatStream> 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>>
      Returns:
      See Also:
    • slidingToList

      public Stream<FloatList> 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>>
      Returns:
      See Also:
    • shuffled

      public FloatStream 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 FloatStream 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>>
    • toImmutableList

      public ImmutableList<Float> 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>>
    • toImmutableSet

      public ImmutableSet<Float> 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>>
    • 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>>
    • 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>>
    • __

      public <SS extends BaseStream> SS __(Function<? super FloatStream,? extends SS> transfer)
      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>>
    • 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>>
    • sequential

      public FloatStream 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>>
    • parallel

      public FloatStream 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 FloatStream 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>>
      Returns:
      See Also:
    • parallel

      public FloatStream parallel(BaseStream.Splitor splitor)
      Description copied from interface: BaseStream
      Consider using sps(ParallelSettings, Function) if only next operation need to be parallelized. For example:
       stream.parallel(splitor).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(splitor), s -> s.map(f)).filter(p)...;
       // Or switch the stream back sequential stream if don't use "sps".
       stream.parallel(splitor).map(f).sequential().filter(p)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(ParallelSettings, 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 FloatStream 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 - Default value is the number of cpu-cores. Steps/operations will be executed sequentially if maxThreadNum is 1.
      splitor - The target array is split by ranges for multiple threads if splitor is splitor.ARRAY and target stream composed by array. It looks like:
      
       for (int i = 0; i < maxThreadNum; i++) {
           final int sliceIndex = i;
      
           futureList.add(asyncExecutor.execute(new Runnable() {
               public void run() {
                   int cursor = fromIndex + sliceIndex * sliceSize;
                   final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
                   while (cursor < to) {
                       action.accept(elements[cursor++]);
                   }
              }
          }));
       }
       
      Otherwise, each thread will get the elements from the target array/iterator in the stream one by one with the target array/iterator synchronized. It looks like:
      
       for (int i = 0; i < maxThreadNum; i++) {
           futureList.add(asyncExecutor.execute(new Runnable() {
               public void run() {
                   T next = null;
      
                   while (true) {
                       synchronized (elements) {
                           if (cursor.intValue() < toIndex) {
                               next = elements[cursor.getAndIncrement()];
                           } else {
                               break;
                           }
                       }
      
                       action.accept(next);
                   }
               }
           }));
       }
       
      Using splitor.ARRAY only when F (the per-element function (usually a lambda)) is very tiny and the cost of synchronization on the target array/iterator is too big to it. For the F involving IO or taking 'long' to complete, choose splitor.ITERATOR. Default value is splitor.ITERATOR.

      Consider using sps(ParallelSettings, Function) if only next operation need to be parallelized. For example:
       stream.parallel(maxThreadNum, splitor).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), s -> s.map(f)).filter(p)...;
       // Or switch the stream back sequential stream if don't use "sps".
       stream.parallel(maxThreadNum, splitor).map(f).sequential().filter(p)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(ParallelSettings, Function) is recommended in most of scenarios.
      Returns:
      See Also:
    • parallel

      public FloatStream parallel(int maxThreadNum, Executor executor)
      Description copied from interface: BaseStream
      Consider using sps(ParallelSettings, Function) if only next operation need to be parallelized. For example:
       stream.parallel(maxThreadNum, 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,  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)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(ParallelSettings, 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>>
      executor - should be able to execute maxThreadNum * following up operations in parallel.
      Returns:
      See Also:
    • parallel

      public FloatStream parallel(Executor executor)
      Description copied from interface: BaseStream
      Consider using sps(ParallelSettings, Function) if only next operation need to be parallelized. For example:
       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)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(ParallelSettings, 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:
      executor - should be able to execute maxThreadNum * following up operations in parallel.
      Returns:
      See Also:
    • parallel

      public FloatStream parallel(int maxThreadNum, BaseStream.Splitor splitor, Executor executor)
      Description copied from interface: BaseStream
      Consider using sps(ParallelSettings, Function) if only next operation need to be parallelized. For example:
       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)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(ParallelSettings, 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>>
      executor - should be able to execute maxThreadNum * following up operations in parallel.
      Returns:
      See Also:
    • parallel

      Description copied from interface: BaseStream
      Consider using sps(ParallelSettings, Function) if only next operation need to be parallelized. For example:
       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)...;
      
       
      In most scenarios, there could be only one operation need be parallelized in the stream. So sps(ParallelSettings, 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:
    • sps

      public <SS extends BaseStream> SS sps(Function<? super FloatStream,? 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>>
      Returns:
    • sps

      public <SS extends BaseStream> SS sps(int maxThreadNum, Function<? super FloatStream,? 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>>
      Returns:
    • sps

      public <SS extends BaseStream> SS sps(BaseStream.ParallelSettings ps, Function<? super FloatStream,? 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(ps).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>>
      Returns:
    • psp

      public <SS extends BaseStream> SS psp(Function<? super FloatStream,? 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>>
      Returns:
    • toArray

      public float[] 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>>
    • 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