11-2 Java集合----Collection接口方法

11-2 Java集合----Collection接口方法

一、Collection 接口
1.Collection 接口是 List、Set 和 Queue 接口的父接口,该接口里定义的方法既可用于操作 Set 集合,也可用于操作 List 和 Queue 集合。 
2.JDK不提供此接口的任何直接实现,而是提供更具体的子接口(如:Set和List)
实现。 
3.在 Java5 之前,Java 集合会丢失容器中所有对象的数据类型,把所有对象都当成 Object 类型处理;从 JDK 5.0 增加了泛型以后,Java 集合可以记住容器中对象的数据类型。
4.向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals();

二、Collection接口的常用方法

1、添加
 (1)add(Object obj)
 (2)addAll(Collection coll)
2、获取有效元素的个数:int size()
3、清空集合: void clear()
4、是否是空集合:  boolean isEmpty()
5、是否包含某个元素
 (1)boolean contains(Object obj):是通过元素的equals方法来判断是否
是同一个对象
 (2)boolean containsAll(Collection c):也是调用元素的equals方法来比
较的。拿两个集合的元素挨个比较。
6、删除
 (1)boolean remove(Object obj) :通过元素的equals方法判断是否是
要删除的那个元素。只会删除找到的第一个元素
 (2)boolean removeAll(Collection coll):取当前集合的差集
7、取两个集合的交集
 (1)boolean retainAll(Collection c):把交集的结果存在当前集合中,不
影响c 8、集合是否相等
 (2)boolean equals(Object obj)
9、转成对象数组: Object[] toArray()
10、获取集合对象的哈希值: hashCode()
11、遍历: iterator():返回迭代器对象,用于集合遍历

Person类:

package java1;

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);
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

CollectionTest:

package java1;
 
import org.junit.Test;
 
import java.util.*;
 
/**
 * 二、集合框架
 * |----Collection接口:单列集合,用来存储一个一个的对象
 * 		|----List接口:存储有序的、可重复的数据。  -->“动态”数组
 * 			|----ArrayList、LinkedList、Vector
 * <p>
 * 		|----Set接口:存储无序的、不可重复的数据   -->高中讲的“集合”
 * 			|----HashSet、LinkedHashSet、TreeSet
 * <p>
 * 		|----Map接口:双列集合,用来存储一对(key - value)一对的数据   -->高中函数:y = f(x)
 * 			|----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
 * <p>
 * 三、Collection接口中的方法的使用
 */
public class CollectionTest {
 
    @Test
    public void test() {
        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");
        coll.addAll(coll1);

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

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

        //isEmpty():判断当前集合是否为空
        System.out.println(coll.isEmpty());
    }

    @Test
    public void test1() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
//        Person p = new Person("Jerry",20);
//        coll.add(p);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);
        //1.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
//        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 test2() {
        //3. remove(Object 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, 456);
        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);

        //retainAll(Collection coll1):获取当前集合和coll1集合的交集,并返回当前集合
//        Collection coll1 = Arrays.asList(123, 456, 789);
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //equals(Object obj):要想返回true,判断当前集合和形参集合的元素是否相同
        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);

        //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});
        System.out.println(arr1.size());//1

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


输出:

4
6
[AA, BB, 123, Sat Feb 13 17:16:49 CST 2021, 456, CC]
true
true
true
Person equals()....
Person equals()....
Person equals()....
true
false
[123, 456, Person{name='Jerry', age=20}, Tom, false]
Person equals()....
Person equals()....
Person equals()....
[123, 456, Tom, false]
[Tom, false]
false
-1200490100
123
456
Person{name='Jerry', age=20}
Tom
false
[AA, BB, CC]
1
2
[123, 456]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YY鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值