Dry goods | An article to understand the overview and common methods of Java Collection collection


Foreword:

  • Collection : A collection is a container provided in java that can be used to store multiple data.

    Since collections and arrays are both containers, what is the difference between them?

  • The length of the array is fixed. The length of the collection is variable.

  • Arrays store elements of the same type, and can store basic data type values. All collections store objects. And the types of objects can be inconsistent. In development, when there are many objects, collections are used for storage.

One, the collection framework

Collection : The root interface of a single-column collection class is used to store a series of elements that conform to a certain rule. It has two important sub-interfaces, java.util.List and java.util.Set. Among them, List is characterized by ordered elements and repeatable elements. The characteristic of Set is that the elements are disordered and cannot be repeated.
The main implementation classes of the List interface are java.util.ArrayList and java.util.LinkedList.
The main implementation classes of the Set interface are java.util.HashSet and java.util.TreeSet.
Insert picture description here
So we first learn the common methods of the root interface of the Collecttion single-column collection class, and it is easier to learn the other collections below.

Two, Collection collection commonly used methods

1. public boolean add(E e): add the given object to the current collection

Method 1:
public boolean add(E e): Add the given object to the current collection.
The return value is a boolean value, which generally returns true, so you don’t need to receive the

code as follows (example):

 //创建集合对象可以使用多态
        Collection<String> coll =  new ArrayList<>();
        System.out.println(coll);//[]   new 是一个对象 它打印的不是地址说明同String方法重写

         /*
            方法一:
            public boolean add(E e):  把给定的对象添加到当前集合中 。
            返回值是一个boolean值,一般都返回true,所以可以不用接收
         */
        boolean b1 = coll.add("张三");
        System.out.println("b1"+b1);//b1:true
        System.out.println(coll);//[张三]
        coll.add("小高");
        coll.add("小张");
        coll.add("小伍");
        coll.add("小陈");
        coll.add("小罗");
        System.out.println(coll);//[张三, 小高, 小张, 小伍, 小陈,小罗]
        System.out.println("************分界线************");

2.public boolean remove(E e): delete the given object in the current collection

Method 2:
public boolean remove(E e): delete the given object in the current collection.
The return value is a boolean value. If there is an element in the collection, delete an element, return true
if there is no element in the collection, delete failure, return false

code is as follows (example):

        boolean b2 = coll.remove("小高");
        System.out.println("b2:"+b2);//b2:true

        boolean b3 = coll.remove("小明");
        System.out.println("b3:"+b3);//b3:false
        System.out.println(coll);//[张三, 小张, 小伍, 小陈, 小罗]

3.public boolean contains(E e): Determine whether the current collection contains the given object

Method 3:
public boolean contains(E e): Determine whether the current collection contains the given object.
Contains return true and
does not contain return false

code as follows (example):

        boolean b4 = coll.contains("小张");
        System.out.println("b4:"+b4);//b4:true

        boolean b5 = coll.contains("啊道");
        System.out.println("b5:"+b5);//b5:false
       

4.public boolean isEmpty(): Determine whether the current collection is empty

Method 4:
public boolean isEmpty(): Determine whether the current collection is empty.
If the collection is empty, return true, if the collection is not empty, the return false

code is as follows (example):

        boolean b6 = coll.isEmpty();
        System.out.println("b6:"+b6);//b6:false

5.public int size(): returns the number of elements in the collection

Method 5:
public int size(): Returns the number of elements in the collection.

The code is as follows (example):

        int b7 = coll.size();
        System.out.println("b7:"+b7);//b7:5

6.public Object[] toArray(): Store the elements in the collection in an array

Method 6:
public Object[] toArray(): Store the elements in the collection in an array.

The code is as follows (example):

       Object[] arr = coll.toArray();//object接收所以类型
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.println(arr[i]);//张三小张小伍小陈小罗
        }

7.public void clear(): Clear all elements in the collection

Method seven:
public void clear(): Clear all elements in the collection.

The code is as follows (example):

        coll.clear();
        System.out.println(coll);//[]
        System.out.println(coll.isEmpty());//true   isEmpty(): 判断当前集合是否为空。

Three, summary

java.util.Collection interface
The top-level interface of all single-column collections, which defines the methods common to all single-column collections.
Any single-column collection can use the methods in the Collection interface.

Common method:
public boolean add(E e) : Add the given object to the current collection.
public void clear() : Clear all elements in the collection.
public boolean remove(E e) : Remove the given object in the current collection.
public boolean contains(E e) : Determine whether the current collection contains the given object.
public boolean isEmpty() : Determine whether the current collection is empty.
public int size() : Returns the number of elements in the collection.
public Object[] toArray() : Store the elements in the collection in an array.

Guess you like

Origin blog.csdn.net/weixin_46235428/article/details/109292479