集合的基本操作

集合:

在java当中,含有着一些不同的存储数据的相关集合。分为单列集合(Collection)和双列集合(Map)。

在这里插入图片描述

Collection

首先学习Collection来进行展示:

以框框为例子,蓝色的代表的是接口,而红色的是代表的接口的实现类!在Collection当中,又分为两种一个是List,一个是Set!

List的特点:元素可以重复,含有索引,有序(在存和取的过程当中是有序的,先存储的时候,在遍历获取时就是先获取的)。

Set的特点和List完全相反:元素不可重复,无索引,无序。

Collection接口是单列集合的祖宗接口,所有的实现类都要实现这个接口。

那么首先来学习的可以是以Collection为例子:

在这里插入图片描述

由于Collection是接口,所以在实现代码的时候,要创建一个实现类实现这个接口,才能展现出来这个接口当中的所有方法。

基本方法

add

添加元素到collection当中

        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        System.out.println(collection);

可以添加重复的元素

clear

删除元素在collection中:

全部进行清空操作!

Collection<String> collection = new ArrayList<>();
collection.add("he1");
collection.add("he2");
collection.add("he3");
collection.add("he4");
System.out.println(collection);

collection.clear();
System.out.println(collection);
remove

删除指定的元素:

Collection<String> collection = new ArrayList<>();
collection.add("he1");
collection.add("he2");
collection.add("he3");
collection.add("he1");
System.out.println(collection);


boolean isNo = collection.remove("he1");
System.out.println(isNo);
System.out.println(collection);

ArrayList的源码删除:

/*从此列表中删除第一个出现的指定元素(如果存在)。如果列表不包含该元素,则该元素保持不变。更正式地说,删除索引 为 i 的元素,使得 (o==null ? get(i)==null : o.equals(get(i))) (如果存在这样的元素)。如果此列表包含指定的元素(或等效地,如果此列表因调用而更改),则返回 true 。
参数:
o – 要从此列表中删除的元素(如果存在)
返回:
如果此列表包含指定的元素,则为 true*/
public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                //找到一个就直接返回了,直接就删除了!
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}

这个是只会删除一个的情况的。

在这里插入图片描述

contains

处理包含的情况:

        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);


//        boolean isNo = collection.remove("he1");
//        System.out.println(isNo);
//        System.out.println(collection);


        System.out.println(collection.contains("he1"));

查看是否含有h1的元素。

在这里插入图片描述

具体的源码是怎么进行查找的呢?

public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

根据对象所继承Object.equals进行判断的。是判断的是对象是否为同一个对象。

public boolean equals(Object obj) {
    return (this == obj);
}

如果String缓冲池内不存在与其指定值相同的String对象,那么此时虚拟机将为此创建新的String对象,并存放在String缓冲池内。

如果String缓冲池内存在与其指定值相同的String对象,那么此时虚拟机将不为此创建新的String对象,而直接返回已存在的String对象的引用。

 //接着System.out.println(s1.equals(s2));这里的equals在String类中被重写过,用来比较两个字符串的实际内容是否相等,即每一个字符是否相等,重写方法末尾会另做说明!!!因为比较的是字符串内容,s1,s2内容都是hello当然是相等的。 
 
		String ss1 = "aaa";
		//在池子里面又创建了一个!
         String ss2 = new String("aaa");
         System.out.println(ss1 == ss2);  //false
         System.out.println(ss1.equals(ss2));  // true
 
ss2是new出来的,所以重新分配内存地址,当用==判断时,返回false,但是两个字符串的内容相同,所以用equals方法时,返回true

对于含有自己创建的引用数据类型的情况!

        Collection<Student> students = new ArrayList<>();
        students.add(new Student("yyy" , 111));
        students.add(new Student("yyy" , 112));
        students.add(new Student("yy" , 111));
        System.out.println(students);

在这里插入图片描述

Collection<Student> students = new ArrayList<>();
students.add(new Student("yyy" , 111));
students.add(new Student("yyy" , 112));
students.add(new Student("yy" , 111));
System.out.println(students);
System.out.println(students.contains(new Student("yyy" , 111)));

在这里插入图片描述

返回的没有的信息,对于自己创建Students的类是直接使用equals进行判断,判断的是地址!

想要进行数据的判断是要进行方法的重写操作的。

在Students的类当中重写其方法!

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Student student = (Student) o;
    return age == student.age && Objects.equals(name, student.name);
}

在这里插入图片描述

这样就是可以直接根据对象的属性值进行判断了!

size()和isEmpty()
Collection<Student> students = new ArrayList<>();
students.add(new Student("yyy" , 111));
students.add(new Student("yyy" , 112));
students.add(new Student("yy" , 111));
System.out.println(students);
System.out.println(students.contains(new Student("yyy" , 111)));
System.out.println(collection.size());
System.out.println(collection.isEmpty());

在这里插入图片描述

集合的遍历方式

由于使用的是Collection的集合来进行处理操作的。那么想要对集合当中的元素进行遍历操作,就不能使用普通的for循环来进行遍历的。

原因是:在使用for循环的时候要进行索引的获取操作,但是在Collection的Set下面的接口是没有索引的。故不能直接使用for循环来进行遍历操作。

含有下面的方式来进行的遍历操作:

Iterator
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);
        
        Iterator<String> iterator = collection.iterator();
        //含有三个元素he1   he2   he3
        while (iterator.hasNext()) {
            String string = iterator.next();
            System.out.println(string);
        }
    }
}

直接进行遍历操作。使用的是指针来进行遍历的。

首先指向的是元素的第一个位置,使用hasNext的方法判断当前位置是否含有元素,若含有元素就直接进行返回true,使用iterator.next()是来进行先获取元素的,在进行指针的移动操作。不断的进行的移动最终使得将全部的元素都进行了遍历操作。

在这里插入图片描述

在这里插入图片描述

指向一个空指针的情况:抛出
package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String string = iterator.next();
            System.out.println(string);
        }

         System.out.println(iterator.next());
    }
}

在这里插入图片描述

当迭代器当中的指针进行移动操作之后,会使得指针指向一个空的情况。造成一个异常的抛出!

/**
返回迭代中的下一个元素。
返回:
迭代中的下一个元素
抛出:
NoSuchElementException – 如果迭代没有更多元素
 */
E next();
在指针的遍历结束的时候,指针不会进行复位
Collection<String> collection = new ArrayList<>();
collection.add("he1");
collection.add("he2");
collection.add("he3");
System.out.println(collection);

Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
    String string = iterator.next();
    System.out.println(string);
}

System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
每一个循环当中是只能调用一个next的方法的。一次指针的移动,一次判断获取元素的操作。
package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String string1 = iterator.next();
            String string2 = iterator.next();
            System.out.println(string1);
            System.out.println(string2);

        }



    }
}

两次移动好像也是没有问题的,但是当含有的元素为奇数个情况就直接报错。

public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String string1 = iterator.next();
            System.out.println(string1);
            String string2 = iterator.next();
            System.out.println(string2);

        }



    }
}

在这里插入图片描述

指针移动两次,但是元素是只有奇数个的情况,在指向最后一个元素的next的时候会直接找不到那一个元素的。直接就抛异常了。

不能使用集合当中的方法进行添加元素和删除元素
package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {

            String string2 = iterator.next();
            System.out.println(string2);
            if(string2.equals("he3")){
                //进行修改
                System.out.println(collection.remove(string2));
            }
        }
    }
}

在这里插入图片描述

但是可以使用Iterator当中的remove方法进行删除操作,直接删除的Collection当中的方法。

package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {

            String string2 = iterator.next();
            System.out.println(string2);
            if(string2.equals("he3")){
                //进行修改
                iterator.remove();
            }
        }
        System.out.println(collection);


    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值