A detailed explanation of the collection framework based on Java

Summary of Java collection classes

Difference Between Collection and Array

Collection: The root interface of a single-column collection class, used to store a series of elements that conform to certain rules.

Commonly used methods in the Collection interface

Commonly used methods in List interface

ArrayList set

LinkedList collection

set interface

HashSet set

LinkedHashSet set

Collections: Collection tool class

Map two-column collection

Commonly used methods in the Map interface

Two ways to traverse the Map collection

First, use the set method to find the value by key

Second, use the Entry object to traverse the map collection

LinkedHashMap

A collection is a type of container provided in Java that can store multiple pieces of data.

[Difference Between Collection] and Array

[The length of the array] is fixed, the length of the collection is variable

Arrays store elements of the same type and can store values ​​of primitive data types. (can be an object, can be a primitive data type)

Collections store objects, and the types of objects can be inconsistent. (can only be objects)

[Collection] : The root interface of a single-column collection class, used to store a series of elements that conform to certain rules.

It has two important sub-interfaces, namely java.util.List and java.util.Set.

The characteristics of List are that the elements are ordered and the elements can be repeated.

The characteristics of Set are that the elements are unordered and non-repeatable.

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 

Commonly used methods in the Collection interface

public  static  void  main ( String[]args ) {
     //Create a collection object, use polymorphism
    Collection<String>coll=newArrayList<>();
    System.out .println (coll); //empty // add () method booleanb1 
    =coll.add ( "Little Zhang is free!" );

    coll. add ( "Xiao Zhang!" );coll. add ( "Xiao Xiao Zhang!" );
    coll.add ( "Little Free!" );coll.add ( " Little Zhang Free!" );

    System.out.println(coll);

    //remove() method coll.remove 
    ( " Little Zhang is free!" ); //Only the first duplicate value is removed System.out 
    .println ( coll );

    //contains() method to determine whether the set contains the given object System.out 
    .println ( coll.contains ( "zyb" ));
    System.out .println ( coll.contains ( "Little Zhang is free!" ));

    //isEmpty(); Determine if the collection is empty System.out 
    .println ( coll.isEmpty ());

    //size() method returns the number of elements in the collection System.out.println ( 
    coll.size ());

    //toArray() method stores the elements in the collection into an array
    Object[]objects=coll.toArray();
    for(inti=0;i<objects.length;i++){
    System.out.println(objects[i]);
    }
    // Traverse the collection/ 
    *for(inti=0;i<coll.size();i++){
    System.out.println(coll[i]);
    }*/ 
    //Clear
    coll.clear();
    System.out.println(coll);
}

[]Common methods in [List interface]

  • public void add(int index, E element): Add the specified element to the specified position in the collection.
  • public E get(int index): Returns the element at the specified position in the collection.
  • public E remove(int index): removes the element at the specified position in the list, and returns the removed element.
  • public E set(int index, E element): Replace the element at the specified position in the set with the specified element, and return the value of the element before the update.

ArrayList set

The structure of the java.util.ArrayList collection data storage is an array structure . Elements are added and deleted slowly and search is fast . Since the most used functions in daily development are querying data and traversing data, ArrayList is the most commonly used collection.

[LinkedList] collection

The structure of the java.util.LinkedList collection data storage is a linked list structure. A collection that facilitates adding and removing elements.

Methods specific to the LinkedList collection

  • public void addFirst(E e): Inserts the specified element at the beginning of this list.
  • public void addLast(E e): Adds the specified element to the end of this list.
  • public E getFirst(): Returns the first element of this list.
  • public E getLast(): Returns the last element of this list.
  • public E removeFirst(): Removes and returns the first element of this list.
  • public E removeLast(): Removes and returns the last element of this list.
  • public E pop(): Pops an element from the stack represented by this list.
  • public void push(E e): Pushes the element onto the stack represented by this list.
  • public boolean isEmpty(): Returns true if the list contains no elements.

private static void show01(){
    LinkedList<String>LinkedList=newLinkedList<String>();
    LinkedList.add("a");LinkedList.add("b");
    LinkedList.add("c");LinkedList.add("d");

    System.out.println ( "Original array" +LinkedList) ;
    System.out .println (LinkedList.removeFirst()); //Remove the first one has the same effect as pop() System.out .println (LinkedList.removeLast()) 
    ;

    LinkedList.addFirst( "Java" ); //The beginning is the same as push() 
    LinkedList.addLast( "promsing" ); //The end is the same as add() System.out 
    .println (LinkedList);
    System.out.println ( LinkedList.getFirst ()); //Get the first 
    System.out.println ( LinkedList.getLast ()); //Get the last

}

set interface

The java.util.Set interface is the same as the java.util.List interface, and also inherits from the Collection interface. It is basically the same as the method in the Collection interface. There is no functional expansion of the Collection interface, but it is more strict than the Collection interface.

HashSet set

java.util.HashSet is an implementation class of the Set interface. The elements it stores are not repeatable, and the elements are unordered (that is, the access order is inconsistent). The underlying implementation of java.util.HashSet is actually a java.util.HashMap support,  

public  class  HashSetDemo { 
     public  static  void  main ( String[] args ) { 
         //Create Set collection 
        HashSet<String> set = new HashSet<String>(); 
         // Add element 
        set .add ( new String( "cba" )) ; 
         set . add ( "abc" ); 
         set . add ( "bac" ); 
         set . add ( "cba" ); 
        //traverse 
        for (String name : set ) {
            System.out.print(name); 
        } 
    } 
}
//The output result is cba abc bac

[LinkedHashSet] set

HashSet guarantees that the elements are unique, but the elements are stored in no order, so we need to use the LinkedHashSet collection to ensure order . It is a data storage structure combining a linked list and a hash table.

public class LinkedHashSetDemo { 
    public static void main(String[] args) { 
        Set<String> set = new LinkedHashSet<String>(); 
        set.add("bbb"); 
        set.add("aaa"); 
        set.add("abc"); 
        set.add("bbc"); 
        Iterator<String> it = set.iterator(); 
        while (it.hasNext()) { 
            System.out.print(it.next()); 
        } 
    } 
}
//The output result is bbb aaa abc bbc

Collections: Collection tool class

  • public static boolean addAll(Collection c, T… elements) : Add some elements to the collection.
  • public static void shuffle(List<?> list) Shuffle: Shuffle the order of the collection.
  • public static void sort(List list) : Sort the elements in the collection according to the default rules.
  • public static void sort(List list, Comparator<? super T> ) : Sort the elements in the collection according to the specified rules.

public static void main (String[] args){
    ArrayList<String> list = new ArrayList<>();
    Collections.addAll( list , "a" , "b" , "c" , "d" , "e" ); //Add multiple e in batches 
    System.out.println( list );
    Collections.shuffle( list ); //Shuffle 
    System.out.println( list );
}

Two-column collection Map

Commonly used methods in the Map interface

public  static  void  main (String[] args)  { 
     //Create a map object 
    HashMap<String, String> map = new HashMap<String, String>(); 
     //Add elements to the set 
    map .put( "Huang Xiaoming" , "Yang Ying" ); 
     map .put( "article" , "Ma Yili" ); 
     map .put( "Deng Chao" , "Sun Li" );
    System.out.println(map); 
    //String remove(String key) 
    System.out.println( map ); 
     // Want to find out who Huang Xiaoming's daughter-in-law is 
    System.out.println( map .get( "Huang Xiaoming" ));
    System.out.println( map .get( "Deng Chao" ));
}

Two ways to [traverse] the Map collection

First, use the set method to find the value by key

public static void main(String[] args) {

        Map<String,Integer> map = new HashMap<>();
         map .put( "Guo Jing" , 175 );
         map .put( "Yang Guo" , 180 );
         map .put( "Qiao Feng" , 183 );
         map .put( "Wuji" , 177 );
         //1. Use the method keySet() in the Map collection to take out all the keys in the Map collection and store them in the set collection 
        Set<String> set = map .keySet ();
         //2. Traverse the set collection and get each key in the Map collection 
        //Use the iterator to traverse the set collection 
        Iterator<String>iterator=set.iterator();
        while(iterator.hasNext()){
            String key=iterator.next();
            //3. Through the method get() in the Map collection, find the value through Key 
            Integer integer= map .get(key); //According to the specified key, find the corresponding value 
            System.out.println(key+ "," + integer);

        }
        System.out.println( "Enhanced For loop========" );
         for (String key: set ){
            Integer integer= map .get(key); //According to the specified key, find the corresponding value 
            System.out.println(key+ "," +integer);
        }
    }

Second, use the Entry object to traverse the map collection

public static void main(String[] args) {

        Map<String,Integer> map=new HashMap<>();
        map.put( "Guo Jing" , 175 );
        map.put( "Yang Guo" , 180 );
        map.put( "Qiao Feng" , 183 );
        map.put( "Wuji" , 177 );
         //1. Use the entrySet() method in the Map collection to take out multiple entry objects in the Map collection and store them in the Set collection 
        Set < Map .Entry < String , Integer>> set =map.entrySet();
        //Second, traverse the Set collection to get each entry object
        Iterator<Map.Entry<String,Integer>>iterator=set.iterator();
        while(iterator.hasNext()){
            Map.Entry<String,Integer>entry=iterator.next();
            String key=entry.getKey();
            Integer value=entry.getValue();
            System.out.println(key+","+value);
        }
        System.out.println( "Enhanced For loop ==============" );
         for ( Map .Entry< String ,Integer>entry: set ){
        }

    }

LinkedHashMap: Stored in order – it is a data storage structure combining a linked list and a hash table.

public class LinkedHashMapDemo { 
        public static void main(String[] args) { 
            LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); 
            map.put( "Deng Chao" , "Sun Li" );
            map.put( "Li Chen" , "Fan Bingbing" );
            map.put( "Andy Lau" , "Zhu Liqian" ); 
             Set <Entry< String , String >> entrySet = map.entrySet(); 
             for (Entry< String , String > entry : entrySet) {
                System.out.println(entry.getKey() + " " + entry.getValue()); 
            }
        } 
    }

It is not easy to create. If this blog is helpful to you, please remember to leave a message + like it.

Leave a Comment

Your email address will not be published. Required fields are marked *