Java 集合框架概述及Collection接口中常用的方法总结

博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,
👉点击跳转到网站

一、集合框架概述:

  1. 集合,数组都是对多个数据进行存储操作的结构,简称Java容器
    说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中的数据)

  2. 数组在存储多个数据的特点:
    2.1.一旦初始化以后,长度就确定了。
    2.2.数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了
    比如:String[] arr,int[] arr1,Object[] arr2

  3. 数组在存储多个数据的缺点:
    3.1.一旦初始化以后,其长度就不可修改
    3.2.数组中提供的方法非常的有限,对于添加,插入,删除数据等操作,非常不方便,效率不高。
    3.3.获取数组中实际元素个数的需求,数组没有现成的属性或方法可用。
    3.4.数组存储数据元素的特点:有序,可重复。对于无序,不可重复的需求,不能满足。

二、Java集合框架
在这里插入图片描述
Java集合框架体系图:

1、Map接口实现的子类是双列集合,存放的K-V
在这里插入图片描述
2、Collection接口有两个重要的子接口List,Set,他们实现的子类都是单列集合。
在这里插入图片描述

三、Collection接口中的方法的使用:

方法和注释都在代码中写出了

public class CollectionTest {
    @Test
    public void test1(){
        //TODO 结论
        //TODO 向Collection接口的实现类的对象中添加数据obj时,要去obj所在类必须重写equals()
        Collection coll = new ArrayList();

        //1.add(Object e):将元素e添加到集合coll中
        coll.add("abc");
        coll.add("程序员");
        coll.add(123);
        coll.add(new Date());

        //2.size():获取添加的元素的个数
        System.out.println(coll.size());//4

        //3.addAll(Collection coll1):将coll1集合中的元素添加到当前集合中
        Collection coll1 = new ArrayList();
        coll1.add("大牛");
        coll1.add("小白");
        coll.addAll(coll1);
        System.out.println(coll.size());//6

        //4.clear():清空集合元素  不是把coll重新赋值为null,而是里面的元素没有了。
      //  coll.clear();

        //5.isEmpty():判断当前集合是否为空
        System.out.println(coll.isEmpty());//true
        System.out.println("***************************");
        //6.contains(Object obj):判断当前集合中是否包含obj
        //我们在判断时会调用obj对象所在类的equals方法
        boolean con = coll.contains("大牛");
        System.out.println(con); //true

        //7.containsAll(Collection coll2):判断形参coll2中的所有元素是否都存在于当前集合中
        Collection coll2 = Arrays.asList(123,"程序员");
        boolean b = coll.containsAll(coll2);
        System.out.println(b);//true
        System.out.println("*******************");
        //8.remove(Object obj):从当前集合中移除obj元素
        System.out.println(coll);//[abc, 程序员, 123, Sun May 30 21:40:14 CST 2021, 大牛, 小白]
        coll.remove("abc");
        System.out.println(coll);//[程序员, 123, Sun May 30 21:40:14 CST 2021, 大牛, 小白]

        //9.removeAll(Object obj):差集:从当前集合中移除coll1中所有的元素
        Collection coll3 = Arrays.asList("123","大牛");
        coll.removeAll(coll3);
        System.out.println(coll);//[程序员, 123, Sun May 30 21:43:18 CST 2021, 小白]

        //10.retainAll(Collection coll4):交集:获取当前集合和coll集合的交集,并返回给当前集合
        Collection coll4 = Arrays.asList(123,"程序员");
        coll.retainAll(coll4);
        System.out.println(coll);//[程序员, 123]

        //11.equals(Object obj):要想返回true,需要当前集合和形参集合的元素相同
        Collection coll5 = new ArrayList();
        coll5.add("程序员");
        coll5.add(123);
        System.out.println(coll.equals(coll5));//true

        //12.hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());//954693576

        //13.集合转换为数组:toArray()
        Object[] objects = coll.toArray();
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);
        }

        //拓展:数组转换为集合:调用Arrays类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
        System.out.println(list);//[AA, BB, CC]

        List<int[]> ints = Arrays.asList(new int[]{12, 13, 14});
        System.out.println(ints.size());//1

        List<Integer> list1 = Arrays.asList(new Integer[]{12, 13, 14});
        System.out.println(list1.size());//3
    }
}


四、集合元素的遍历操作,使用迭代器Iterator接口

在这里插入图片描述
执行原理:
在这里插入图片描述

  1. 内部的方法:next(),hasNext()
    hasNext():判断是否还有下一个元素
    next():指针下移,将下移后集合位置上的元素返回
  2. 集合对象每调用一次iterator()都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。

案例代码1:

@Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add("程序员");
        coll.add("Hello,world");

        Iterator iterator = coll.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

输出结果如下:
在这里插入图片描述
案例代码2

public class CollectionIterator {
    public static void main(String[] args) {
        Collection col = new ArrayList();
        col.add(new Book("三国演义", "罗贯中", 10.));
        col.add(new Book("小李飞刀", "古龙", 5.1));
        col.add(new Book("红楼梦", "曹雪芹", 34.6));
        //1.先得到col对应的的迭代器
        Iterator iterator = col.iterator();
        //2.使用while循环遍历
//        while (iterator.hasNext()) {
//            Object obj = iterator.next();
//            System.out.println("obj=" + obj);
//        }

        //快捷键 itit
        //显示所有快捷键的快捷键 ctrl+j
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println("obj=" + obj);
        }
        //3.当退出while循环后,这时iterator迭代器,指向最后的元素
//        iterator.next(); //报异常NoSuchElementException
        //4.如果希望再次遍历,需要我们重置迭代器
        iterator = col.iterator();
        System.out.println("第二次遍历");
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println("obj=" + obj);
        }

    }
}

class Book {
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

输出结果如下:

obj=Book{name='三国演义', author='罗贯中', price=10.0}
obj=Book{name='小李飞刀', author='古龙', price=5.1}
obj=Book{name='红楼梦', author='曹雪芹', price=34.6}
第二次遍历
obj=Book{name='三国演义', author='罗贯中', price=10.0}
obj=Book{name='小李飞刀', author='古龙', price=5.1}
obj=Book{name='红楼梦', author='曹雪芹', price=34.6}

3.内部定义了remove方法,可以在遍历的时候,删除集合中的元素,此方法不同于集合直接调用remove()

 @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add("程序员");
        coll.add("Hello,world");

        Iterator iterator = coll.iterator();
        while (iterator.hasNext()){
            Object obj = iterator.next();
            if (obj.equals(123)){
                iterator.remove();
            }
        }

        iterator = coll.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

输出结果:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值