新星计划Day3【JavaSE】 集合 Part1

新星计划Day3【JavaSE】 集合 Part1

👩‍💻博客主页:京与旧铺的博客主页

✨欢迎关注🖱点赞🎀收藏⭐留言✒

🔮本文由京与旧铺原创,csdn首发!

😘系列专栏:java学习

💻首发时间:🎞2022年4月27日🎠

🎨你做三四月的事,八九月就会有答案,一起加油吧

🀄如果觉得博主的文章还不错的话,请三连支持一下博主哦

🎧最后的话,作者是一个新人,在很多方面还做的不好,欢迎大佬指正,一起学习哦,冲冲冲
💬推荐一款模拟面试、刷题神器👉点击进入网站


在这里插入图片描述


🛒导航小助手🎪

🥡511 集合框架与项目的对比与概述

一,集合框架的概述

1.集合,数组都是对多个数组进行存储操作的结构,简称java容器

说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)

2.1、数组在存储多个数据方面的特点:

​ 1. 一旦初始化以后,长度就确定了

​ 2. 数组一旦定义好,其元素的类型也就确定了,我们也就只能操作指定类型的数据了

​ 比如:String[ ] arr;

​ int[ ] arr1;

2.2数组在存储多个数据方面的缺点

​ 1.一旦初始化以后,其长度就不可以修改、

​ 2.数组中提供的方法非常有限,对于添加,删除,插入数据等操作,非常不便,同时效率不高

​ 3.获取数组中的实际元素的个数的需求,数组没有现成的属性或方法可用

​ 4.数组存储数组的特点:有序,可重复,对于无序和不能重复的需求,数组不能满足

🥠512 集合框架涉及到的API

  • Java 集合可分为CollectionMap两种体系
    • Collection接口:单列数据,定义了存取一组对象的方法的集合
    • 单列集合,用来存储一个一个的对象
      • List:元素有序、可重复的集合 “动态”数组
      • Set:元素无序、不可重复的集合 高中讲的集合
    • Map接口:双列数据,保存具有映射关系“key-value对”的集合 高中函数:y=f(x)
    • 双列集合,用来存储一对(key-value)一对的数据

Collection接口继承树

img

Map接口继承树

img

🍜513 Collections接口中的常用方法1

public class CollectionTest{
    public void test1{
        Collection coll=new Arraylist();
        //add(Object e):将元素e添加到集合coll中
        coll.add("AA");
        coll.add("BB");
        coll.add(123);//自动装箱
        coll.add(new Date());
        //size():获取添加的元素的个数
        System.out.println(coll.size());//4
        //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
         Collection coll1=new Arraylist();
        coll.add(456);
        coll.add("CC");
        coll.addAll(coll1);
        System.out.println(coll1.size());
        System.out.println(coll);
        //clear():清空集合元素
        coll.clear();
        //isEmpty():判断当前集合是否为空
        System.out.println(coll.isEmpty());
    }
}

🥂518 Collections接口中的常用方法2

测试类

向Collections接口的实现类的对象中添加数据obj时,要求obj所在类中重写equals()

public class CollectionTest{
    public void test1(){
        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
        coll.add(new Person("Jerry",20));
        //1.contains(Object obj):判断当前集合中是否包含obj
        //我们在判断时,会调用obj对象所在类的equals()
        boolean contains=coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new String("Tom")));
        System.out.println(coll.contains(new Person("Jerry",20)));
        //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
        Collection coll1=Arrays.asList(123,456);
        System.out.println(colls.containsAll(coll1));
    }
}

Person类

public class Person{
    private String name;
    private int age;
    public Person(){
    }
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age=age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        System.out.println("Person equals()....");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age);
    }
}

🍣519 Collections接口中的常用方法2

测试类

  • public void test2{
        //3.remove(Object obj):从当前集合中移除obj元素
        Collection coll=new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new String("Tom"));
            coll.add(false);
            coll.add(new Person("Jerry",20));
        coll.remove(123);
        System.out.println(coll);
        coll.remove(new Person("Jerry",20));
        System.out.println(coll);
        //4.removeAll(Collection coll1):差集:当前集合中移除coll1所有的元素
        Collection coll1=Arrays.asList(123,456);
        coll.removeAll(coll1);
        System.out.println(coll);
    }
    public void test3{
        
        Collection coll=new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new String("Tom"));
            coll.add(false);
            coll.add(new Person("Jerry",20));
        //retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回当前集合
        Collection coll1=Arrays.asList(123,456,789);
        coll.retainAll(coll1);
        System.out.println(coll);
        //equals(Object obj):要想返回true,需要判断当前集合和形参集合的元素相同
        Collection coll1=new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new String("Tom"));
            coll.add(false);
            coll.add(new Person("Jerry",20));
        
    }
    

🍱519 Collections接口中的常用方法3

public void test2{
    
    Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
        coll.add(new Person("Jerry",20));
    //hashCode():返回当前对象的哈希值
    System.out.println(coll.hashCode());
    //8.集合----数组
    Object[] arr=coll.toArray();
    for(int i=0;i<arr.length;i++){
        System.out.println(arr[i]);
    }
    //拓展:数组---集合:调用Arrays类的静态方法asList()
    List<String> list=Arrays.asList(new String[]{"AA","BB","CC"});
    System.out.println(list);
    //iterator():返回Iterator接口的实例,用于遍历集合元素
}

🎫521 使用iterator遍历Collection

Iterator对象称为迭代器(设计模式的一种),主要用于遍历Collection 集合中的元素。
GOF给迭代器模式的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节。迭代器模式,就是为容器而生。类似于“公交车上的售票员”、“火车上的乘务员”、“空姐”。
Collection接口继承了java.lang.Iterable接口,该接口有一个iterator()方法,那么所有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象。
Iterator 仅用于遍历集合,Iterator本身并不提供承装对象的能力。如果需要创建Iterator 对象,则必须有一个被迭代的集合。
集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。

public void IteatorTest{
    public void test1{
         Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
        coll.add(new Person("Jerry",20));
        Iterator iterator=coll.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
         
    }
}  

👝522 迭代器iterator的执行原理

img

⚾523 iterator遍历集合的两种错误写法


/**
 * 集合元素的遍历操作,使用迭代器Iterator接口
 * 1.内部的方法:hasNext()和 next()
 * 2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
 */
public class IteratorTest {

    @Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        //错误方式一:
//        Iterator iterator = coll.iterator();
//        while(iterator.next() != null){
//            System.out.println(iterator.next());
//        }

        //错误方式二:
        //集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
        while(coll.iterator().hasNext()){
            System.out.println(coll.iterator().next());
        }
    }
}

🦺524 Iterator迭代器remove()的使用

public void test2(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);
    //删除集合中的Tom
        Iterator iterator=coll.iterator();
    while(iterator.hasNext()){
        Object obj=iterator.next();
        if("Tom".equals(obj)){
            iterator.remove();
        }
        
    }
    while(iterator.hasNext()){
        System.out.println(iterator.next());
    }
}

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

如果还未调用next()或在上一次调用next方法之后已经调用了remove方法,再调用remove都会报IllegalStateException、

好的,集合的内容我们就进行到这里,在后面的内容中呢我们也会应用到它,现在先对它有一个基础的了解


下期预告:新星计划Day3 【JavaSE】集合 Part2🍩

}

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

}


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

如果还未调用next()或在上一次调用next方法之后已经调用了remove方法,再调用remove都会报IllegalStateException、

好的,集合的内容我们就进行到这里,在后面的内容中呢我们也会应用到它,现在先对它有一个基础的了解

------

下期预告:新星计划Day3 【JavaSE】集合  Part2🍩

觉得文章写的不错的亲亲们,点赞评论走一波,爱你们哦!🥗


结束语🏆🏆🏆
🔥推荐一款模拟面试、刷题神器网站
点击跳转进入网站[点击进入](https://www.nowcoder.com/exam/oj?page=1&tab=%E8%AF%AD%E6%B3%95%E7%AF%87&topicId=220&fromPut=pc_csdncpt_jyjp_java)
1、算法篇(398题):面试必刷100题、算法入门、面试高频榜单
2、SQL篇(82题):快速入门、SQL必知必会、SQL进阶挑战、面试真题
3、大厂笔试真题:字节跳动、美团、百度、腾讯…
  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

京与旧铺

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

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

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

打赏作者

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

抵扣说明:

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

余额充值