Collection and Map Sources

Common java collection framework

Collection interface, which implements the Iterable interface, has two important and common methods:

Iterator<T> iterator();   //Explains that a collection that implements this interface can take an iterator and iterate using it
default void forEach(Consumer<? super T> action)//You can use foreach for iteration, and Lamda expressions for iteration

Common Collection interfaces have three subinterfaces: List, Set, and Queue.

List, a single-column ordered collection, common implementation classes are ArrayList, LinkedList, and Vector

1. ArrayList implementation: A single-column collection implemented as an array that allows null values, elementData is actually an array of type Object (note: the array is a contiguous memory address in memory)

transient Object[] elementData;

public ArrayList() {

The default initial length is 10, or you can specify an initial length

/** * Default initial capacity. */

private static final int DEFAULT_CAPACITY = 10;

ArrayList adds elements:

public boolean add(E e) {
    add(e, elementData, size);
    return true;

Where modCount starts with 0, add adds elements to the elementDate array, and size defaults to 0

private void add(E e, Object[] elementData, int s) {
    if (s == elementData.length)
        elementData = grow();
    elementData[s] = e;
    size = s + 1;

When adding elements, the array is expanded if the size of the collection==length of the array, otherwise the element to be added is directly assigned to the sth element of the array, size+1

About grow() expansion:

First expansion, initialize 10 lengths:

return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)];

Return an array of new Obect, and expand it again to calculate from the original initial capacity and the minimum initial capacity. The calculation method is slight (simple, just do it yourself). The main point is:

Expansion again returns Arrays'copyof method, which is a copy of the array

return elementData = Arrays.copyOf(elementData, newCapacity);

Array copy: copy length, copy type, and then adjust the system's array copy method, System.arraycopy is a native method, the underlying C

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;



Look at the get method again: check the length before returning the index elements of the array

public E get(int index) {
    Objects.checkIndex(index, size);
    return elementData(index);
return (E) elementData[index];

There are many other methods that are basically based on array replication, such as remove, addAll

2. The Vector implementation differs from ArrayList by adding the synchronized keyword in many ways, that is, Vector is thread-safe, and when multiple threads access instances of the same Vector object at the same time, there are no thread-safe issues

Both ArrayList and Vector implement Cloneable, interface, which can be cloned and serialized


3. LinkedList, first of all, it is non-thread-safe, and its methods do not include the synchronized keyword. In addition, LinkedList implements the Deque ue interface, which inherits Queue, which means it also has some queue functions

public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>,Deque<E>, Cloneable,

Looking at the LinkedList properties, it is of type Node, with first and last

 * Pointer to first and last node.
transient Node<E> first;
transient Node<E> last;

The Node class is a privately owned static class that contains the element itself, the previous element, and the next element, so LinkedList is a two-way chain table structure:

private static class Node<E> {
    E item;
    Node<E> next;
    Node<E> prev;

Look at some of its ways:

As you can see, you can add from either the first element or the last element, and add(), which is linked to the last element

There are also some ways to Queue:

You can either pick from the team or you can specify the end from the team. See each method in the code and don't give a detailed description.

Set collection, single column, no duplicate collection, can have null elements, but can only have one null. Common implementations are HashSet, TreeSet, SortedSet

1. HashSet, as its name implies, is a set set set set set set set set set implemented by hash algorithm, which can be interpreted as an implementation of hash algorithm to determine whether the elements in the set are consistent or not.

This has to be compared to HashMap because:

Inside the constructor of HashSet is a new HashMap object

When adding an element to a set set set, an int value obtained by exclusive-sum operation between the hashcode value of the element and the binary of 16 is used as the key to determine whether the map is empty or not.

For traversal of HashSet, only foreach and iterator can be used, not in the way of specifying subscripts (without the concept of subscripts). Traversing by iterator is actually an operation on the map object - > the iterator object of the ketSet of the obtained map:



Map interface:

Tip: After the article is written, the catalog can be generated automatically, how to generate the help document to the right


Article Directory



Tip: Here you can add a general description of what this article will record:
For example: With the continuous development of artificial intelligence, machine learning is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: The following is the main body of this article. The following cases can be used as reference.

1. What is pandas?

Example: pandas is a NumPy-based tool created to solve data analysis tasks.

2. Steps for use

1. Introducing Libraries

The code is as follows (example):

import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('ignore') import ssl ssl._create_default_https_context = ssl._create_unverified_context 

2. Read in data

The code is as follows (example):

data = pd.read_csv( '') print(data.head()) 

The url network requested data used here.


Tip: Here is a summary of the article:
For example: That's what we're going to talk about today. This article just briefly introduces the use of pandas, which provides a number of functions and methods that enable us to process data quickly and easily.














Keywords: Java

Added by Ice on Thu, 03 Feb 2022 05:19:28 +0200