文
章
目
录
章
目
录
学习使用Stream.concat()方法将两个流合并成一个流,该流中的元素由合并的两个流的所有元素组成。
1.Stream的concat()方法
static <T> Stream<T> concat(Stream<? extends T> firstStream,
Stream<? extends T> secondStream)
- 这个方法创建了一个惰性合并的流,其元素是第一个流的元素,然后是第二个流的元素。
- 如果两个输入流都是有序的,则结果流也是有序的。
- 如果任何一个输入流是并行的,则结果流是并行的。
- 当结果流关闭时,两个输入流的关闭处理程序都会被调用。
2.合并两个流
Java示例合并两个数字流 – 获得包含来自两个流的数字的流。
public class Main
{
public static void main(String[] args)
{
Stream<Integer> firstStream = Stream.of(1, 2, 3);
Stream<Integer> secondStream = Stream.of(4, 5, 6);
Stream<Integer> resultingStream = Stream.concat(firstStream, secondStream);
System.out.println( resultingStream.collect(Collectors.toList()) );//[1, 2, 3, 4, 5, 6]
}
}
3.合并多个流
Java示例合并四个数字流 – 获得包含来自所有流的数字的流。请注意,我们对Stream.concat()函数进行了静态导入,使代码更易读。
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Stream.*;
public class Main
{
public static void main(String[] args)
{
Stream<Integer> first = Stream.of(1, 2);
Stream<Integer> second = Stream.of(3,4);
Stream<Integer> third = Stream.of(5, 6);
Stream<Integer> fourth = Stream.of(7,8);
Stream<Integer> resultingStream = Stream.concat(first, concat(second, concat(third, fourth)));
System.out.println( resultingStream.collect(Collectors.toList()) );//[1, 2, 3, 4, 5, 6, 7, 8]
}
}
4.Java合并流并保留唯一元素
4.1 数字和字符串
在合并两个流时,我们可以使用distinct() API,结果流将只包含唯一不重复的元素。
public class Main
{
public static void main(String[] args)
{
Stream<Integer> firstStream = Stream.of(1, 2, 3, 4, 5, 6);
Stream<Integer> secondStream = Stream.of(4, 5, 6, 7, 8, 9);
Stream<Integer> resultingStream = Stream.concat(firstStream, secondStream)
.distinct();
System.out.println( resultingStream.collect(Collectors.toList()) );//[1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
4.2 自定义对象
在合并自定义对象流的情况下,我们可以在流迭代期间去除重复的元素。我们可以使用distinctByKey()函数,以下是为Java流中根据对象属性进行独特性检查而创建的示例。
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;
}
}
程序输出:
[
Employee [id=1, firstName=Lokesh, lastName=Gupta],
Employee [id=5, firstName=Brian, lastName=Piper],
Employee [id=7, firstName=Charles, lastName=Piper],
Employee [id=6, firstName=David, lastName=Beckham]]
在评论区留下与Java 8中合并两个或多个对象流相关的问题。