Stream提供concat()方法来连接两个流并返回一个流。 要连接Lists,Sets和Arrays,我们首先将它们转换为流,然后使用concat()将它们组合起来。 可以使用收集器的方法将输出流转换为List,Set等,例如toList(),toSet()等。
static <T> Stream<T> concat(
Stream<? extends T> a, Stream<? extends T> b)
concat()创建延迟连接流。 此流包含第一个流的所有元素,后跟第二个流的所有元素。 如果订购了两个输入流,则将对所得到的流进行排序。 如果两个流中的任何一个是平行的,则得到的流将是平行的。
import java.util.stream.Stream;
public class ConcatStreamsDemo {
public static void main(String[] args) {
Stream<String> s1 = Stream.of("AA", "BB", "CC");
Stream<String> s2 = Stream.of("AA", "BB", "DD");
Stream<String> s = Stream.concat(s1, s2);
s.forEach(e->System.out.print(e+" "));
//Remove duplicates using distinct()
s1 = Stream.of("AA", "BB", "CC");
s2 = Stream.of("AA", "BB", "DD");
System.out.println("\nRemove duplicates using distinct()");
s = Stream.concat(s1, s2).distinct();
s.forEach(e->System.out.print(e+" "));
}
}
输出:
AA BB CC AA BB DD
Remove duplicates using distinct()
AA BB CC DD
Concat Lists
在这个例子中,我们有两个列表。 这些列表将转换为流,然后使用concat()方法连接它们。 输出流将使用Collectors.toList()转换为List。 获得不同的元素
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ConcatListsDemo {
public static void main(String[] args) {
List<Book> list1 = new ArrayList<>();
List<Book> list2 = new ArrayList<>();
{
list1.add(new Book("Core Java", 200));
list1.add(new Book("Spring MVC", 300));
list1.add(new Book("Learning Freemarker", 150));
list2.add(new Book("Core Java", 200));
list2.add(new Book("Spring MVC", 300));
list2.add(new Book("Learning Hibernate", 400));
}
List<Book> list = Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList());
list.forEach(b->System.out.println(b.getName()+", "+ b.getPrice()));
//Remove duplicates using distinct()
System.out.println("--Remove duplicates using distinct()--");
list = Stream.concat(list1.stream(), list2.stream()).distinct().collect(Collectors.toList());
list.forEach(b->System.out.println(b.getName()+", "+ b.getPrice()));
}
}
public class Book {
private String name;
private int price;
public Book(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
final Book book = (Book) obj;
if (this == book) {
return true;
} else {
return (this.name.equals(book.name) &&
this.price == book.price);
}
}
@Override
public int hashCode() {
int hashno = 7;
hashno = 13 * hashno + (name == null ? 0 :
name.hashCode());
return hashno;
}
}
输出:
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Core Java, 200
Spring MVC, 300
Learning Hibernate, 400
--Remove duplicates using distinct()--
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Learning Hibernate, 400
Concat Arrays
这些数组将转换为流,然后使用concat()方法连接它们。 输出流将使用Stream.toArray()转换为数组。 获得不同的元素
import java.util.stream.Stream;
public class ConcatArraysDemo {
public static void main(String[] args) {
Book[] bk1 = new Book[3];
Book[] bk2 = new Book[3];
{
bk1[0] = new Book("Core Java", 200);
bk1[1] = new Book("Spring MVC", 300);
bk1[2] = new Book("Learning Freemarker", 150);
bk2[0] = new Book("Core Java", 200);
bk2[1] = new Book("Spring MVC", 300);
bk2[2] = new Book("Learning Hibernate", 400);
}
Book[] bks = (Book[]) Stream.concat(Stream.of(bk1),
Stream.of(bk2)).toArray(b -> new Book[b]);
for(Book b : bks) {
System.out.println(b.getName()+", "+ b.getPrice());
}
//Remove duplicates using distinct()
System.out.println("--Remove duplicates using distinct()--");
bks = (Book[]) Stream.concat(Stream.of(bk1), Stream.of(bk2)).distinct().toArray(b -> new Book[b]);
for(Book b : bks) {
System.out.println(b.getName()+", "+ b.getPrice());
}
}
}
输出:
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Core Java, 200
Spring MVC, 300
Learning Hibernate, 400
--Remove duplicates using distinct()--
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Learning Hibernate, 400
Concat Sets
这些集合将转换为流,然后使用concat()方法连接它们。 输出流将使用Collectors.toSet()转换为Set。
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ConcatSetsDemo {
public static void main(String[] args) {
Set<Book> set1 = new HashSet<>();
Set<Book> set2 = new HashSet<>();
{
set1.add(new Book("Core Java", 200));
set1.add(new Book("Spring MVC", 300));
set1.add(new Book("Learning Freemarker", 150));
set2.add(new Book("Core Java", 200));
set2.add(new Book("Spring MVC", 300));
set2.add(new Book("Learning Hibernate", 400));
}
Set<Book> set = Stream.concat(set1.stream(), set2.stream()).collect(Collectors.toSet());
set.forEach(b->System.out.println(b.getName()+", "+ b.getPrice()));
}
}
输出:
Spring MVC, 300
Learning Freemarker, 150
Learning Hibernate, 400
Core Java, 200