Stream.of()
用于将将输入的数据类型转换为Stream流.用法如下:
Stream<String> stream = Stream.of("1", "2", "4");
forEach()
进行遍历
filter()
进行筛选:用法如下:
public class Demo01Stream {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("ydaiq");
list.add("欧阳娜娜");
list.add("汤唯");
list.add("迪丽热巴");
list.add("高圆圆");
list.add("张三丰");
list.add("张无忌");
list.add("张阳");
//使用Stream流的方式进行筛选
//筛选以"张"开头的名字
list.stream().filter(name -> name.startsWith("张"))
//筛选长度为 3 的名字
.filter(name -> name.length() == 3)
//使用Stream流提供的 forEach 方法来输出
.forEach(name-> System.out.println(name));
//结果: 张三丰
// 张无忌
}
}
map()
该接口需要一个 Function 函数式接口作为参数,可以将当前流中的类型转换为另外一种类型.
public class Demo02StreamMap {
public static void main(String[] args) {
//创建一个流
Stream<String> stream = Stream.of("1", "2", "4"); //String
//使用map方法进行元素类型转换,将String类型转换为Integer类型
Stream<Integer> stream1 = stream.map(obj -> Integer.parseInt(obj));
//遍历
stream1.forEach(i -> System.out.print(i)); //Integer
//结果:1 2 4
}
}
long count()
统计个数,是一个终极方法,所以不能够使用链式编程,返回值是 long 类型
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
//先转换为Stream流
Stream<Integer> stream = list.stream();
//使用count方法,返回值是long类型
long count = stream.count();
System.out.println(count);
//结果:7
}
limit
用于截取一定长度的流中元素,是一个延时方法
public class Demo04Stream_Limit {
public static void main(String[] args) {
//获取一个Stream流
String[] str = {"喜羊羊","美羊羊","灰太狼","红太狼"};
Stream<String> stream = Stream.of(str);
//使用limit方法
Stream<String> stream1 = stream.limit(2);
//遍历
stream1.forEach(name -> System.out.print(name+" "));
//结果:喜羊羊 美羊羊
}
}
skip
用于跳过前几个元素来截取流,是延时方法,用法同 limit
public class Demo05Stream_Skip {
public static void main(String[] args) {
//获取一个Stream流
String[] str = {"喜羊羊","美羊羊","灰太狼","红太狼"};
Stream<String> stream = Stream.of(str);
//使用skip方法
Stream<String> stream1 = stream.skip(2);
//遍历
stream1.forEach(name -> System.out.print(name+" "));
//结果:灰太狼 红太狼
}
}
concat
将两个流连接成一个,返回一个连接好的流
public class Demo06Stream_Concat {
public static void main(String[] args) {
//获取两个Stream流
String[] str = {"喜羊羊","美羊羊"};
Stream<String> stream = Stream.of(str);
Stream<String> stream2 = Stream.of("ydaiq", "wzz", "nicai", "zhangsan");
//使用concat方法
Stream<String> concatStream = Stream.concat(stream, stream2);
//遍历
concatStream.forEach(name -> System.out.print(name+" "));
//结果:喜羊羊 美羊羊 ydaiq wzz nicai zhangsan
}
}
以上方法的综合使用
public class Demo07Stream_Test {
public static void main(String[] args) {
ArrayList<String> one = new ArrayList<>();
one.add("迪丽热巴");
one.add("宋远桥");
one.add("苏星河");
one.add("石破天");
one.add("石中玉");
one.add("老子");
one.add("壮壮");
one.add("洪七公");
Stream<String> one1 = one.stream().filter(name -> name.length() == 3)
.limit(3);
ArrayList<String> two = new ArrayList<>();
two.add("古力娜扎");
two.add("张无忌");
two.add("赵丽颖");
two.add("张三丰");
two.add("尼古拉斯赵四");
two.add("张天爱");
two.add("张二狗");
Stream<String> two1 = two.stream().filter(name -> name.startsWith("张"))
.skip(2);
Stream<Person> personStream = Stream.concat(one1, two1).map(name -> new Person(name));
personStream.forEach(name -> System.out.println(name));
}
}