Common method demonstration in Java collection Collection interface

Since the interface cannot create objects, the demonstration of the following methods will be demonstrated with the implementation class ArrayList() of the subinterface List of the Collection interface.

Add add(Objec tobj) and addAll(Collection coll)

(1) add(Objec tobj) adds an object, if it is a basic data type, it is automatically boxed.

		ArrayList list1 = new ArrayList();
        Date date = new Date();
        
        list1.add(1);//基本数据类型自动装箱
        list1.add(date);//引用数据类型

        System.out.println(list1);

output result
insert image description here

(2) addAll(Collection coll) adds a collection.

		ArrayList list1 = new ArrayList();
        list1.add("张三");
        list1.add(123);

        ArrayList list2 = new ArrayList();
        list2.add("李四");
        list2.add(55);
        
        list1.addAll(list2);//将集合list2加到集合list1中
        System.out.println(list1);

output result
insert image description here

Get the number of valid elements int size()

int size()

		ArrayList list1 = new ArrayList();
        Date date = new Date();

        list1.add(1);
        list1.add(date);

        System.out.println(list1.size());

output result
insert image description here

Clear the collection void clear()

void clear()

        ArrayList list1 = new ArrayList();
        Date date = new Date();

        list1.add(1);
        list1.add(date);

        list1.clear();//清空集合

        System.out.println(list1.size());//输出元素个数

output result
insert image description here

Is it an empty collection boolean isEmpty()

boolean isEmpty()

        ArrayList list1 = new ArrayList();
        Date date = new Date();

        System.out.println(list1.isEmpty());//未添加元素,判断是否是空集合

        list1.add(1);
        list1.add(date);

        System.out.println(list1.isEmpty());//添加完元素,判断是否是空集合

output result
insert image description here

Whether to contain an element boolean contains(Object obj) and boolean containsAll(Collection c)

(1) boolean contains(Object obj): It is determined by the equals method of the element whether it is the same object.

		ArrayList list1 = new ArrayList();
        Date date = new Date();
        
        list1.add(1);
        list1.add(date);

        System.out.println(list1.contains(1));

output result
insert image description here

(2) boolean containsAll(Collection c): It is also compared by calling the equals method of the element. Take the elements of two sets and compare them one by one.

        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();

        //list1赋值
        list1.add(1);
        list1.add("张三");

        //list2赋值
        list2.add(1);
        list2.add("张三");

        System.out.println(list1.containsAll(list2));

Note on output results
insert image description here
: If it is a custom object, you need to override the equals method of Object in the object, otherwise the comparison is the address. After overriding, the value of the property is compared.

remove boolean remove(Object obj) and boolean removeAll(Collection coll)

(1) 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 removed.

        ArrayList list1 = new ArrayList();

        list1.add("张三");
        list1.add("李四");
        list1.add("张三");
        list1.add("张三");
        list1.add("张三");

        list1.remove("张三");//在集合list1中删除元素"张三"

        System.out.println(list1);

output result
insert image description here

(2) boolean removeAll(Collection coll): Take the difference of the current collection. (ie AB=A-AB)

        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();

        list1.add("张三");
        list1.add("李四");

        list2.add("王五");
        list2.add("李四");


        list1.removeAll(list2);

        System.out.println(list1);

output result
insert image description here

Take the intersection of two collections boolean retainAll(Collection c)

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

        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();

        list1.add("张三");
        list1.add("李四");

        list2.add("王五");
        list2.add("李四");
        
        list1.retainAll(list2);

        System.out.println(list1);

output result
insert image description here

Is the collection equal boolean equals(Object obj)

boolean equals(Object obj)

        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();

        list1.add("张三");
        list1.add("李四");

        list2.add("张三");
        list2.add("李四");
        
        System.out.println(list1.equals(list2));

Note on output results
insert image description here
: The principle is to call the equals method of the collection element for comparison. If the collection element is a custom object, the equals method must be rewritten, otherwise the address value is compared.

Convert to object array Object[] toArray()

Object[] toArray()

		ArrayList list1 = new ArrayList();

        list1.add("张三");
        list1.add("李四");

        Object[] objects = list1.toArray();

Get the hash value of the collection object

hashCode()

        ArrayList list1 = new ArrayList();

        list1.add("张三");
        list1.add("李四");

        System.out.println(list1.hashCode());

output result
insert image description here

traverse iterator()

iterator()

        ArrayList list1 = new ArrayList();

        list1.add("张三");
        list1.add("李四");

        Iterator iterator = list1.iterator();

        while (iterator.hasNext()){
    
    
            String next = (String) iterator.next();

            System.out.println(next);
        }

output result
insert image description here

Guess you like

Origin blog.csdn.net/baiqi123456/article/details/123762804