我是靠谱客的博主 完美机器猫,这篇文章主要介绍【Java】Java8新特性 —— Stream流,现在分享给大家,希望可以做个参考。

我们可以将要处理的元素集合看作一种流,流在管道中传输,并且可以在管道的节点上进行处理。

一 、Stream流的获取方法

1)单列集合

        可以使用 Collection 接口中的 stearm() 方法生成流。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public class StreamTest { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); // Stream<String> stream = list.stream(); // stream.forEach(s -> System.out.println(s)); list.stream(); // 打印元素 list.stream().forEach(s -> System.out.println(s)); } }

2)双列集合

        可以先获取 Set 集合,再间接的生成流。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public class StreamTest { public static void main(String[] args) { HashMap<String,Integer> hashMap = new HashMap<>(); hashMap.put("Cindy",21); hashMap.put("Lucy",22); hashMap.put("Nancy",24); // 双列集合不能直接获取Stream流 // 通过keySet()获取所有的键,将Set集合中的元素放入Stream流中 hashMap.keySet().stream(); // 通过entrySet()获取所有的键值对,将Set集合中的元素放入Stream流中 hashMap.entrySet().stream(); } }

3)数组

        调用 Arrays 中的静态方法 stream() 生成流。

复制代码
1
2
3
4
5
6
public class StreamTest { public static void main(String[] args) { int[] arr = {1,2,3}; Arrays.stream(arr); } }

4)同种数据类型的多个数据

复制代码
1
2
3
4
5
public class StreamTest { public static void main(String[] args) { Stream.of(1,2,3,4,5); } }

二、Stream流的常见操作方法

方法名称方法作用
forEach(Consumer action)迭代流中的每个元素
limit(long maxSize)获取前 n 个元素
skip(long n)跳过前 n 个元素
concat(Stream a, Stream b)将 a 和 b 两个流合并为一个
distinct()去除流中重复的元素
count()返回流中的元素数
filter()通过设置的条件滤出元素
collect()收集流中的数据

1)forEach() & limit() & skip() 示例

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class StreamTest { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <= 10; i++) { list.add(i); } // limit()方法:获取前 n 个元素 list.stream().limit(2) .forEach(i -> System.out.println(i)); // 结果:1 2 System.out.println("---------测试分割线---------"); // skip()方法:跳过前 n 个元素 list.stream().skip(8) .forEach(i -> System.out.println(i)); // 结果:9 10 } }

2)concat(Stream a, Stream b) & distinct() & count() 示例

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class StreamTest { public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<>(); list1.add("aaa"); list1.add("bbb"); list1.add("bbb"); // distinct()方法:去除流中重复的元素,依赖hashCode和equals方法 list1.stream().distinct() .forEach(s -> System.out.println(s)); // 结果:aaa bbb ArrayList<String> list2 = new ArrayList<>(); list2.add("ccc"); // concat()方法:将 stream1 和 stream2 合并为一个 /* Stream<String> stream1 = list1.stream(); Stream<String> stream2 = list2.stream(); Stream<String> stream3 = Stream.concat(stream1,stream2); stream3.forEach(s -> System.out.println(s)); // 结果:aaa bbb bbb ccc */ Stream.concat(list1.stream(),list2.stream()) .forEach(s -> System.out.println(s)); // 结果:aaa bbb bbb ccc System.out.println("---------测试分割线---------"); // count()方法:返回流中的元素数 long count = list1.stream().count(); System.out.println(count); // 结果:3 } }

3)filter() & collect() 示例

在 Stream 流中无法直接修改集合、数组等数据源中的数据,工具类 Collectors 提供了具体的收集方式:

        public static <T> Collector toList() :把元素收集到 List 集合中

        public static <T> Collector toSet() :把元素收集到 Set 集合中

        public static <T> Collector toMap(Function keyMapper,Function valueMapper) :把元素收集到 Map 集合中

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class StreamTest { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("acd"); list.add("ade"); list.add("bcd"); // filter()方法:通过设置的条件滤出元素,滤出以a开头的所有元素 list.stream().filter(s -> s.startsWith("a")) .forEach(s -> System.out.println(s)); // 结果:aaa acd ade System.out.println("---------测试分割线---------"); // collect()方法:收集流中的数据 List<String> newList = list.stream().filter(s -> s.startsWith("a")) .collect(Collectors.toList()); System.out.println(newList); // 结果:[aaa, acd, ade] } }

三、Stream流完整实例

1)查找字符串中所有以 “a” 开头的元素。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class StreamTest { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("abc"); list.add("abd"); list.add("acd"); list.add("bcde"); list.add("bcdef"); // 匿名内部类 /* list.stream().filter( new Predicate<String>() { @Override public boolean test(String s) { boolean result = s.startsWith("a"); return result; } } ).forEach(s -> System.out.println(s));*/ // Lambda表达式 /* list.stream().filter( (s) -> { boolean result = s.startsWith("a"); return result; }).forEach(s -> System.out.println(s));*/ // Lambda表达式 list.stream().filter(s -> s.startsWith("a")) .forEach(s -> System.out.println(s)); } }

2)将集合 {1,2,3,4,5,6,7,8,9,10} 中的奇数删除,只保留偶数。

复制代码
1
2
3
4
5
6
7
8
9
10
11
public class StreamTest { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <= 10; i++) { list.add(i); } List<Integer> list1 = list.stream().filter(integer -> integer % 2 == 0) .collect(Collectors.toList()); System.out.println(list1); } }

3)现有两个ArrayList集合,分别存储六名男演员和六名女演员,要求完成如下操作:

  • 男演员只要名字为3个字的前两人
  • 女演员只要姓杨的,并且不要第一个
  • 把过滤后的男演员姓名和女演员姓名合并到一起
  • 把上一步操作后的元素作为构造方法的参数创建演员对象,实例化一个 Actor 类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Actor { private String name; public Actor() { } public Actor(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class StreamTest { public static void main(String[] args) { ArrayList<String> manList = new ArrayList<>(); manList.add("张国立"); manList.add("刘烨"); manList.add("张卫健"); manList.add("张信哲"); manList.add("郑伊健"); manList.add("徐峥"); ArrayList<String> womanList = new ArrayList<>(); womanList.add("杨颖"); womanList.add("杨幂"); womanList.add("林心如"); womanList.add("张天爱"); womanList.add("赵丽颖"); womanList.add("关晓彤"); // 男演员只要名字为3个字的前两人 List<String> newManList = manList.stream().filter(s -> s.length() == 3).limit(2) .collect(Collectors.toList()); // 女演员只要姓杨的,并且不要第一个 List<String> newWomanList = womanList.stream().filter(s -> s.startsWith("杨")).skip(1) .collect(Collectors.toList()); // 把过滤后的男演员姓名和女演员姓名合并到一起 Stream<String> meet = Stream.concat(newManList.stream(), newWomanList.stream()); // 把meet流中的元素作为构造方法的参数,创建演员对象,遍历数据演员类Actor // meet.map(Actor::new).forEach(s -> System.out.println(s.getName())); meet.forEach(name -> { Actor actor = new Actor(name); System.out.println(actor.getName()); }); // 结果:张国立 张卫健 杨幂 } }

(未完,后续补充)

最后

以上就是完美机器猫最近收集整理的关于【Java】Java8新特性 —— Stream流的全部内容,更多相关【Java】Java8新特性内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部