Lambda_14 函数表达式 Stream Concat拼接流|8月更文挑战

545 阅读2分钟

这是我参与8月更文挑战的第29天,活动详情查看:8月更文挑战

@[TOC](Stream Concat拼接流)

1、concat 流的拼接

Stream concat()方法

static <T> Stream<T> concat(Stream<? extends T> firstStream, Stream<? extends T> secondStream)

  • 此方法创建一个延迟连接的流,其元素是firstStream的所有元素,后跟secondStream的所有元素。
  • 如果两个输入流都是有序的,则对所得到的流进行排序。
  • 如果任一输入流是并行的,则得到的流是平行的。
  • 关闭结果流时,将调用两个输入流的关闭处理程序。

2、concat 拼接多个流

public class Lambada15 {
    public static void main(String[] args) {
        List<Student> s1 = Arrays.asList(new Student("张三", 18),
                new Student("李四", 19));
        List<Student> s2 = Arrays.asList(new Student("王五", 21),
                new Student("赵六", 22));
        List<Student> s3 = Arrays.asList(new Student("张三2", 18),
                new Student("李四2", 19));
        List<Student> s4 = Arrays.asList(new Student("王五2", 21),
                new Student("赵六2", 22));

        //使用stream concat拼接s1 和s2
        Stream<Student> stuStream12 = Stream.concat(s1.stream(), s2.stream());
        stuStream12.forEach(t-> System.out.println(t));

        System.out.println("=================================");
        //如何把s1 s2 s3 s4 都拼接上呢?
        List[] lists = {s1,s2,s3,s4};
        //定于一个Student 类型的流
        Stream<Student> studentStream = Stream.<Student>builder().build();
        for (List list:lists) {
            studentStream = Stream.concat(studentStream, list.stream());
        }
        studentStream.forEach(t-> System.out.println(t));
    }
}

合并多个流也可以使用如下写法

public static void main(String[] args) {
        List<Student> s1 = Arrays.asList(new Student("张三", 18),
                new Student("李四", 19));
        List<Student> s2 = Arrays.asList(new Student("王五", 21),
                new Student("赵六", 22));
        List<Student> s3 = Arrays.asList(new Student("张三2", 18),
                new Student("李四2", 19));
        List<Student> s4 = Arrays.asList(new Student("王五2", 21),
                new Student("赵六2", 22));

        System.out.println("=================================");
        //如何把s1 s2 s3 s4 都拼接上呢?
        List[] lists = {s1,s2,s3,s4};
        //定于一个Student 类型的流
        Stream<Student> studentStream = Stream.<Student>builder().build();
        for (List list:lists) {
            studentStream = Stream.concat(studentStream, list.stream());
        }
        studentStream.forEach(t-> System.out.println(t));
        
        System.out.println("=============方法2====================");
        Stream<Student> studentStream2 = Stream.<Student>builder().build();
        studentStream2 = Stream.concat(s1.stream(),concat(s2.stream(), concat(s3.stream(), s4.stream())));
         studentStream2.forEach(t-> System.out.println(t));
    }


3、 自定义对象对比

在合并自定义对象流的情况下,我们可以在流迭代期间删除重复的元素。 我们可以使用对象属性示例为java流创建的distinctByKey()函数

 
public class Main
{
    public static void main(String[] args)
    {
        //获取两个对象流
        Stream<Employee> stream1 = getEmployeeListOne().stream();
        Stream<Employee> stream2 = getEmployeeListTwo().stream();
         
        Stream<Employee> resultingStream = Stream.concat(stream1, stream2)
                .filter(distinctByKey(Employee::getFirstName));
         
        System.out.println( resultingStream.collect(Collectors.toList()) );
    }
     
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
    {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
 
    private static ArrayList<Employee> getEmployeeListOne()
    {
        ArrayList<Employee> list = new ArrayList<>();
        list.add( new Employee(1l, "Lokesh", "Gupta") );
        list.add( new Employee(5l, "Brian", "Piper") );
        list.add( new Employee(7l, "Charles", "Piper") );
        list.add( new Employee(6l, "David", "Beckham") );
        return list;
    }
     
    private static ArrayList<Employee> getEmployeeListTwo()
    {
        ArrayList<Employee> list = new ArrayList<>();
        list.add( new Employee(2l, "Lokesh", "Gupta") );
        list.add( new Employee(4l, "Brian", "Piper") );
        list.add( new Employee(3l, "David", "Beckham") );
        return list;
    }
}