[17] - container (Collection) collection list interface ArrayList LinkedList vector

Why containers?

Because the array does not meet the needs

Reason

Once the length of an array is created, it cannot be changed, so the array has limitations and cannot be widely used

Main points:

A collection can only store objects (that is, reference data types)

Interface framework

The Java collection framework provides a set of high-performance and easy-to-use interfaces and classes, which are located in the java.util package

Data stored in a collection is called an element


Why do collections have so many interfaces

Because the underlying data interfaces are different, different interfaces are needed to implement them

(underlying data structure: ordered, unordered)

Interface characteristics

Collection interface

Store a set of non unique, unordered objects

List interface:

Stores a set of non unique, ordered (index ordered) objects

Set interface:

Store a set of unique, unordered objects

List interface > ArrayList > usage (API)

List interface is ordered and not unique (java.util)

ArrayList the underlying data structure is an array

Advantage: fast array traversal
Disadvantages: low efficiency of deleting and adding elements

0.5 times of each expansion

(1) Add element

list1.add("Hello");

(2) View the number of elements in the collection size()

list1.size()

(3) See if the collection is empty

list1.isEmpty()

(4) Add the specified element object to the current collection addAll(Collection c)

List list1 = new ArrayList<>();
//(1) Add element add(Object obj)
       list1.add("Hello");
       list1.add(123);//Auto boxing Integer type
  List list2 = new ArrayList<>();
        list2.add("hello");
        list2.add(456);
        list1.addAll(list2);
        System.out.println("Recheck list1 Number of elements in:"+list1.size());
        System.out.println("See list Elements in collection:"+list1);

(5) Delete elements (object & index first from left to right)

list1.remove("hello")

System.out.println("Delete elements from objects:"+list1.remove("hello"));
//Delete 123
 System.out.println("Delete by index:"+list1.remove(0));
//list1.remove(123) will report an error because it is out of bounds because the compiler will think it is deleting an index
list1.remove(new Integer(123));//This is the right way
 //Delete the contents of list1[hello,123,world] list2[hello,123] in all b sets of a set
 list1.remove(list2);//Only list1[world] is left after execution
//Delete different elements from two sets
  list1.retainAll(list2);

(6) Determine whether the specified element exists in the collection (return the result of boolean type true/false

  System.out.println("hello Does it exist in the collection:"+list1.contains("hello"));
//Judge whether set 1 contains all elements of set 2 (return boolean type result true/false
    System.out.println("Determine whether set 1 contains all elements of set 2"+list1.containsAll(list2));

(7) Empty all element objects in the collection

list1.clear();

(8) Gets the element object at the specified index location

 list1.get(1);

(9) Sets the element object on the specified index location to a new object

  list1.set(1,"java");

(10) Add at specified location

list1.add(1,"html");

(11) The location of the lookup element in the collection (first element object left to right) is not - 1

list1.indexOf("java");

(12) Traverse everything in the collection

12.1 use the enhanced for loop to traverse elements in a collection

 System.out.println("Use enhancement for Loop through the elements in a set");
   for (Object obj : list1) {
   System.out.println(obj);
    }

12.2 using normal for loop traversal

 System.out.println("Use ordinary for Cyclic traversal");
      for (int i = 0; i < list1.size(); i++) {
        System.out.println(list1.get(i));//Get get the object according to the index
     }

12.3.1 traversal using iterators (forward traversal)

 Iterator ite = list1.iterator(); //forward traversal
    while (ite.hasNext()){//Determine whether there are element objects in the collection
        Object obj = ite.next();//Automatic up conversion
        System.out.println(obj);
  }

12.3.2 traversal using iterators (reverse traversal)

System.out.println("Use listIterator()Backward Traversal");
ListIterator listIterator = list1.listIterator();
 System.out.println("forward traversal");//Principle: first look at the elements in the collection, and then reverse traverse
 System.out.println("At the beginning of a collection,Is there any element behind it"+listIterator.hasNext());
 System.out.println("At the beginning of a collection,Is there any element in front"+listIterator.hasPrevious());
    while (listIterator.hasNext()){
           System.out.println(listIterator.next());
     }
  System.out.println("End of set reached,Is there any element behind it"+listIterator.hasNext());
  System.out.println("End of set reached,Is there any element in front"+listIterator.hasPrevious());
  System.out.println("Traversing the elements of a collection in reverse");
     while (listIterator.hasPrevious()){
         System.out.println(listIterator.previous());
    }
package com.lin.list;

import java.util.*;

public class TestArrayList {
    public static void main(String[] args) {
        //Create collection object interface new implementation class
        List list1 = new ArrayList<>();

        //(1) Add element add(Object obj)
        list1.add("Hello");
        list1.add(123);//Auto boxing Integer type
        list1.add(new Scanner(System.in));

        //(2) View the number of elements in the collection size()
        System.out.println(list1.size());

        //(3) See if the collection is empty
        System.out.println("See if the collection is empty:"+list1.isEmpty());

        //(4) Add the specified element object to the current collection addAll(Collection c)
        List list2 = new ArrayList();
        list2.add("hello");
        list2.add(456);
        list1.addAll(list2);
        System.out.println("Recheck list1 Number of elements in:"+list1.size());
        System.out.println("See list Elements in collection:"+list1);

        //(5) Delete elements (object & index first from left to right)
        System.out.println("Delete elements from objects:"+list1.remove("hello"));
        //Delete 123
        System.out.println("Delete by index:"+list1.remove(0));
        //list1.remove(123) will report an error because it is out of bounds because the compiler will think it is deleting an index
        list1.remove(new Integer(123));//This is the right way
        //Delete the contents of list1[hello,123,world] list2[hello,123] in all b sets of a set
        list1.remove(list2);//Only list1[world] is left after execution
        //Delete different elements from two sets
        list1.retainAll(list2);

        //(6) Determine whether the specified element exists in the collection (return the result of boolean type true/false
        System.out.println("hello Does it exist in the collection:"+list1.contains("hello"));
        //Judge whether set 1 contains all elements of set 2 (return the result of boolean type true/false
        System.out.println("Determine whether set 1 contains all elements of set 2"+list1.containsAll(list2));

        //(7) Empty all element objects in the collection
        list1.clear();

        //(8) Gets the element object at the specified index location
        System.out.println("Get object with index position 1"+list1.get(1));

        //(9) Sets the element object on the specified index location to a new object
        list1.set(1,"java");

        //(10) Add at specified location
        list1.add(1,"html");

        //(11) The location of the lookup element in the collection (first element object left to right) is not - 1
        list1.indexOf("java");

        //(12) Traverse everything in the collection

        //12.1 use the enhanced for loop to traverse elements in a collection
        System.out.println("Use enhancement for Loop through the elements in a set");
        for (Object obj : list1) {
            System.out.println(obj);
        }
        //12.2 using normal for loop traversal
        System.out.println("Use ordinary for Cyclic traversal");
        for (int i = 0; i < list1.size(); i++) {
            System.out.println(list1.get(i));//Get get the object according to the index
        }

        //12.3 traversal using iterators
        Iterator ite = list1.iterator(); //forward traversal
        while (ite.hasNext()){//Determine whether there are element objects in the collection
            Object obj = ite.next();//Automatic up conversion
            System.out.println(obj);
        }
        System.out.println("Use listIterator()Backward Traversal");
        ListIterator listIterator = list1.listIterator();
        System.out.println("forward traversal");//Principle: first look at the elements in the collection, then reverse traverse
        System.out.println("At the beginning of a collection,Is there any element behind it"+listIterator.hasNext());
        System.out.println("At the beginning of a collection,Is there any element in front"+listIterator.hasPrevious());
        while (listIterator.hasNext()){
            System.out.println(listIterator.next());
        }
        System.out.println("End of set reached,Is there any element behind it"+listIterator.hasNext());
        System.out.println("End of set reached,Is there any element in front"+listIterator.hasPrevious());
        System.out.println("Traversing elements in a collection in reverse");
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previous());
        }

    }
}

List interface LinkList usage (API)

List interface is ordered and not unique (java.util)
The underlying data structure of LinkedList is linked list
Advantages: high efficiency of deleting and adding elements
Disadvantages: complex data structure is divided into one-way linked list and two-way linked list

LinkedList - the API is the same as ArrayList, so there are more methods

LinkedList list = new LinkedList();
        //Add to
        list.add("hello");//Additive elements
        list.addFirst("java");//Add elements to the collection element header
        list.addLast("world");//Add an element at the end of a collection element

        //delete
       /* list.removeFirst();//Delete header element
        list.removeLast();//Delete tail element*/

        //Traversal is consistent with ArrayList
        for (Object o : list) {
            System.out.println(o);
        }

List interface Vector usage (API)

List interface is ordered and not unique (java.util)
API usage is the same as ArrayList
The underlying data structure is the same as ArrayList, which is an array of Object type
Difference between Vector and ArrayList:

(1)

Vector's add() method is a synchronous method

The add() method of ArrayList is asynchronous

(2)

Vector expansion is doubled each time,

ArrayList expands 0.5x each time

(3)

When Vector calls the construction method, its direct initialization capacity is 10,

ArrayList is initialized with a capacity of 10 when the add method is called for the first time

(4)

Vector's version is JDK 1.0,

ArrayList, JDK version 1.2

(5)

Vector is thread synchronous, with high security and low efficiency,

ArrayList is thread asynchronous, low security and high efficiency

public static void main(String[] args) {
    //Set object created
    Vector vector=new Vector();
    System.out.println(vector.capacity()+"\t Number of elements in the collection:"+vector.size());
    //ArrayList al=new ArrayList();
    //al.add("hello");
    //Add to
    vector.add("hello");
    vector.add(0, "world");
    vector.addElement("java"); //Additive elements
    
    System.out.println(vector.capacity()+"\t Number of elements in the collection:"+vector.size());
    System.out.println("Number of elements in the collection:"+vector.size());
    System.out.println(vector);
    //delete
    //vector.remove(1);
    //vector.remove("java");
    //vector.clear();
    //vector.removeElement("java");
    //How to get elements
    System.out.println(vector.get(1));
    
    System.out.println(vector);
    System.out.println("Strengthen for loop");
    for(Object obj:vector){
      System.out.println(obj);
    }
    System.out.println("Traversing a collection with iterators:");
    for(Iterator ite=vector.iterator();ite.hasNext();){
      System.out.println(ite.next());
    }
    
    Iterator ite=vector.iterator(); 
    while(ite.hasNext()){
      System.out.println(ite.next());
    }
  }
}

Published 26 original articles, won praise 0, visited 672
Private letter follow

Tags: Java JDK

Posted on Sun, 15 Mar 2020 06:44:08 -0400 by hellouthere