Collection在JDK8之后增加的新特性

Collection是单列集合体系的最顶端.大家肯定都不陌生.但是JDK8之后增加了一些非常好用的功能.今天我们就来挨个演示一个.
首先我们先来介绍第一个删除集合中满足某个要求的所有的元素

[AppleScript] 纯文本查看 复制代码

?

1

default boolean removeIf(Predicate<? super E> filter)


我们先用传统的方式实现这个要求:

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

public class Demo {

    public static void main(String[] args) {

        //删除所有姓张的元素

        ArrayList<String> list = new ArrayList<>();

        list.add("张无忌");

        list.add("张三丰");

        list.add("赵敏");

        list.add("周芷若");

        list.add("张翠山");

        for (int i = list.size() - 1; i >= 0; i--) {

            if (list.get(i).startsWith("张")){

                list.remove(i);

            }

        }

        System.out.println(list);

    }

}


我们用这个新特性来实现同样的功能只需要一句代码就可以实现:

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

public class Demo {

    public static void main(String[] args) {

        //删除所有姓张的元素

        ArrayList<String> list = new ArrayList<>();

        list.add("张无忌");

        list.add("张三丰");

        list.add("赵敏");

        list.add("周芷若");

        list.add("张翠山");

        list.removeIf(s -> s.startsWith("张"));

        System.out.println(list);

    }

}


我们知道ArrayList是线程不安全的.在JDK8之前,我们不能在多线程中去遍历这个集合.JDK8之后.我们可以用多线程遍历集合了.大大提高了我们遍历集合的效率.我们下面通过一个多线程计算集合中所有元素的和的案例来演示一下.

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

public class Atest {

    int sum = 0;

    List<Integer> strList = createList();

 

    public static void main(String[] args) {

        new Atest().mytest();

    }

 

    /**

     * 多线程计算list中数值的和

     * 测试spliterator遍历`

     */

    public void mytest() {

        Spliterator<Integer> spliterator01 = strList.spliterator();

        Spliterator<Integer> spliterator02 = spliterator01.trySplit();

        Spliterator<Integer> spliterator03 = spliterator01.trySplit();

        Spliterator<Integer> spliterator04 = spliterator02.trySplit();

        MyThread t01 = new MyThread(spliterator01);

        MyThread t02 = new MyThread(spliterator02);

        MyThread t03 = new MyThread(spliterator03);

        MyThread t04 = new MyThread(spliterator04);

        t01.start();

        t02.start();

        t03.start();

        t04.start();

        //等所有的线程都执行完成后输出计算结果

        try {

            Thread.sleep(15000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        System.out.println("结果为:" + sum);

    }

 

    class MyThread extends Thread {

        private Spliterator<Integer> list;

 

        public MyThread(Spliterator<Integer> list) {

            this.list = list;

        }

 

        @Override

        public void run() {

            String threadName = Thread.currentThread().getName();

            System.out.println("线程" + threadName + "开始运行-----");

            list.forEachRemaining(num -> {

                sum += num;

                System.out.println(num + " " + threadName);

                try {

                    Thread.sleep(1000);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            });

        }

 

    }

 

 

    private static List<Integer> createList() {

        List<Integer> result = new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            result.add(i);

        }

        System.out.println(result);

        return result;

    }

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值