【Java笔记】集合(Collection接口)的使用

面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储。使用 Array 存储对象方面具有一些弊端,而 Java 集合就像一种容器,可以动态地把多个对象的引用放入容器中。

目录

数组在内存存储方面的特点

数组在存储数据方面的弊端

Java集合的框架

Collection接口的使用


数组在内存存储方面的特点

1. 数组初始化以后,长度就确定了

2. 数组声明的类型就决定了进行元素初始化时的类型

数组在存储数据方面的弊端

1. 数组初始化以后,长度就不可变了,不便于扩展

2. 数组中提供的属性和方法少,不便于进行添加、删除、插入等操作,且效率不高。同时无法直接获取存储元素的个数

3. 数组存储的数据是有序的、可以重复的,存储数据单一

Java集合的框架

Collection 接口:单列集合,用来存储一个一个的对象

        List 接口:存储有序的、可重复的数据(“动态” 数组)

                ArrayList、LinkedList、Vector

        Set 接口:存储无序的、不可重复的数据

                HashSet、LinkedHashSet、TreeSet

Map 接口:双列集合,用来存储一对(key - value)的数据(映射)

                HashMap、LinkedHashMap、TreeMap、Hashtable、Properties

Collection接口的使用

Collection 接口的方法
添加add(Object obj)
addAll(Collection coll)
获取有效元素的个数int size()
清空集合void clear()
是否是空集合boolean isEmpty()
是否包含某个元素boolean contains(Object obj)是通过元素的 equals 方法来判断是否是同一个对象
boolean containsAll(Collection coll)也是调用元素的 equals 方法来比较,拿两个集合的元素挨个比较
删除boolean remove(Object obj)通过元素的 equals 方法判断是否是要删除的元素。(只会删除找到的第一个元素)
boolean removeAll(Collection coll)取当前集合的差集
取两个集合的交集boolean retainAll(Collection coll)把交集的结果存在当前集合中,不影响 coll
集合是否相等boolean equals(Object obj)
转成对象数组

Object [ ] toArray

获取集合对象的哈希值hashCode()
遍历iterator()返回迭代器对象,用于集合遍历
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

public class CollectionTest {
    @Test
    public void test1(){
        Collection coll = new ArrayList();
        //add()
        coll.add("AA");
        coll.add(123);
        coll.add(new Date());
        //size()
        System.out.println(coll.size());
        //addAll()
        Collection coll1 = new ArrayList();
        coll1.add("CC");
        coll1.add(456);
        coll.addAll(coll1);
        System.out.println(coll.size());
        //clear()
        coll.clear();
        //isEmpty()
        System.out.println(coll.isEmpty());
    }
}

>>> 3
    5
    true
import org.junit.Test;
import java.util.*;

public class CollectionTest {
    @Test
    public void test1(){
        Collection coll = new ArrayList();
        //add()
        coll.add("AA");
        coll.add(123);
        coll.add(new Person("Tom",20));
        coll.add(false);
        coll.add("DD");
        boolean contains = coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new Person("Tom",20)));//需要重写 equals()
        //containsAll()
        Collection coll1 = Arrays.asList("AA",456);
        System.out.println("containsAll():" + coll.containsAll(coll1));
        //remove()
        coll.remove(123);
        System.out.println("remove():" + coll);
        //removeAll()
        coll.removeAll(coll1);
        System.out.println("removeAll():" + coll);
        //retainAll()
        Collection coll2 = Arrays.asList(false,2345);
        coll.retainAll(coll2);
        System.out.println("retainAll():" + coll);
        //hashCode()
        System.out.println("hashCode():" + coll.hashCode());
        coll.add(678);
        //集合 ——> 数组 toArray()
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println("toArray():" + arr[i]);
        }
        //数组 ——> 集合 asList()
        List<String> list = Arrays.asList(new String[]{"AA","BB"});
        System.out.println("asList():" + list);
    }
}
class Person{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object o) {
        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);
    }
}

>>> true
    true
    containsAll():false
    remove():[AA, CollectionTest.Person@22927a81, false, DD]
    removeAll():[CollectionTest.Person@22927a81, false, DD]
    retainAll():[false]
    hashCode():1268
    toArray():false
    toArray():678
    asList():[AA, BB]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

java小白。。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值