Collection framework of java

introduce

Collection interface and its derivatives

What is the collection?

Simply put, it is a encapsulated data structure

Looking at a picture, we need to clarify the relationship inside:

Classification of each color: (divided into three types)

The relationship between the three should be clearly sorted out according to the direction of the arrow, as shown in the figure below:

Collection means collection. It is used to store "single" elements, that is, single "elements" are stored one by one,
Three interfaces extend the Collection interface, namely the List interface, the Queue interface, and the Set interface
The implementation classes that implement the List interface are generally ArrayList. The implementation classes that implement the Queue interface are LinkedList and PriorityQueue
The implementation classes that implement the Set interface include TreeSet and HashSet
In the future, we need to focus on the List interface, Queue interface and Set interface
List interface: linear structure
Queue interface: queue
Set interface: a set in the mathematical sense. The elements cannot be repeated (the repeated elements are counted as one), which implies the semantics of search / search
Deque interface: double ended queue
SortedSet interface: an ordered set of non repeatable elements

Summary: two points need to be clarified:

1: Interface to interface, class to interface relationship
2: What interfaces does each specific implementation class implement

The interface implementation classes we need to focus on are: stack, ArrayList, LinkedList, HashSet, PriorityQueue and TreeSet
Vector is rarely used. It is basically replaced by Stack

Map interface and its derivatives

The difference between the Map interface and the Collection interface is that the Map interface is used to store key value pairs
Look at a picture:
The Map interface has two implementation classes, of which HashMap and TreeMap need to be mastered

SortedMap interface: an ordered set of key value pairs

The meaning of each interface implementation class

In general, these interface implementation classes encapsulate the functions of the data structure we know, so we can use it directly

summary

There are many collections. Why?
A: because behind the collection are data structures.
Different ways of describing and organizing data result in so many data structures, which is why there are so many collections.

Significance of learning

Advantages and functions of java collection framework

  • Using a mature collection framework helps us write efficient and stable code conveniently and quickly
  • Learning the data structure knowledge behind it will help us understand the advantages, disadvantages and usage scenarios of each collection

Understanding the company's written interview questions in previous years



Collection interface

Collection official document (stamp me)

common method

Collection example

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

public class Demo {
    public static void main(String[] args) {
        Collection<String> list = new ArrayList<>();
        System.out.println(list.size());
        System.out.println(list.isEmpty());
        list.add("I");
        list.add("love");
        list.add("Java");
        System.out.println(list.size());
        System.out.println(list.isEmpty());
        Object[] array = list.toArray();
        System.out.println(Arrays.toString(array));
        for (String s : list) {
            System.out.println(s);
        }
        list.remove("love");
        for (String s : list) {
            System.out.println(s);
        }
        list.clear();
        System.out.println(list.size());
        System.out.println(list.isEmpty());
    }
}

Operation results:

0
true
3
false
[I, love, Java]
I
love
Java
I
Java
0
true

Map interface

Map official document (stamp me)

Common methods of Map

Map code example

import java.util.Map;
import java.util.HashMap;

public class Demo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        System.out.println(map.size());
        System.out.println(map.isEmpty());
        System.out.println(map.get(" Author "));
        System.out.println(map.getOrDefault("author", "nameless"));
        System.out.println(map.containsKey(" Author "));
        System.out.println(map.containsValue(" Anonymity "));
        map.put("author", "Lu Xun");
        map.put("title", "A Madman's Diary");
        map.put("Publication time", "1918 year");
        System.out.println(map.size());
        System.out.println(map.isEmpty());
        System.out.println(map.get(" Author "));
        System.out.println(map.getOrDefault("author", "nameless"));
        System.out.println(map.containsKey(" Author "));
        System.out.println(map.containsValue("nameless"));
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

Operation results:

0
true 
null 
nameless
false 
false 
3
false 
Lu Xun
 Lu Xun
true 
false 
author
 Lu Xun
 Publication time
1918 year
 title
 A Madman's Diary

Tags: Java data structure set

Posted on Sun, 05 Sep 2021 21:30:11 -0400 by Thundarfoot