余腾

导航

Collection集合的方法使用小节

Collection集合中的一些方法的使用

package com.yuteng;

import org.junit.Test;

import java.util.*;

/**
 * 一、集合框架的概述
 * 1 集合、数组都是对多个数据进行存储操作的结构,简称Java容器。
 * 说明:此时的存储主要指的是内存层面的存储,不涉及持久化的存储()
 * 2.1 数组在存储多个数据的特点:
 * >一旦初始化以后、其长度就确定了
 * > 数组一旦定义好,其元素的类型也就确定了。我们只能操作指定类型的数据了比如String [] arr  ;int [] arr1
 * 2.2 数组在存储多个数据方面的缺点:
 * >一旦初始化之后,其长度就不可修改。
 * >数组中提供的方法非常有限,对于添加、删除、插入数据等非常不便,同时效率不高如删除第三个元素 三个之后的元素都得提前移一下很麻烦
 * >获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
 * >数组存储特点:有序的、可重复对于无序的、不可重复的需求,数组满足不了
 * <p>
 * 二、 集合框架
 * |----Collection接口:单列集合,用来存储一个一个的对象
 * |-----List接口:有序的可重复的数据 --->“动态”数组
 * |-----ArrayList、LinkedList、Vector
 * |-----Set接口:无序的不可重复的数据 --->高中将的集合
 * |-------HashSet LinkedHashSet、TreeSet
 * |----Map接口:双列集合,用来存储一对(key-value)一对的数据
 * --->高中函数 y=f(x) y相当于value  x相当于key 即一个Key对应一个Value
 * |-------HashMap、LinkedHashMap、TreeMap、HashTable、Properties
 *
 * @version 1.0
 * @author: 余腾
 * @date: 2021-07-21 17:24
 */
public class CollectionTest {
    @Test
    public void test1() {
        Collection coll = new ArrayList();
        // 1 集合 用add()添加
        coll.add("aa");
        coll.add("BB");
        coll.add(123);
        coll.add(new Date());
        // 2 集合的长度用size()
        System.out.println(coll.size());
        Collection coll1 = new ArrayList();
        coll1.add(456);
        coll1.add("CC");
        //3 addAll() 将coll中的元素添加到当前集合
        coll1.addAll(coll);
        System.out.println(coll1.size());
        System.out.println(coll1);
        //4 clear()清空集合元素
        coll.clear();
        coll1.clear();
        //5 isEmpty()
        System.out.println(coll.isEmpty());
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
        coll.add(new Person("Tom", 20));

        //6 、contains(Obj obj)
        System.out.println(coll.contains(new String("Tom")));
        System.out.println(coll.contains(new Person("Tom", 20)));
        //7 、containsAll()
        coll1.add(123);
        coll1.add(456);
        System.out.println("containsAll->>>>>>>>>>>");
        System.out.println(coll.containsAll(coll1));

    }

    @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);
        //8  remove(Obj o)  从当前集合中移除obj元素
        coll.remove(1234);
        System.out.println(coll);
        coll.remove(new Person("Jerry", 20));
        System.out.println(coll);
        //9  removeAll(Collection coll1)  从当前集合中移除coll1中所有的元素
        Collection coll1 = Arrays.asList(123, 4567);
        coll.removeAll(coll1);
        System.out.println(coll);
    }

    @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);
        Collection coll1 = Arrays.asList(123, 456, 789);
//    10    retainAll(Collection coll1):交集:获取当前集合和coll集合的交集,并修改当前集合

        coll.retainAll(coll1);
        System.out.println(coll);
        Collection coll2 = new ArrayList();
        coll2.add(456);
        coll2.add(123);
        coll2.add(new Person("Jerry", 20));
        coll2.add(new String("Tom"));
        coll2.add(false);
        System.out.println(coll.equals(coll2));
    }
    @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);
        Collection coll1 = Arrays.asList(123, 456, 789);
        //11 返回当前对象的哈希值
        System.out.println(coll.hashCode());
        //12  集合---》数组:toArray()
        Object[] arr = coll.toArray();
        for (Object o : arr) {
            System.out.println(o);
        }
        //拓展 数组变为集合
        List<Object> objects = Arrays.asList(arr);
        //小坑
        List ints = Arrays.asList(new int[]{123, 456});
        //结果为1 他认为你是一个整体
        System.out.println(ints.size());
        List integers = Arrays.asList(new Integer[]{123, 456});
        //结果为2  因为Integer是包装类
        System.out.println(integers.size());
        // 13  iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中进行测试
    }

}

posted on 2021-07-21 20:22  余腾  阅读(59)  评论(0)    收藏  举报

Fork me on Gitee