好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

手把手带你了解Java-Stream流方法学习及总结

前言

Stream是一个来自数据源的元素队列并支持聚合操作,其中具有以下特性:

Stream只负责计算,不存储任何元素,元素是特定类型的对象,形成一个队列 数据源可以实集合、数组、I/O channel、generator等 聚合操作有类似SQL的:filter、map、match、sorted等操作 Stream流的执行类似于懒加载,用户使用时才执行相应操作 可消费性;Stream只能被消费一次,被消费后需要重新生成

本文总结了部分在日常开发中经常接触到的一些Stream流相关的方法,不足之处或有错误欢迎留评,总结的几个方法如下:

void forEach() : 迭代流中的数据 Stream map() : 用于映射每个元素到对应的结果 Stream filter() : 条件过滤器 Stream sorted() : 排序 R collect() : 流数据 -> 集合/数组 待补充…

forEach()

forEach()源码:

?

1

void forEach(Consumer<!--? super T--> action);

Stream().forEach() : 迭代流中的数据

forEach() 的返回类型为void,不会产生新的流

举个栗子:

?

1

2

3

4

5

6

7

8

9

public void testForEach() {

      /**

       * 使用forEach()内部迭代

       * ints()表示整数类型

       * limit()表示限制流个数(次数)

       */

      Random random = new Random();

      random.ints().limit( 10 ).forEach(System.out::println);

}

运行结果:

再举个灵活一点的栗子:

?

1

2

3

4

5

6

7

8

9

10

11

public void testForEach() {

     /**

      * 使用forEach()转换集合类型,如List->Map

      */

     AtomicInteger i = new AtomicInteger(); //原子类型

     Map<Integer,String> map = new HashMap<>();

     List<String> list = new ArrayList<>(Arrays.asList( "Hello" , "," , "world" ));

     list.stream().forEach(s->{

         map.put(i.getAndIncrement(),s);

     });

}

对于forEach()方法在本人日常开发中常用于:

使用内循环对集合进行遍历 使用foreach方法将List转为Map形式

map()

map()源码:

?

1

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

Stream().map() 用于映射每个元素到对应的结果

返回类型为Stream,map()会产生新的流并返回

举个栗子:

?

1

2

3

4

5

6

7

8

9

10

11

12

public void testMap(){

     /**

      * map()获取list每个元素的平方值

      * distinct()去重操作

      * collect()将map()产生的新的流转为List类型

      */

     List<Integer> list = Arrays.asList( 1 , 2 , 3 );

     list.stream().map(num -> num * num)

                  .distinct()

                  .collect(Collectors.toList())

                  .forEach(System.out::println);

}

filter()

filter()源码:

?

1

Stream<T> filter(Predicate<? super T> predicate);

Stream().filter()为条件过滤器

举个栗子:

?

1

2

3

4

5

6

7

8

9

10

public void testFilter(){

     /**

      * filter()过滤空字符串

      * count()统计符合条件的个数,返回类型long

      */

     List<String> list = new ArrayList<>(Arrays.asList( "ab" , "" , "abc" , "" , "acd" ));

     long count = list.stream()

                      .filter(str -> str.isEmpty())

                      .count();

}

sorted()

sorted()源码:

?

1

2

Stream<T> sorted();

Stream<T> sorted(Comparator<? super T> comparator);

Stream支持两种方式的排序:

无参方法默认为自然排序sorted(Comparator comp) 按自定义比较器进行排序

仍然是举个栗子:

1)无参方法

?

1

2

3

4

5

6

7

8

public void testSorted(){

     /**

      * sort()无参默认为自然排序

      * 返回类型Stream 会产生新的流

      */

     List<String> list = new ArrayList<>(Arrays.asList( "aaa" , "ccc" , "bbb" ));

     list.stream().sorted().forEach(System.out::println);

}

执行结果:

2)带参方法(User类由name和age组成)

先比较年龄,按从小到大排序 若年龄相等,则按性名自然排序

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public void testSorted(){

     /**

      * sort(Comparator comp)按自定义比较器进行排序

      * 返回类型同样是Stream 会产生新的流

      */

     List<User> userList = new ArrayList<>(Arrays.asList(

        new User( "zhangsan" , 22 ),

        new User( "wangwu" , 22 ),

        new User( "badao" , 32 ),

        new User( "kongfu" , 16 )

     ));

     Stream<User> sorted = userList.stream().sorted((x, y) -> {

         if (x.getAge() == y.getAge()) { // 使用流中的序列两两进行比较

             return x.getName()测试数据pareTo(y.getName());

         } else {

             return x.getAge() - y.getAge(); //顺序

             // y.getAge() - x.getAge() 为逆序

         }

     });

     sorted.forEach(System.out::println);

}

执行结果:

collect()

collect()源码:

?

1

2

3

<R> R collect(Supplier<R> supplier,

               BiConsumer<R, ? super T> accumulator,

               BiConsumer<R, R> combiner);

stream().collect() 由三个参数构成 :

1.Supplier 生产者,返回最终

2.结果BiConsumer<R, ? super T> accumulator,累加器 :

其中 R 为要返回的集合, ? super T 为遍历过程中的每个参数,相当于add操作

3.BiConsumer<R,R> combiner,合并器 :

有并行流时才会使用,相当于将第二部操作形成的list添加到最终的list,addAll操作

举个栗子:

1)new()

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public void testCollect(){

 

     Stream<String> stream = Stream.of( "hello" , "world" , "hello,world" );

    

     // new()

     List<String> list = stream.collect(Collectors.toList()); //List

     // 指定集合类型,如ArrayList

     ArrayList<String> arrayList = stream.collect(Collectors.toCollection(ArrayList:: new ));

     //Set

     stream.collect(Collectors.toSet());

     // 指定HashSet

     HashSet<String> hashSet = stream.collect(Collectors.toCollection(HashSet:: new ));

     // 拼接字符串

     String str = stream.collect(Collectors.joining());

2)new() -> add() -> addAll()

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public void testCollect(){

     /**

      * 参数传递行为相当于: new() -> add() -> addAll()

      */

     Stream<String> stream = Stream.of( "hello" , "world" , "hello,world" );

     // new() -> add() -> addAll()完整演示

     HashMap<String,String> map = stream.collect(HashMap:: new ,(x,y)->{

         x.put(y,y); // x 为集合,y 为当前遍历元素,以当前遍历元素作为kv

     },HashMap::putAll);

     map.forEach((x,y)->{

         System.out.println(x+ " : " +y);

     });

   

执行结果:

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/weixin_55932383/article/details/119741519

查看更多关于手把手带你了解Java-Stream流方法学习及总结的详细内容...

  阅读:14次