天天看点

java8包装类支持流操作

java8包装类支持流操作

  • ​​1.为什么包装类需要对流进行支持​​
  • ​​2.有哪些包装类​​
  • ​​2.1ToLongFunction:Long->long​​
  • ​​2.2ToIntFunction:Integer->int​​
  • ​​2.3ToDoubleFunction:Double->double​​
  • ​​2.4ToDoubleBiFunction:doSome:Double->double​​
  • ​​2.5ToIntBiFunction:doSome:Double->double​​
  • ​​2.6ToLongBiFunction:doSome:Long->long​​
  • ​​2.7IntFunction:int -> Int​​
  • ​​2.8LongFunction:long -> Long​​
  • ​​2.9DoubleFunction:double -> Double​​
  • ​​3.对应的流操作​​
  • ​​3.1IntStream​​
  • ​​4.如何使用​​

1.为什么包装类需要对流进行支持

List<int> list = new ArrayList<>();      

2.有哪些包装类

2.1ToLongFunction:Long->long

package java.util.function;
@FunctionalInterface
public interface ToLongFunction<T> {
    long applyAsLong(T value);
}      

2.2ToIntFunction:Integer->int

package java.util.function;
@FunctionalInterface
public interface ToIntFunction<T> {
    int applyAsInt(T value);
}      

2.3ToDoubleFunction:Double->double

package java.util.function;
@FunctionalInterface
public interface ToDoubleFunction<T> {
    double applyAsDouble(T value);
}      

2.4ToDoubleBiFunction:doSome:Double->double

package java.util.function;
@FunctionalInterface
public interface ToDoubleBiFunction<T, U> {
    double applyAsDouble(T t, U u);
}      

2.5ToIntBiFunction:doSome:Double->double

package java.util.function;
@FunctionalInterface
public interface ToIntBiFunction<T, U> {
    int applyAsInt(T t, U u);
}      

2.6ToLongBiFunction:doSome:Long->long

package java.util.function;
@FunctionalInterface
public interface ToLongBiFunction<T, U> {
    long applyAsLong(T t, U u);
}      

2.7IntFunction:int -> Int

package java.util.function;
@FunctionalInterface
public interface IntFunction<R> {
    R apply(int value);
}      

2.8LongFunction:long -> Long

package java.util.function;
@FunctionalInterface
public interface LongFunction<R> {
    R apply(long value);
}      

2.9DoubleFunction:double -> Double

package java.util.function;
@FunctionalInterface
public interface DoubleFunction<R> {
    R apply(double value);
}      

3.对应的流操作

3.1IntStream

package java.util.stream;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.Objects;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.ObjIntConsumer;
import java.util.function.Supplier;
public interface IntStream extends BaseStream<Integer, IntStream> {
    IntStream filter(IntPredicate predicate);
    IntStream map(IntUnaryOperator mapper);
    <U> Stream<U> mapToObj(IntFunction<? extends U> mapper);
    LongStream mapToLong(IntToLongFunction mapper);
    DoubleStream mapToDouble(IntToDoubleFunction mapper);
    IntStream flatMap(IntFunction<? extends IntStream> mapper);
    IntStream distinct();
    IntStream sorted();
    IntStream peek(IntConsumer action);
    IntStream limit(long maxSize);
    IntStream skip(long n);
    void forEach(IntConsumer action);
    void forEachOrdered(IntConsumer action);
    int[] toArray();
    int reduce(int identity, IntBinaryOperator op);
    OptionalInt reduce(IntBinaryOperator op);
    <R> R collect(Supplier<R> supplier,
                  ObjIntConsumer<R> accumulator,
                  BiConsumer<R, R> combiner);
    int sum();
    OptionalInt min();
    OptionalInt max();
    long count();
    OptionalDouble average();
    IntSummaryStatistics summaryStatistics();
    boolean anyMatch(IntPredicate predicate);
    boolean allMatch(IntPredicate predicate);
    boolean noneMatch(IntPredicate predicate);
    OptionalInt findFirst();
    OptionalInt findAny();
    LongStream asLongStream();
    DoubleStream asDoubleStream();
    Stream<Integer> boxed();

    @Override
    IntStream sequential();

    @Override
    IntStream parallel();

    @Override
    PrimitiveIterator.OfInt iterator();

    @Override
    Spliterator.OfInt spliterator();
    // Static factories
    public static Builder builder() {
        return new Streams.IntStreamBuilderImpl();
    }
    public static IntStream empty() {
        return StreamSupport.intStream(Spliterators.emptyIntSpliterator(), false);
    }
    public static IntStream of(int t) {
        return StreamSupport.intStream(new Streams.IntStreamBuilderImpl(t), false);
    }
    public static IntStream of(int... values) {
        return Arrays.stream(values);
    }
    public static IntStream iterate(final int seed, final IntUnaryOperator f) {
        Objects.requireNonNull(f);
        final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
            int t = seed;

            @Override
            public boolean hasNext() {
                return true;
            }

            @Override
            public int nextInt() {
                int v = t;
                t = f.applyAsInt(t);
                return v;
            }
        };
        return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
                iterator,
                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
    }
    public static IntStream generate(IntSupplier s) {
        Objects.requireNonNull(s);
        return StreamSupport.intStream(
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false);
    }
    public static IntStream range(int startInclusive, int endExclusive) {
        if (startInclusive >= endExclusive) {
            return empty();
        } else {
            return StreamSupport.intStream(
                    new Streams.RangeIntSpliterator(startInclusive, endExclusive, false), false);
        }
    }
    public static IntStream rangeClosed(int startInclusive, int endInclusive) {
        if (startInclusive > endInclusive) {
            return empty();
        } else {
            return StreamSupport.intStream(
                    new Streams.RangeIntSpliterator(startInclusive, endInclusive, true), false);
        }
    }
    public static IntStream concat(IntStream a, IntStream b) {
        Objects.requireNonNull(a);
        Objects.requireNonNull(b);

        Spliterator.OfInt split = new Streams.ConcatSpliterator.OfInt(
                a.spliterator(), b.spliterator());
        IntStream stream = StreamSupport.intStream(split, a.isParallel() || b.isParallel());
        return stream.onClose(Streams.composedClose(a, b));
    }
    public interface Builder extends IntConsumer {
        @Override
        void accept(int t);
        default Builder add(int t) {
            accept(t);
            return this;
        }
        IntStream build();
    }
}      

4.如何使用

@Test
  public void method7(){
    LongSummaryStatistics statistics = Main.getUsers()
        .stream()
        .mapToLong(u -> {
          System.out.print(u);
          return u.getId();
        }).summaryStatistics();
    System.out.println("max:"+statistics.getMax());
    System.out.println("min:"+statistics.getMin());
    System.out.println("ave:"+statistics.getAverage());
    System.out.println("sum:"+statistics.getSum());
  }      
{id=58,name=name:0.2689475499558849}
{id=51,name=name:0.6716262160499563}
{id=38,name=name:0.06574632673216096}
{id=70,name=name:0.6792261605337495}
{id=33,name=name:0.2516699155393817}
{id=84,name=name:0.6028574395729949}
{id=76,name=name:0.23834435845470736}
{id=30,name=name:0.4791369843257146}
{id=5,name=name:0.30710665597599773}
{id=77,name=name:0.4131467144257531}max:84
min:5
ave:52.2
sum:522
      

继续阅读