专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java中collection常用方法详解

Java中collection常用方法详解

更新时间:2020-08-07 16:21:49 来源:动力节点 浏览2878次

Collection方法接口介绍

Collection接口有3种子类型集合:List、Set和Queue,再下面是一些抽象类,最后是具体实现类,常用的有ArrayList、LinkedList、HashSet、LinkedHashSet、ArrayBlockingQueue等,下面是Collection的所有方法。

java中collection方法

这些方法即可以操作Set集合,也可以操作Queue和List集合,下面分别使用Collection集合接口的方法说明

java中collection方法

下面是主要方法的演示:  

@Test
    @SuppressWarnings("all")
    public void testCollection() {
        // 创建Collection接口的实现
        Collection collection = new ArrayList<>();
        // 添加元素
        collection.add("嘻嘻");
        String src = "????";
        collection.add(src);
        System.out.println(collection);

        // 创建Collection的实现
        Collection coll = new HashSet<>();
        coll.add("?");
        coll.add("?");
        coll.add("?");
        System.out.println(coll);
        // 添加一个集合数据
        collection.addAll(coll);
        // 输出集合的长度
        System.out.println(collection);
        // 判断是否包含
        System.out.println(collection.contains("?"));
        // 移除元素
        collection.remove("?");
        // 添加对象
        collection.add(new Person("张三", 23, 5000d));
        // 当认为两个对象属性一致,相等时候,需重写hashCode 和 equals方法
        System.out.println(collection.contains(new Person("张三", 23, 5000d)));

        System.out.println("-------");
        collection.add(null);
    
        Collection collection1 = new ArrayList<>();
        collection1.add("嘻嘻");
        collection1.add("?");
        // 求两个集合的交集(只保留collection1存在的元素)
        collection.retainAll(collection1);
        System.out.println(collection);
        // 清空元素
        collection.clear();
        System.out.println(collection);
    }

java8新特性操作集合

使用lambda表达式遍历集合

java8为Collection的父接口(Iterable)提供了一个默认的Foreach方法,我们可以使用它进行集合遍历 

 @Test
    public void testForeach() {
        Collection collection = new ArrayList<>();
        collection.add("i");
        collection.add("love");
        collection.add("china");
        // foreach遍历
        collection.forEach(e-> System.out.println(e));
        // 可以使用方法引用简写
        collection.forEach(System.out::println);
        // 或者迭代器的forEachRemaining方法
       collection.iterator().forEachRemaining(System.out::println);
    }

使用java8的predicate操作集合  

 @Test
    public void testPredicate() {
        Collection collection = new ArrayList<>();
        // 添加0-49
        for (int i = 0; i < 50; i++) {
            collection.add(i);
        }

        // 移除10-49的数字
        collection.removeIf(e -> (e > 9 && e < 50));
        System.out.println(collection);// 输出[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    }

基于流操作集合

java8之后引入了Stream相关流操作java集合,通过流大大简化了对集合操作

@Test

    public void testIntStream() {
        Collection collection = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            collection.add(random.nextInt(100));
        }
        System.out.println(collection);
        // collection存储的数值是包装类型,可以将其转换为IntStream
        IntStream intStream = collection.stream().mapToInt(e -> e);
        // intStream.forEach(System.out::println);
        System.out.println(collection.stream().mapToInt(e -> e).sum());
        // 输出最大值
        collection.stream().mapToInt(e -> e).max().ifPresent(System.out::println);
        // 输出最小值
        collection.stream().mapToInt(e -> e).min().ifPresent(System.out::println);
        // 统计大于50的数
        System.out.println(collection.stream().filter(e -> e > 50).count());
        // 原集合每一个值加1
        collection.stream().mapToInt(e-> e+1).forEach(System.out::println);
        // 排序
        collection.stream().mapToInt(e-> e).sorted().forEach(System.out::println);
        // 原数值每一个元素扩大2倍
        int[] ints = collection.stream().mapToInt(e -> e << 1).toArray();
        // 输出原数组
        System.out.println(Arrays.toString(ints));
        // 将数组转流
        IntStream stream = Arrays.stream(ints);
        // 输出流平均数
        System.out.println(stream.average().getAsDouble());
    }

以上就是动力节点java培训机构的小编针对“Java中collection常用方法详解”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>