集合框架Collection的使用和原理

集合框架

Collection下分为两大派系:List和Set

  • List:有序,有下标,可重复
  • Set:无序,无下标,不可重复

官方解释: 集合层次结构中的根界面 。 集合表示一组被称为其元素的对象。 一些集合允许重复元素,而其他集合不允许。 有些被命令和其他无序。 JDK不提供此接口的任何直接实现:它提供了更具体的子接口的实现,如SetList该界面通常用于传递集合,并在需要最大的通用性的情况下对其进行操作。

Collection

图片来源: Java:集合,Collection接口框架图 - 那些年的事儿 - 博客园 (cnblogs.com)


Collection的特点是Set的特点

但要根据实现来定,如实例化ArrayList时元素可重复


基本使用

Collection的实现类AbstractCollection,重写了toString方法:用StringBuilder逐个拼接

AbstractCollection

  • removerAll差集,留下两者中都没有的:[1,2]
  • retainAll交集,留下两者中都有的:[3,4,4]
  • clear()清空
public static void removeAndRetainAllTest(Collection collection,Collection collection1){
    collection.add(1);
    collection.add(2);
    collection.add(3);
    collection.add(4);
    collection.add(4);

    collection1.add(3);
    collection1.add(4);
    collection1.add(5);

    collection.removeAll(collection1);
    collection.retainAll(collection1);

    collection.clear();
  //-----------------------------------------------
    /**
     *  removerAll差集,留下两者中都没有的:[1,2]
     *  retainAll交集,留下两者中都有的:[3,4,4]
     *
     *  clear:[]
     */
    System.out.println(collection);
  }

官方推荐元素遍历时用Iterator接口

  1. 迭代器遍历时不允许用集合本身的remove方法


  2. 迭代器的原理是用"指针"指针元素,

  3. 集合自己删除时,不会改变指针位置


public static void iteratorTest(Collection collection){

    Iterator iterator = collection.iterator();
    /**
     * 如果还有下一个元素,
     * 就取出下一个元素
     * 然后删除掉
     */
    while (iterator.hasNext()){

        Object next = iterator.next();

        System.out.print("元素:"+next+"-size:");
        
        iterator.remove();//删除当前指针位置上的元素

        System.out.println(collection.size());

    }

}

iterator


重写对象的equals方法:用属性来比较对象是否相等

Student实体类:

public String name;
public int age;
public char sex;

public Student(){}

public Student(String name,int age,char sex){
    this.age = age;
    this.name = name;
    this.sex = sex;
}

重写toString()
  1. Collection直接打印对象信息

  2. 不再默认直接打印对象地址

@Override
public String toString() {
    return "Student{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", sex=" + sex +
        '}'+"\n";
}

重写equals()
  1. 输入equals或hashCode,找到对应的方法
  2. 回车:直接生成代码模板

equals

class

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    if(o instanceof Student){
        Student student = (Student) o;
        return age == student.age && sex == student.sex && Objects.equals(name, student.name);
    }
    return  false;
}

用new对象的方式删除元素

public static void removeObject(Collection collection){
    Student student = new Student("长歌", 18, '女');
    Student student1 = new Student("世民", 22, '男');
    Student student2 = new Student("则天", 20, '女');

    collection.add(student);
    collection.add(student1);
    collection.add(student2);

    /**
     * [Student{name='长歌', age=18, sex=女}
     * , Student{name='世民', age=22, sex=男}
     * , Student{name='则天', age=20, sex=女}
     * ]
     */
    System.out.println(collection);
    //-------------------------------------------------

    //new对象删除
    collection.remove(new Student("长歌",18,'女'));

    /**
     * new对象删除后:[Student{name='世民', age=22, sex=男}
     * , Student{name='则天', age=20, sex=女}
     * ]
     */
    System.out.println("new对象删除后:"+collection);

    //--------------------------------------------------

    /**
     * Collection保存的是对象地址,
     * 删除后对象还是能用的
     */
    collection.removeAll(collection);

    //Student{name='长歌', age=18, sex=女}
    System.out.println(student);

}

toString

Collection保存的是对象地址,删除后对象还是能用的

collection


不为空和判断是否全部包含

public static void containsArrAndIsEmpty(Collection collection,Collection collection1){

    collection.add(1);
    collection.add(2);
    collection.add(3);

    collection1.add(2);
    collection1.add(3);

    /**
     * 不为空
     * 再判断是否全部包含
     */
    if(!collection1.isEmpty()){
        boolean b = collection.containsAll(collection1);
        boolean a = collection1.containsAll(collection);
        /**
       *  AbstractCollection实现了这个方法
       *
       *  collection1为空时自动返回true
       */
        //true-false
        System.out.println(b+"-"+a);
    }

}

containsAll
contains


main方法


 public static void main(String[] args) {
   /**
    * Collection接口只能由子类实现
    */
   Collection collection = new ArrayList();
   Collection collection1 = new ArrayList();

   //removeAndRetainAllTest(collection,collection1);

   //iteratorTest(collection);

   //removeObject(collection);

   containsArrAndIsEmpty(collection,collection1);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

helloses

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

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

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

打赏作者

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

抵扣说明:

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

余额充值