Collection集合

Collection集合

总结于尚硅谷
一、集合框架的概述

  • 1.集合、数组都是对多个数据进行存储操作的结构,简称java容器。
  • 说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)
  • 2.1数组在存储多个数据方面的特点:

一旦初始化以后,其长度就确定了。
数组一旦定义好,其元素的类型页就确定了。我们页就只能操作指定类型的数据了。
比如:String[] arr;int [] arr1;Object [] arr2;

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

一旦初始化以后,其长度不可修改了
数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便。
获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
数组存储数据的特点:有序。可重复。对于无序、不可重复的需求,不能满足。

二、集合框架

|----Collection接口:单列集合,用来存储一个一个的对象
|----List接口:存储有序的、可重复的数据。–>”动态“数组
|—ArrayList、LinkList、Vector
|----Set接口:存储无序的、不可重复的数据。–>高中讲的”集合“
|—HashSet、LinkedHashSet、TreeSet
|———Map接口:双列集合,用来存储一对(key-value)一对的数据 -->高中函数:y=f(x)
|-----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties

  • 三、Collection接口中的方法的使用

add(Object e):将元素e添加到集合coll中
size():获取添加的元素个数
addAll(Collection coll1):将coll1的元素添加到当前的集合中
clear():清空集合元素
isEmpty():判断当前集合元素是否为0个
contains(Object obj):判断当前集合中是否包含obj
containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
removeAll(Collection coll1):差集:从当前集合中移除coll1中的所有的元素
retainAll(Collection coll1):交集
equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
hashCode():返回当前对象的哈希值
集合—>数组:toArray()
拓展:数组—>集合:调用Arrays类的静态方法asList()
iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试

集合元素的遍历操作,使用迭代器Iterator接口
1.内部的方法:hasNext()和next()
2集合对象每次调用iterator()方法都得到一个全新的迭代器对象。
默认游标都在集合的第一个元素之前
3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()

在这里插入图片描述

CollectionTest类

/**
 * 一、集合框架的概述
 *
 * 1.集合、数组都是对多个数据进行存储操作的结构,简称java容器。
 *  说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)
 *
 *  2.1数组在存储多个数据方面的特点:
 *      》一旦初始化以后,其长度就确定了。
 *      》数组一旦定义好,其元素的类型页就确定了。我们页就只能操作指定类型的数据了。
 *      比如:String[] arr;int [] arr1;Object [] arr2;
 *
 *  2.2数组在存储多个数据方面的缺点:
 *       》一旦初始化以后,其长度不可修改了
 *       》数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便。
 *       》获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
 *       》数组存储数据的特点:有序。可重复。对于无序、不可重复的需求,不能满足。
 *
 *  二、集合框架
 *      |----Collection接口:单列集合,用来存储一个一个的对象
 *          |----List接口:存储有序的、可重复的数据。-->”动态“数组
 *              |---ArrayList、LinkList、Vector
 *          |----Set接口:存储无序的、不可重复的数据。-->高中讲的”集合“
 *              |---HashSet、LinkedHashSet、TreeSet
 *
 *      |---——Map接口:双列集合,用来存储一对(key-value)一对的数据 -->高中函数:y=f(x)
 *          |-----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
 *
 *
 * 三、Collection接口中的方法的使用
 * add(Object e):将元素e添加到集合coll中
 *size():获取添加的元素个数
 *addAll(Collection coll1):将coll1的元素添加到当前的集合中
 *clear():清空集合元素
 *isEmpty():判断当前集合元素是否为0个
 *contains(Object obj):判断当前集合中是否包含obj
 *containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
 *removeAll(Collection coll1):差集:从当前集合中移除coll1中的所有的元素
 *retainAll(Collection coll1):交集
 *equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
 *hashCode():返回当前对象的哈希值
 *集合--->数组:toArray()
 *拓展:数组--->集合:调用Arrays类的静态方法asList()
 
 *iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
 */
package com.day0306_1;

import org.junit.jupiter.api.Test;

import java.util.*;




public class CollectionTest {

    @Test
    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();
        coll1.add(456);
        coll1.add("CC");
        coll1.addAll(coll);

        System.out.println(coll1.size());//6
        System.out.println(coll);

        //clear():清空集合元素
        coll.clear();

        //isEmpty():判断当前集合元素是否为0个
        System.out.println(coll.isEmpty());


    }

    /**
     * Collection接口中声明的方法的测试
     *
     * 结论:
     * 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()
     *
     */

    @Test
    public void test2(){
        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
//        Person p=new Person("Jerry",20);
//        coll.add(p);
        coll.add(new Person("Jerry",20));
        //contains(Object obj):判断当前集合中是否包含obj
        //我们在判断是会调用obj对象所在类的equals()。
        boolean contains = coll.contains(123);
        System.out.println(contains);//true
        System.out.println(coll.contains(new String("Tom")));//true调用equals方法
//        System.out.println(coll.contains(p));//true
        System.out.println(coll.contains(new Person("Jerry",20)));//false-->true

        //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
        Collection coll1= Arrays.asList(123,4567);
        System.out.println(coll.containsAll(coll1));

    }

    @Test
    public void test3(){
        //3.remove(Object obj):从当前集合中移除obj元素
        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        coll.remove(1234);
        System.out.println(coll);

        coll.remove(new Person("Jerry",20));
        System.out.println(coll);

        //4.removeAll(Collection coll1):差集:从当前集合中移除coll1中的所有的元素
        Collection coll1= Arrays.asList(123,4567);
        coll.removeAll(coll1);
        System.out.println(coll);



    }

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

        //5.retainAll(Collection coll1):交集
//        Collection coll1= Arrays.asList(123,456,789);
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
        Collection coll1=new ArrayList();
        coll1.add(123);
        coll1.add(456);
        coll1.add(new Person("Jerry",20));
        coll1.add(new String("Tom"));
        coll1.add(false);

        System.out.println(coll.equals(coll1));

    }

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

        //7.hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());

        //8.集合--->数组:toArray()
        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);

//        List arr1 = Arrays.asList(new int[]{123, 456});//[[I@101df177]
        List arr1 = Arrays.asList(123, 456);
        System.out.println(arr1);//[123, 456]

        List arr2 = Arrays.asList(new Integer[]{123, 456});//[[I@101df177]
        System.out.println(arr2);//[123, 456]

        //9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
    }

}

IteratorTest 类

package com.day0306_1;


import org.junit.jupiter.api.Test;

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

/**
 * 集合元素的遍历操作,使用迭代器Iterator接口
 * 1.内部的方法:hasNext()和next()
 * 2集合对象每次调用iterator()方法都得到一个全新的迭代器对象。
 * 默认游标都在集合的第一个元素之前
 * 3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()
 *
*/
public class IteratorTest {


    @Test
    public void test1(){
        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();

        //方式一:
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        //报异常:NoSuchElementException
//        System.out.println(iterator.next());

        //方式二:不推荐
//        for (int i = 0; i < coll.size(); i++) {
//            System.out.println(iterator.next());
//        }

        //方式三:推荐
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }

    //错误写法
    @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());
//        }

        //错误方式二 :

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



    }


    //测试Iterator中的remove()
    //如果还未调用next或在上一次调用next方法之后已经调用了remove方法,
    //再调用remove都会报IllegalStateException。


    @Test
    public void test3(){
        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.hasNext()){
            Object obj=iterator.next();
            if("Tom".equals(obj)){
                iterator.remove();
            }
        }
        //遍历集合
        iterator= coll.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }


}



ForTest 类

package com.day0306_1;

import org.junit.jupiter.api.Test;

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

/**
 *
 * jdk5.0新增了foreach循环,用于遍历数组和集合
 *
 *
 */
public class ForTest {

    @Test
    public void test1(){

        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        //for (集合元素的类型 局部变量:集合对象)
        for (Object obj:coll) {
            System.out.println(obj);
        }


    }
    @Test
    public void test2(){
        int [] arr =new int[]{1,2,3,4,5,6};
        //for (数组元素的类型 局部变量:数组对象)

        for (int i:arr) {
            System.out.println(i);


        }

    }

    //练习
    @Test
    public void test3(){

        String [] arr =new String[]{"MM","MM","MM"};

//        //方式一普通for循环
//        for (int i = 0; i < arr.length; i++) {
//            arr[i]="GG";
//        }

        //方式二:增强for循环
        for (String s:arr
             ) {
            s="GG";
        }


        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }


    }


}

Person类

package com.day0306_1;

import java.util.Objects;

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);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

日星月云

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

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

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

打赏作者

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

抵扣说明:

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

余额充值