Java8-04-01-笔记

Posted 寻7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8-04-01-笔记相关的知识,希望对你有一定的参考价值。

创建Stream

1,Collection系列集合

通过Collection系列集合提供的方法stream()或者parallelStream()

default Stream<E> stream() :返回一个顺序流
default Stream<E> parallelStream() :返回一个并行流
package java.util;
//...

/*
 * The root interface in the <i>collection hierarchy</i>.  A collection
 * represents a group of objects, known as its <i>elements</i>.  Some
 * collections allow duplicate elements and others do not.  Some are ordered
 * and others unordered.  The JDK does not provide any <i>direct</i>
 * implementations of this interface: it provides implementations of more
 * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>.  This interface
 * is typically used to pass collections around and manipulate them where
 * maximum generality is desired.
 */
public interface Collection<E> extends Iterable<E> {
    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }
    default Stream<E> parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }
    ...}
  • 应用案例
    List<String> list = new ArrayList<>();
    Stream<String> stream1 = list.stream();
        
    Set<String> set = new HashSet<>();
    Stream<String> stream2 = set.stream();
        
    Map<String,String> map = new HashMap<>();
    //map是不可以获取stream流的,除非对键或值的集合进行操作
    

2,Arrays静态方法

通过Arrays中的静态方法stream获取数组的流

public static <T> Stream<T> stream(T[] array)
//重载形式,能够处理对应基本类型的数组
public static IntStream stream(int[] array)
public static LongStream stream(long[] array)
public static DoubleStream stream(double[] array)
package java.util;
//...

/*
 * This class contains various methods for manipulating arrays (such as
 * sorting and searching). This class also contains a static factory
 * that allows arrays to be viewed as lists.
 */
public class Arrays {
	public static <T> Stream<T> stream(T[] array) {
        return stream(array, 0, array.length);
    }
    ...}
  • 应用案例
    Employee[] employees = new Employee[10];
    Stream<Employee> stream3 = Arrays.stream(employees);
    

3,Stream多值创建流

由值创建流,通过Stream类的静态方法of()创建一个流,可以接收任意数量的参数

public static<T> Stream<T> of(T... values)
package java.util.stream;
//...

public interface Stream<T> extends BaseStream<T, Stream<T>> {
	public static<T> Stream<T> of(T... values) {
        return Arrays.stream(values);
    }
    ...}
  • 应用案例
    Stream<String> stream4 = Stream.of("sun","stone","hahah");
    

4,Stream无限流

由函数创建流:创建无限流,使用静态方法Stream.iterate()和Stream.generate()创建无限流

迭代:public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
生成:public static<T> Stream<T> generate(Supplier<T> s)
package java.util.stream;
//...

public interface Stream<T> extends BaseStream<T, Stream<T>> {
	public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
       .../*
       @FunctionalInterface
		public interface UnaryOperator<T> extends Function<T, T> {
		
	     * Returns a unary operator that always returns its input argument.
	     *
	     * @param <T> the type of the input and output of the operator
	     * @return a unary operator that always returns its input argument
	
		    static <T> UnaryOperator<T> identity() {
		        return t -> t;
		    }
		}
       */
    }
    public static<T> Stream<T> generate(Supplier<T> s) {
        Objects.requireNonNull(s);
        return StreamSupport.stream(
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
    }
    ...}
  • 应用案例

    Stream<Integer> stream5 = Stream.iterate(0,(x)->x+2);
    stream5.limit(7).forEach(System.out::println);//换行打印 0 2 4 6 8 10 12
    
    Stream.generate(()->Math.random()).limit(5).forEach(System.out::println);//换行打印5个随机生成数
    

以上是关于Java8-04-01-笔记的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记:python3,代码片段(2017)

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

sh bash片段 - 这些片段大多只是我自己的笔记;我找到了一些,有些我已经找到了

需要一种有效的方法来避免使用 Laravel 5 重复代码片段

Oracle-常用数据库对象笔记(片段)

如何使用导航组件处理片段内的向上按钮