JDK1.8新特性之Stream类初识

JDK1.8新特性之Stream类初识

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {

    public static void main(String[] args) {
        StreamString stream = Stream.of("good", "good", "study", "day", "day", "up");
        // forEach方法
            stream.forEach(System.out::println);

        // fi< er方法(输出长度大于3的字符串)
        stream.fi< er((s) - s.len >h()  3).forEach(System.out::println);

        // distinct(去重复)
        // stream.distinct().forEach(System.out::println);

        // map映射操作,将字符串转大写后再输出
        stream.map(s - s.toUpperCase()).forEach(System.out::println);

        // flatMap
        StreamListInteger ss = Stream.of(Arrays.asList(1, 2, 3), Arrays.asList(5, 6));
        ss.flatMap(list - list.stream()).forEach(System.out::println);

        // reduce,找出流中长度最大的字符串
        OptionalString opt = stream.reduce((s1, s2) - s1.len >h() = s2.len >h() ? s1 : s2);
        System.out.println(opt.get());

        // collect
        ListString list = stream.collect(Collectors.toList());
        list.forEach(System.out::println);

        // ::方法引用
        // 引用静态方法:Integer.valueOf
        // 引用实例方法:list::add
        // 引用构造方法:ArrayList::new

    }
}

转载于:https://www.cnblogs.com/zxfei/p/10907452.html

最新回复(0)
/jishunZowZu_2BmjRpWadEcbEm5GV6xBDJG12c3gVCpYizmrBs_3D4833574
8 简首页