本文主要讲解通过使用 Stream.concat() 方法 去合并两个stream,新的stream由原来的两个stream所有元素组成(相同元素会覆盖)。
1.Stream concat()方法基本介绍
static <T> Stream<T> concat(Stream<? extends T> firstStream,
Stream<? extends T> secondStream)
- 此方法创建一个延迟连接的流,其元素是firstStream的所有元素,后跟secondStream的所有元素。
- 如果两个输入流都是有序的,则对所得到的流进行排序。
- 如果任一输入流是并行的,则得到的流是平行的。
- 关闭结果流时,将调用两个输入流的关闭处理程序。
2.Stream concat()合并两个stream
下面用两个数字类型的stream进行合并。
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()) );
}
}
程序运行输出:
$title(Console)
[1, 2, 3, 4, 5, 6]
3.合并多个流(Streams)
下面使用4个数字的stream作为演示数据。
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()) );
}
}
程序输出:
$title(Console)
[1, 2, 3, 4, 5, 6, 7, 8]
4.合并stream并保留唯一性
4.1数字和字符串
在合并两个流 并且保证数据唯一性,
import java.util.stream.Collectors;
import java.util.stream.Stream;
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自定义对象对比
在合并自定义对象流的情况下,我们可以在流迭代期间删除重复的元素。 我们可以使用对象属性示例为java流创建的distinctByKey()函数
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
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]]
把你的疑问留在留言板我会不定期解答。
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/243622.html