Java集合 上(笔记十九)

一、Java集合框架概述

1、Java集合框架概述

为什么使用集合,而不使用数组,下面来看看数组的弊端:
在这里插入图片描述

a、集合的使用场景

在这里插入图片描述
在这里插入图片描述

b、Collection接口继承树

在这里插入图片描述
详细版:
在这里插入图片描述
从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射。Collection 接口又有 3 种子类型,List、Set 和 Queue,再下面是一些抽象类,最后是具体实现类,常用的有 ArrayList、LinkedList、HashSet、HashMap 等等。

c、Map接口继承树

在这里插入图片描述

二、 Collection接口方法

在这里插入图片描述

1、Collection 接口方法

在这里插入图片描述
在这里插入图片描述
实例:

package ex.helllo.er.day25;
import org.junit.Test;
import java.util.*;
/**
* @Description: 
* @Author: dyq
* @Date: 2021/5/8
*/
public class CollectionTest {
    @Test
    public void  Test1(){
        Collection collection = new ArrayList();
        //add(Object e)
        collection.add("AA");
        collection.add("BB");
        collection.add("CC");
        collection.add(new Date());
        collection.add(123);//自动装箱
        collection.add('女');
        //size();
        System.out.println("集合长度:"+collection.size()); //集合长度:6

        //addAlL(Collection coll1):将coLl1集合中的元素添加到当前的集合中
        Collection collection1 = new ArrayList();
        collection1.add("牛逼");
        collection1.add(682645223L);
        collection.addAll(collection1);
        System.out.println("集合长度:"+collection.size()); //集合长度:8

        //boolean isEmpty() //判断集合是否为空
        System.out.println("判断集合是否为空"+collection.isEmpty()); //false
        //遍历集合
        System.out.println("*******遍历集合***********");
        for (Object collection2:collection) {
            System.out.println(collection2);
        }
        //清空集合
        collection.clear();
        System.out.println("清空集合后,判断集合是否为空"+collection.isEmpty()); //true
    }

    @Test
    public void  Test2(){
        Collection coll = new ArrayList();
        coll.add("AA");
        coll.add(new Date());
        coll.add(123);//自动装箱
        coll.add('女');
        coll.add(new String("Tom"));
//        Person p =new Person("Jerry",20);
//        coll.contains(p);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        //1. boolean contains(Object o);判断当前集合中是否包含obj
        //我们在判断时会调用obj对象所在类的equals()
        boolean contains = coll.contains(123);
        System.out.println(contains);//true
        System.out.println(coll.contains(new String("Tom"))); //true
       // System.out.println(coll.contains(p));//true
        System.out.println(coll.contains(new Person("jak",20)));//false

        //2. boolean containsAll(Collection<?> c);判断形参coll1中的所有元紊是否都存在于当前集合电
        Collection coll1 = Arrays.asList(123,456);
        System.out.println(coll.containsAll(coll1)); //false
    }

    @Test
    public void  Test3(){
        Collection coll3 = new ArrayList();
        //add(Object e)
        coll3.add(123);
        coll3.add(345);
        coll3.add("678");
        coll3.add('男');

       boolean q = coll3.remove(123);
        System.out.println(q); //true
        System.out.println(coll3); //[345, 678, 男]

        //removeAll(Collection coll1):从当前集合中移除coll1当中所有的元素
        Collection coll1 = Arrays.asList(123,456);
        coll3.removeAll(coll1);
        System.out.println(coll1); //[123, 456]
    }

    @Test
    public void  Test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(345);
        coll.add(678);
        coll.add('女');

        //5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
//        Collection coll1 = Arrays.asList(123,345,'男');
//        coll.retainAll(coll1);
//        System.out.println(coll); //[123, 345]

        //equals(Object obj):
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(345);
        coll1.add(678);
        coll1.add('女');

        System.out.println(coll.equals(coll1)); //true  如果123和345调换位置结果就为flase  因为这是有序的集合
    }
    @Test
    public void  Test5(){
        Collection coll = new ArrayList();
        coll.add("AA");
        coll.add(new Date());
        coll.add(123);//自动装箱
        coll.add('女');
        coll.add(new String("Tom"));
        coll.add(false);
         //hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode()); //1440641423

        //集合----》数组:toArray()
        Object[] arr = coll.toArray();
        for (int i =0;i<arr.length;i++){
            System.out.print(arr[i]+"  "); //AA  Sat May 08 17:05:40 CST 2021  123  女  Tom  false
        }

        //拓展:数组----》集合
        List<String> list= Arrays.asList(new String[]{"AA", "BB", "CC", "DD"}); //[AA, BB, CC, DD]
        System.out.println(list);

        List arr1 = Arrays.asList(123,456);
        System.out.println(arr1); //[123, 456]

        List arr3 = Arrays.asList(new int[]{123,456});
        System.out.println(arr3.size()); //1

        List arr2 = Arrays.asList(new Integer[]{13,56});
        System.out.println(arr2.size());//2
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北街风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值