1、两种获取Stream流的方式
java.util.stream.Stream
获取流两种方式:
1、所有Collection 集合(单列集合)都可以通过stream默认方式获取流
default Stream
2、Stream接口的静态方法of可以获取数组对应的流
static
参数是一个可变参数,就可传递一个数组
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.*;
import java.util.stream.Stream;
/*
两种方式获取流
1、Collection 接口中的方法stream()
2、Stream接口的静态方法of:返回单个元素的序列Stream
static Stream of (T... values)
参数是一个可变参数,可传递一个数组
*/
public class Demo02GetStream {
public static void main(String[] args) {
//把单列集合转换成Stream
List list = new ArrayList();
Stream stream = list.stream();
//Set
Set set = new HashSet<>();
Stream stream1 = set.stream();
Map map1 = new HashMap<>();
Set strings = map1.keySet();
Stream stream2 = strings.stream();
Collection values = map1.values();
Stream stream3 = values.stream();
Set> entries = map1.entrySet();
Stream> stream4 = entries.stream();
//把数组转换成Stream流
Stream stream5 = Stream.of(1, 2, 34, 4); //可变参数
//可变参数可以传数组
int[] arr = {1,3,4,5};
Stream stream6 = Stream.of(arr);
String[] arr2 = {"a","fff","fs"};
Stream stream7 = Stream.of(arr2);
}
}
2、Stream 流中的常用方法-forEach
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.stream.Stream;
/*
void forEach(Consumer super T> action);
该方法接收一个Consumer函数接口,会将每一个流元素交给该函数进行处理
Consumer 是一个消费性的函数式接口,可以传递Lambda表达式
注意:
for each 用来遍历流中的数据
是终结方法, 遍历之后不能调用Stream流中的其他方法
*/
public class Demo02Streamfor {
public static void main(String[] args) {
Stream af = Stream.of("af", "sdkf", "cc", "gg", "hh");
/* af.forEach((String name)->{
System.out.println(name);
});*/
//简化Lambda表达式
System.out.println("--------");
af.forEach((name)-> System.out.println(name));
}
}
结果
af
sdkf
cc
gg
hh
3、Stream 流中的常用方法-filter
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.stream.Stream;
public class Demo02StreamFilter {
public static void main(String[] args) {
//创建Stream 流
Stream stream = Stream.of("张三丰", "张无极", "张强", "赵敏", "周芷若");
//对Stream 流中的元素进行过滤,只要姓张的, 得到一个新的流, 接收他
Stream stream2 = stream.filter((String name )->{ return name.startsWith("张");});
stream2.forEach((name)-> System.out.println(name));
}
}
4、Stream 流的特点,只能使用一次
Stream流属于管道流,只能被使用一次
第一个Stream流调用完毕方法,数据就会流转到下一个Stream 上
而这时第一个Stream流已经使用完毕,就会关闭了
所以第一个Streaml流就不能再调用方法了
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.stream.Stream;
public class Demo02StreamFilter {
public static void main(String[] args) {
//创建Stream 流
Stream stream = Stream.of("张三丰", "张无极", "张强", "赵敏", "周芷若");
//对Stream 流中的元素进行过滤,只要姓张的, 得到一个新的流, 接收他
Stream stream2 = stream.filter((String name )->{ return name.startsWith("张");});
stream2.forEach((name)-> System.out.println(name));
stream.forEach((name)-> System.out.println(name)); //报错,
//Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
}
}
结果
张三丰
张无极
张强
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.sourceStageSpliterator(AbstractPipeline.java:279)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at con.day13.demo05.StreamAndMethod.Stream.Demo02StreamFilter.main(Demo02StreamFilter.java:13)
5、Stream 流中的常用方法-map
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.stream.Stream;
public class Demo02StreamMap {
public static void main(String[] args) {
Stream stream = Stream.of("1", "2", "3", "4");
Stream integerStream = stream.map((s) -> Integer.parseInt(s));
integerStream.forEach(i-> System.out.println(i));
}
}
结果
1
2
3
4
6、Stream 流中的常用方法-count
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.ArrayList;
import java.util.stream.Stream;
//统计流中元素的个数, 返回值是long类型的整数,是一个终结方法,不能再调用Stream中其他方法
public class Demo02StreamCount {
public static void main(String[] args) {
Stream a = Stream.of("a", "b", "d");
System.out.println(a.count());
ArrayList list = new ArrayList<>();
list.add("22");
list.add("4");
list.add("7");
Stream stream2 = list.stream();
long count = stream2.count();
System.out.println(count);
}
}
3
3
7、Stream 流中的常用方法-limit
limit 是一个延迟方法, 对流中元素截取,返回一个新的流,可以调用流中其他方法
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.stream.Stream;
public class Demo02StreamLimit {
public static void main(String[] args) {
Stream stream = Stream.of("44", "fsdf", "t", "9", "2", "iu");
Stream limit = stream.limit(4);
// System.out.println(limit.count()); //4
limit.forEach(s-> System.out.println(s));
}
}
}
结果
44
fsdf
t
9
8、Stream 流中的常用方法-skip
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.Arrays;
import java.util.stream.Stream;
public class Demo02StreamSkip {
public static void main(String[] args) {
String[] arr = {"sdf","fsd","wr","il"};
Stream stream = Stream.of(arr);
//跳过前三个元素
Stream skip = stream.skip(3);
// System.out.println(skip.count()); 1
//遍历新的流
skip.forEach(s -> System.out.println(s)); //il
}
}
9、Stream 流中的常用方法-concat
package con.day13.demo05.StreamAndMethod.Stream;
import java.util.stream.Stream;
public class Demo02StreamConcat {
public static void main(String[] args) {
String[] arr = {"sdf","fsd","wr","il"};
Stream stream = Stream.of(arr);
String[] arr2 = {"tyui","rg","ghj","ghj"};
Stream stream2 = Stream.of(arr);
Stream concat1 = Stream.concat(stream, stream2);
concat1.forEach(e-> System.out.println(e));
}
}
结果
sdf
fsd
wr
il
sdf
fsd
wr
il
10、练习,集合元素处理,传统方式
import java.util.ArrayList;
public class DemoArrayList {
public static void main(String[] args) {
ArrayList list1 = new ArrayList<>();
list1.add("张无忌");
list1.add("黑色裤");
list1.add("迪丽热巴");
list1.add("古力扎");
list1.add("张收到回复");
list1.add("胡返回");
//只要名字为3个字的成员,储存到新集合中
ArrayList list11 = new ArrayList<>();
for (String s : list1) {
if (s.length() == 3){
list11.add(s);
}
}
//只要前三个人
ArrayList list12 = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list12.add(list11.get(i)); //i = 0 1 2
}
ArrayList list2 = new ArrayList<>();
list2.add("张三丰");
list2.add("张三好");
list2.add("张三符");
list2.add("王三看");
list2.add("张三与");
list2.add("杨三与");
//只要姓张的
ArrayList list21 = new ArrayList<>();
for (String s2 : list2) {
//只要姓张的成员
if (s2.startsWith("张")) {
list21.add(s2);
}
}
//不要前两个
ArrayList list22 = new ArrayList<>();
for (int i = 2; i < list21.size(); i++) {
list22.add(list21.get(i)) ; //2 - length
}
//5、将两个队伍合并成一个队伍,存储到一个新的集合中
ArrayList all = new ArrayList<>();
all.addAll(list12);
all.addAll(list22);
//6、根据姓名创建Person对象,存储到一个新集合中
ArrayList list = new ArrayList<>();
for (String s : all) {
list.add(new Person(s));
}
//7、打印整个队伍的Person
for (Person person : list) {
System.out.println(person);
}
}
}
结果
Person{name='张无忌'}
Person{name='黑色裤'}
Person{name='古力扎'}
Person{name='张三符'}
Person{name='张三与'}
11、练习,集合元素处理, Stream 流处理
package con.day13.demo05.StreamAndMethod.Lianxi;
import java.util.ArrayList;
import java.util.stream.Stream;
public class DemoStream01 {
public static void main(String[] args) {
ArrayList list1 = new ArrayList<>();
list1.add("张无忌");
list1.add("黑色裤");
list1.add("迪丽热巴");
list1.add("古力扎");
list1.add("张收到回复");
list1.add("胡返回");
Stream oneStream = list1.stream().filter(name->name.length() == 3).limit(3);
ArrayList list2 = new ArrayList<>();
list2.add("张三丰");
list2.add("张三好");
list2.add("张三符");
list2.add("王三看");
list2.add("张三与");
list2.add("杨三与");
Stream twoStream = list2.stream().filter(name->name.startsWith("张")).skip(2);
Stream.concat(oneStream, twoStream).map(name -> new Person(name)).forEach(p-> System.out.println(p));
//简写
Stream.concat(oneStream, twoStream).map(Person::new).forEach(System.out::println);
}
}
结果
Person{name='张无忌'}
Person{name='黑色裤'}
Person{name='古力扎'}
Person{name='张三符'}
Person{name='张三与'}