Detailed Collection-Collection Interface

Collection interface

The Collection interface is the parent interface of the List, Set and Queue interfaces. The methods defined in this interface can be used to manipulate the Set collection as well as the List and Queue collections.
JDK does not provide any direct implementation of this interface, but provides more specific sub-interfaces (such as Set and List) implementations.
Before Java5, Java collections would lose the data types of all objects in the container, and treat all objects as Object types; after the addition of generics from JDK 5.0, Java collections can remember the data types of objects in the container.

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-aL7PjPTe-1614868115130)(assets/image-20210212111441388.png)]

Collection interface method

1. Add

​ boolean add(Object obj)

​ boolean addAll(Colllection c)

2. Get the number of valid elements

​ int size()

3. Empty the collection

​ void clear()

4. Is it an empty collection

​ boolean isEmpty()

5. Whether it contains an element

​ boolean contains(Object obj) is to judge whether it is the same object through the equals() method of the element

​ boolean containsAll(Collection c) is to call the equals() method of the element to compare, and compare the elements of the two collections one by one

6. Delete

​ boolean remove(Object obj) Determine whether it is the element to be deleted through the equals() method of the element, only the first element found will be deleted

​ boolean removeAll(Collection c) Take the difference of the current collection

7. Take the intersection of two sets

​ boolean retainAll(Collection c) Store the result of the intersection in the current collection without affecting the collection c

8. Is the set equal

​ boolean equals(Object obj)

9, into an array of objects

​ Object[] toArray()

10. Get the hash value of the collection object

​ int hashCode()

11. Traverse

​ Iterator iterator() returns an iterator object, used to traverse the collection

Case:

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);
        System.out.println(coll.contains(new String("Tom")));
//        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):从当前集合中移除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);

        //5.retainAll(Collection coll1):交集:获取当前集合和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(456);
        coll1.add(123);
        coll1.add(new Person("Jerry",20));
        coll1.add(new String("Tom"));
        coll1.add(false);

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


    }

    @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.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

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

    }
}

Guess you like

Origin blog.csdn.net/qq_44346427/article/details/110729377