Collection中常用api

2021/9/17 6:06:43

本文主要是介绍Collection中常用api,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!


Collection中常用api


package com.atguigu.java;

/**
 * @author liangqichen
 * @create 2021-09-15 23:01
 */
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;

        if (age != person.age) return false;
        return name != null ? name.equals(person.name) : person.name == null;
    }

//    @Override
//    public int hashCode() {
//        int result = name != null ? name.hashCode() : 0;
//        result = 31 * result + age;
//        return result;
//    }
}

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Collection 接口中声明的方法的测试
 * <p>
 * 结论
 * 向 Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals().
 *
 * @author liangqichen
 * @create 2021-09-15 22:58
 */
public class CollectionTest {
    @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)));  // true

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

    @Test
    public void test2() {
        // 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(123);
        System.out.println(coll);  // [456, Person{name='Jerry', age=20}, Tom, false]
        coll.remove(new Person("Jerry", 20));
        System.out.println(coll);  // [456, Tom, false]

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

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

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

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

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

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

        // 8. 集合 ---> 数组 :  toArray() 集合到数组的转换
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + "\t");  // 123	456	Person{name='Jerry', age=20}	Tom	false
        }

        // 拓展 数组--->集合 : 调用Arrays类的静态方法 asList()
        List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
        System.out.println(list);  // [AA, BB, CC]

        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

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



这篇关于Collection中常用api的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程