Collection 01

Posted by nwoeddie23 on Tue, 28 Dec 2021 22:03:08 +0100

Set (set frame)

Collection is an interface. It is a data structure used to store objects. Its length is variable, and different types of objects can be stored in the collection. It also provides a series of methods to operate this object.
It is divided into external interface, interface implementation, and set framework algorithm.

Like arrays, sets are used to store data (multiple data);

1. Overview of data stored in sets and arrays:
Collection and array are structures that store multiple data, which are referred to as Java containers for short.
Note: storage at this time mainly refers to memory level storage, and does not involve persistent storage (. txt,.jpg,.avi, in the database)

2. Characteristics of array storage:

Once initialized, its length is determined.
Once an array is defined, the type of its elements is determined. We can only operate on the specified type of data.

  •   For example: String[] arr;int[] arr1;Object[] arr2;
    

3. Disadvantages of array storage:

  •  > Once initialized, its length cannot be modified.
    
  •  > The methods provided in the array are very limited. It is very inconvenient and inefficient to add, delete and insert data.
    
  •  > The requirement to obtain the number of actual elements in the array. There are no ready-made properties or methods available in the array
    
  •  > The characteristics of array storage data: orderly and repeatable. For disordered and unrepeatable requirements, they cannot be met.
    
  •  >The array is traversed according to the subscript, which is suitable for query
    

4. Advantages of collective storage:

  • Solve the disadvantages of array data storage.

  • Variable length, rich traversal methods

  • It is suitable for adding and deleting queries.

Inheritance structure of collection

The common things are extracted upward to form the inheritance architecture of the container. (Abstract)

1, Sub interface of Collection

1.1 List interface: the stored data is orderly and can be repeated

Implementation class of List interface:

ArrayList class
LinkList class:
vector class:

1.2 Set interface: the stored data is out of order, and duplicate values cannot be stored

Implementation class of Set interface

hasSet subclass

Collections tool class

1.3 common methods

  • Boolean add (E) add element

  • Boolean addall (collection <? Extensions E > C) adds a small set to a large set

  • boolean contains(Object o) returns true if the collection contains the specified element.

  • boolean isEmpty() returns true if the collection does not contain elements

  • boolean remove(Object o) removes a single instance of the specified element, if any, from this collection.

  • int size() returns the number of elements in this collection (the length of the collection)

  • Object[] toArray() returns an array containing all the elements in this collection, (the collection is converted into an array)

Iterator iterator

  • Iterator iterator() returns the iterator (traversing elements) that iterates over the elements of this collection

Code implementation:
Generics are often used with collection objects.
In essence, it is a small means provided by the compiler to provide better readability.
The types of collection elements are constrained through the syntax definition of generics, security checks are carried out, and errors are displayed at compile time
Collection col = new ArrayList();

package day05Collection;

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

/**
 * @author yyc
 */
public class CollectionTest1 {
    public static void main(String[] args) {
        //<E> Generics are often used with collection objects.
        // In essence, it is a small means provided by the compiler to provide better readability.
        // The types of collection elements are constrained through the syntax definition of generics, security checks are carried out, and errors are displayed at compile time
        //1. Create a collection object. The type in the generic constraint is a reference type, not a basic type
        Collection<Integer> col = new ArrayList<>();//< > generics were used earlier. Keep consistent
        //col.add("aaa"); report errors
        col.add(23);
        //2. Common methods
        //① Common add() for adding elements to a collection
        col.add(45);
        col.add(1);
        System.out.println(col);
        //col.clear(); Empty collection
        System.out.println(col);
        System.out.println(col.contains(5));//Judge whether this collection contains the specified number, true
        System.out.println(col.equals(23));//Determine whether the set is equal to 23
        System.out.println(col.hashCode());//Get the hash value of col, 53290
        System.out.println(col.remove(23));//Remove 23 from set col
        System.out.println(col);
        System.out.println(col.isEmpty());//Judge whether set 4 is empty
        System.out.println(col.size());//Gets the length of the collection
        Object[] os = col.toArray();//Set to array
        System.out.println(Arrays.toString(os));//[45, 1]

        Collection<Integer> col2 = new ArrayList<>();
        col2.add(67);
        col2.add(68);
        col2.add(69);
        col.addAll(col2);//Add set col2 to set col2
        System.out.println(col);
        System.out.println(col.size());
        System.out.println(col.containsAll(col2));//Determine whether col2 is included in col
        System.out.println(col2.retainAll(col));//
        //Gets the element in the collection
        //Method 1: through Iterator iterator < E > Iterator (), --- return Iterator, which is specially used to iterate the elements in the collection
        Iterator<Integer> it = col.iterator();//Get the iterator reference through the iterator method of the collection. The iterator is not a container
       //Traversal set
        while (it.hasNext()){//hadNext() judges that there are elements, and returns true if there are
            Integer in = it.next();//next() gets the element.
            System.out.print(in+" ");
        }
        System.out.println();
        //Method 2: for (data type variable name: container (set)) {}
        for (Integer in : col){
            System.out.println(in);
        }

    }
}

collections tool class

Overview: provides methods specifically for Collection collections Collections is the implementation class of a Collection

  • Static Boolean addall (collection <? Super T > C, t... elements) adds all specified elements to the specified collection.

  • Static t max (collection <? Extensions T > Col) returns the largest element of a given set according to the natural order of its elements

  • Static t min (collection <? Extensions T > coll, comparator <? Super T > COMP) returns the smallest element of a given set according to the natural order of its elements.

  • static void sort(List list, Comparator<? super T> c)

  • static void reverse(List<?> list)

  • static void swap(List<?> list, int i, int j)

package day05Collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yyc
 */
/*
* Test Collection's tool class Collections
* */
public class Collections_Test {
    public static void main(String[] args){
        //1. Create a List collection and add elements
        List<Integer> list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);
        //Add elements to the collection at once.
        Collections.addAll(list,15,23,54,65,34);
        System.out.println(list);
        System.out.println(Collections.max(list));//Gets the maximum value.
        System.out.println(Collections.min(list));//Get minimum value

        //Reverse (list) reverse
        Collections.reverse(list);
        System.out.println(list);

        //Sort collections naturally
        Collections.sort(list);
        System.out.println(list);//Sort the list set memory naturally
        
        Collections.swap(list,0,2);
        //Swap positions for the elements of subscript 0 and subscript 2 of the list set
        System.out.println(list);
    }
}

2.2List interface

Overview: it is an ordered Collection and a sub interface of the Collection interface. All methods of the Collection can be used. In addition, many extension methods are made. Users of this interface can accurately control the insertion position of each element in the list, Users can access an element based on its integer index (position in the list) and search for elements in the list.

characteristic:

  • Element order

  • Allow duplicate elements to be stored

  • Element has index

  • Can store null

Topics: JavaSE