Day 20 of learning big data - Collection, list

Posted by danaman on Fri, 11 Feb 2022 05:20:04 +0100

The 20th day of learning big data - Collection and List (because you need to accompany your family for the new year, there is no time for learning to continue recording, and it will continue to be updated every day from today)

Learning step by step according to the appeal map can get twice the result with half the effort

aggregate

The difference between set and array

Differences between sets and arrays:
1. The length of the array is immutable, and the length of the set is variable
2. Arrays can store elements of the same basic data type or reference data type
The collection can only store reference data types, and elements of different data types can be stored in the collection
(Note: Although I said here that collections can store different data types, it can also be done in practice, in actual development, a collection stores an element that references a data type)

Collection

Because Collection is an interface, it cannot be instantiated. We need to find a subclass of it to instantiate the interface in a polymorphic way. Here, we will temporarily use ArrayList
For example.
1. Add function
boolean add(Object e) ensures that this collection contains the specified element (optional operation)
boolean addAll(Collection c) adds all elements in the specified collection to this collection (optional)
2. Delete function
boolean remove(Object o) removes a single instance (if any) of the specified element from the collection (optional).
boolean removeAll(Collection c) deletes all elements of this collection contained in the specified collection (optional operation).
void clear() removes all elements from this collection (optional).
3. Get function
Iterator iterator() returns the iterator of the elements in this collection.
4. Judgment function
boolean contains(Object o) returns true if the collection contains the specified element.
boolean containsAll(Collection c) returns true if the collection contains all elements in the specified collection.
boolean isEmpty() returns true if the collection does not contain elements.
5. Get length method:
int size() returns the number of elements in this collection
6. Intersection function
Boolean retain all (collection C) retains only the elements contained in the specified collection in this collection (optional operation).
7. Convert collection to array
Object[] toArray() returns an array containing all elements in this collection.

public class CollectionDemo2 {

    public static void main(String[] args) {
        //Create collection object (interface polymorphism)
        Collection c = new ArrayList();

//        boolean add(Object e) ensures that this collection contains the specified element (optional operation)
        System.out.println(c.add("hello"));
        System.out.println(c.add("world"));
        System.out.println(c.add("hello"));
        //It can be all data types. Due to the automatic packing of packaging class,
        c.add(20);
        c.add(12.25);
        System.out.println(c);

        //void clear() removes all elements from this collection (optional).
//        c.clear();

        //boolean remove(Object o) removes a single instance (if any) of the specified element from the collection (optional).
        //Deletes the specified element
        //Remove only one eligible element
//        System.out.println("delete the specified element from the collection:" + c.remove("world"));
        System.out.println("Remove the specified element from the collection:"+c.remove("hello"));

        //boolean contains(Object o) returns true if the collection contains the specified element.
        //Judge whether the collection contains an element. If so, return true
        System.out.println(c.contains("hello"));

        //boolean isEmpty() returns true if the collection does not contain elements.
        System.out.println(c.isEmpty());

        //int size() returns the number of elements in this collection
        //Length of collection
        System.out.println("The length of the collection is:"+c.size());


        /**
         * java.lang.Object
         *      java.util.AbstractCollection<E>
         *              java.util.AbstractList<E>
         *                      java.util.ArrayList<E>
         */
        System.out.println(c); //What is called here is the toString() method in the AbstractCollection class

    }
}

boolean addAll(Collection c) adds all elements in the specified collection to this collection (optional)
boolean removeAll(Collection c) deletes all elements of this collection contained in the specified collection (optional operation).
boolean containsAll(Collection c) returns true if the collection contains all elements in the specified collection.
Boolean retain all (collection C) retains only the elements contained in the specified collection in this collection (optional operation).

//boolean addAll(Collection c) adds all elements in the specified collection to this collection (optional)
        System.out.println("take c2 Add to collection c1 Medium:");
        System.out.println(c1.addAll(c2));
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);
        System.out.println("=========================================");
        //boolean removeAll(Collection c) deletes all elements of this collection contained in the specified collection (optional operation).
        //Only change the data in the c1 set
//        System.out.println(c1.removeAll(c2));
//        System.out.println("c1:" + c1);
//        System.out.println("c2:" + c2);
        System.out.println("=========================================");
        //boolean containsAll(Collection c) returns true if the collection contains all elements in the specified collection
        System.out.println(c1.containsAll(c2));
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);
        //Boolean retain all (collection C) retains only the elements contained in the specified collection in this collection (optional operation).
        /**
         *  Suppose there are now two sets c1,c2
         *  c1 For c2 intersection, the result of the final intersection is saved in c1, and c2 remains unchanged
         *  Element c2 is not deleted in common with c2.
         */
        System.out.println(c1.retainAll(c2));
//        System.out.println(c2.retainAll(c1));
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);

Traversal of collection: the purpose is to take out the elements of the collection in turn
Object[] toArray() returns an array containing all the elements in this collection
Convert the collection into an array and then iterate over it

Object[] objects = c.toArray();

Iterator iterator() returns the iterator of the elements in this collection.
It is a proprietary way of traversing collections
boolean hasNext() determines whether there are elements in the iterator
Object next() returns the elements in the iterator

//Create a collection object
Collection c = new ArrayList();
//Add elements to the collection
c.add("hello");
c.add("world");
c.add("java");
c.add("hadoop");
c.add("hive");

//Gets the iterator object for collection c
Iterator iterator = c.iterator();
while (iterator.hasNext()) {
            Object next = iterator.next();

            //The downward transformation uses methods specific to the element data type
            String s = (String) next;
            System.out.println(s + "--Length:" + s.length());

//            System.out.println(iterator.next());
        }

1. Can I change the while loop into a for loop? Yes, but not recommended. It is recommended to use the while loop in work
The same iterator can only be traversed once. Multiple traversals have no effect, because after traversing once, the pointer points to the end.
2. Why does Java define Iterator as an interface? Instead of a class?
In the future, you need to create different sets to store according to different data. Each set has its own characteristics. It is likely that the traversal order and mode of each set are different
Therefore, when taking values in the future, the methods used are not necessarily the same, so the iterator should not directly implement how to traverse, but provide an interface
In the future, the unique collection class will implement the value taking method in this interface to realize its own value taking characteristics.

Little practice

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

/*
    Store custom objects and traverse

 */
public class CollectionTest2 {
    public static void main(String[] args) {
        //1. Create collection object
        Collection c = new ArrayList();

        //2. Create student object
        Student s1 = new Student("Fei Zhang", 17);
        Student s2 = new Student("Guan Yu", 18);
        Student s3 = new Student("Zhao Yun", 19);
        Student s4 = new Student("Huang Zhong", 20);
        Student s5 = new Student("ma chao", 21);

        //3. Add student object to collection
        c.add(s1);
        c.add(s2);
        c.add(s3);
        c.add(s4);
        c.add(s5);

        //4. Traversal set
        //Get iterator object
        Iterator iterator = c.iterator();

        //Traversing iterators to get elements
        while (iterator.hasNext()) {
            Student s = (Student) iterator.next();
            System.out.println(s.getName() + "---" + s.getAge());
        }

    }
}

List (a subclass of collection, so some methods inherit from the parent class)

List interface (inherited from Collection interface)
1. The elements in the List set are ordered (the storage and retrieval order are the same) 1, 2, 3, 4, 5 - > 1, 2, 3, 4, 5
2. The List collection contains the concept of index
3. The elements in the List set are repeatable 1, 1, 2, 3, 4, 4, 5

public class ListDemo1 {
    public static void main(String[] args) {
        //Create a List collection object and create the object in the form of interface polymorphism
        List list = new ArrayList();

        //Add elements to the collection
        list.add("hello");
        list.add("world");
        list.add("bigdata");
        list.add("java");

        //ergodic
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            String s = (String) iterator.next();
            System.out.println(s + ",The length of the string is:" + s.length());
        }
    }
}

List specific functions of related sets:
Because the List collection has the concept of subscript index, a unique method is derived from this index
1. Add features:
void add(int index, Object element) inserts the specified element into the specified position in this list (optional operation).
2. Delete function:
Object remove(int index) deletes the element at the specified position in the list (optional operation).
3. Get function:
Object get(int index) returns the element at the specified position in this list.
4. Modification function:
Object set(int index, Object element) replaces the element at the specified position in this list with the specified element (optional operation).
5. List collection specific iterators
ListIterator listIterator() returns the list iterators in the list (in the appropriate order).

public class ListDemo2 {
    public static void main(String[] args) {
        List list = new ArrayList();
        //Add element to collection
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("bigdata");
        list.add("hadoop");
        System.out.println(list);
        System.out.println("======================================");
        //void add(int index, Object element) inserts the specified element into the specified position in this list (optional operation)
        //Out of range: index < 0 | index > size () = = = > 0 < = index < = size () = = > [0, size ()]
//        list.add(0,"hive");
//        System.out.println(list);
//        list.add(5,"spark");
//        System.out.println(list);
//        list.add(7,"flink");
//        System.out.println(list);
//        list.add(10,"Hbase"); //IndexOutOfBoundsException
//        System.out.println(list);
//        System.out.println("======================================");
        //Object remove(int index) deletes the element at the specified position in the list (optional operation).
//        System.out.println(list.remove(3));
//        System.out.println(list);
        System.out.println("======================================");
        //Object get(int index) returns the element at the specified position in this list.
        System.out.println(list.get(4));
        System.out.println(list);
        System.out.println("======================================");
        //Object set(int index, Object element) replaces the element at the specified position in this list with the specified element (optional operation).
        //Returns the element replaced at the specified position
        Object obj = list.set(2, "hive");
        System.out.println(obj);
        System.out.println("list:" + list);
    }
}

List collection specific iterators
ListIterator listIterator() returns the list iterators in the list (in the appropriate order).
public interface ListIterator extends Iterator
Because ListIterator inherits from the Iterator interface, there must be hasNext() and next() methods inside

Object previous() returns the previous element in the list and moves the cursor position backward.
Returns the previous element in the list and moves the cursor position backward. This method can be called repeatedly to traverse the list backward, or mixed back and forth with the call to next().
1. This method is to get the previous element in the collection
2. The pointer of the element obtained by this method is the same as that obtained by next()
Note: if you want to traverse backwards, you must traverse positively and move the pointer to the end first. Not commonly used in development, but may be asked in an interview.
boolean hasPrevious() returns true. If the reverse list is traversed, the list iterator has multiple elements.
Judge whether the previous position is a uranium element. If there is an element, return true. If there is no element, return false.

public class ListDemo3 {
    public static void main(String[] args) {
        //1. Create a List collection object
        List list = new ArrayList();

        //2. Add element to collection
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("bigdata");

        //3. Traversal
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()) {
            String s = (String) listIterator.next();
            System.out.println(s + ",The length of the string is:" + s.length());
        }

//        Object previous = listIterator.previous();
//        System.out.println(previous);//NoSuchElementException

        System.out.println("=======================================");
        while(listIterator.hasPrevious()){
            Object previous = listIterator.previous();
            String s = (String) previous;
            System.out.println(s + ",The length of the string is:" + s.length());
        }
    }
}

The unique traversal method of List collection: the combination of size() and get() methods

List collection traversal method:
1. Call toArray() method and turn it into array traversal
2. Iterator traversal
3. size() and get() methods are used together to traverse

//Traversal (use size() in combination with get() method)
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);

            //Downward transformation
            String s = (String) o;
            System.out.println(s + "----" + s.length());
        }

Requirement: there is a set in which some string elements are stored. I want to judge whether there is a string of "bigdata"
If so, we add a "yes".
Concurrent modificationexception: concurrent modification exception
When such modification is not allowed, java detects that the object has concurrent modification exceptions.

   reason:
       The iterator depends on the set. When traversing the elements in the iterator, when we judge that it is successful, we add an element to the set, but
       At this time, the iterator does not know that the element has been added, so it reports an error.
       Simple description: during iterator traversal, you cannot modify elements through collections


   terms of settlement:
       1,Iterator traversal, iterator modification
       2,Set traversal
public class ListDemo6 {
    public static void main(String[] args) {
        List list = new ArrayList();

        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("bigdata");
        list.add("hive");

        System.out.println(list);
        // reason:
        //    The iterator depends on the set. When traversing the elements in the iterator, when we judge that it is successful, we add an element to the set, but
        //    At this time, the iterator does not know that the element has been added, so it reports an error.
        //    Simple description: during iterator traversal, you cannot modify elements through collections
//        ListIterator listIterator = list.listIterator();
//        while(listIterator.hasNext()){
//            String s = (String) listIterator.next();
//            if("bigdata".equals(s)){
//                list. add("yes");// Concurrent exception
//            }
//        }

        //terms of settlement:
        //            1. Iterator traversal, iterator modification
        //            2. Set traversal
        //list has a special iterator and ListIterator has several special uses
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()) {
            String s = (String) listIterator.next();
            if ("bigdata".equals(s)) {
                listIterator.add("yes");
            }
        }

        //Set traversal
        for(int i=0;i<list.size();i++){
            String s = (String) list.get(i);
            if("bigdata".equals(s)){
                list.add("yes"); //Add at the end of the collection
            }
        }
        System.out.println(list);


    }
}

Topics: Java Big Data list