stream流map多个字段_Java8Streamconcat()合并多个流本⽂主要讲解通过使⽤ at()⽅法 去合并两个stream,新的stream由原来的两个stream所有元素组成(相同元素会覆盖)。
1.Stream concat()⽅法基本介绍
static Stream 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 firstStream = Stream.of(1, 2, 3);
Stream secondStream = Stream.of(4, 5, 6);
Stream resultingStream = at(firstStream, secondStream);
System.out.println( List()) );
}
}
程序运⾏输出:
$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 first = Stream.of(1, 2);
Stream second = Stream.of(3,4);
Stream third = Stream.of(5, 6);
Stream fourth = Stream.of(7,8);
Stream resultingStream = at(first, concat(second, concat(third, fourth)));
System.out.println( List()) );
}
}
程序输出:
$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 firstStream = Stream.of(1, 2, 3, 4, 5, 6);
Stream secondStream = Stream.of(4, 5, 6, 7, 8, 9);
Stream resultingStream = at(firstStream, secondStream)
.
distinct();
System.out.println( List()) );
}
}
输出结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
4.2⾃定义对象对⽐
在合并⾃定义对象流的情况下,我们可以在流迭代期间删除重复的元素。 我们可以使⽤对象属性⽰例为java流创建的distinctByKey()函数import java.util.ArrayList;
import java.util.Map;
import urrent.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 stream1 = getEmployeeListOne().stream();
Stream stream2 = getEmployeeListTwo().stream();
Stream resultingStream = at(stream1, stream2)
.filter(distinctByKey(Employee::getFirstName));
System.out.println( List()) );
}
public static Predicate distinctByKey(Function super T, Object> keyExtractor) {
Map map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
private static ArrayList getEmployeeListOne()
{
ArrayList 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 getEmployeeListTwo()
{
ArrayList 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;
java stream}
}
输出结果
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]]把你的疑问留在留⾔板我会不定期解答。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。