Collection framework and the data structure behind it

Table of Contents

1 Introduction

2. The meaning of learning

2.1 Advantages and functions of Java collection framework

3. Interface

3.1 Basic relationship description

3.2 Collection interface description

3.3 Collection common methods

3.4 Collection example

3.5 Map interface description

3.6 Map example

4. Implementation

5.Framework

5.1. Use of collection framework


1. Introduction

Java
collection framework
Java Collection Framework
, also known as container
container
, is defined in
java.util
A set of interfaces under the package interfaces
and its implementation class
classes
.

Its main performance is to combine multiple elements
element
Placed in a single unit for quick and easy storage of these elements
store
Retrieve retrieve, manage
manipulate
, which is what we usually call addition, deletion, checking and modification
CRUD
.

For example, a deck of playing cards
(
a collection of cards
)
, a mailbox
(
A collection of emails
)
, an address book
(
Mapping relationship between a set of names and phone numbers
)
etc.

2. The meaning of learning

2.1 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.

3. Interface

3.1 Basic relationship description

1.
Collection
: Used to store and manage a group of objects
objects
, these objects are generally called elements
elements

1.
Set
:
Elements cannot be repeated, and there is a hidden meaning behind them.
Find
/
Search
semantics

1.
SortedSet
:
An ordered set of non-repeating elements

2.
List
:
linear structure

3.
Queue
:
Queue

4.
Deque
:
deque

Tencent

Java
Backend development interview experience

1.
HashMap
Do you understand? Let me introduce it. If an object is
key
hour,
hashCode
and
equals
What should we pay attention to when using methods?

2.
HashSet
and
HashMap
What’s the difference?

3.
HashMap
Is it thread safe? So what does it take to require thread safety?

Alibaba
-Java
Backend development interview experience

1.ArrayList
and
LinkedList
What’s the difference?

2.
Have you ever understood
HashMap
Is it a specific implementation?

3. HashMap
and
ConcurrentHashMap
Which one is more efficient?

Today’s headlines
-Java
Backend development interview experience

1.
Programming question: Determine whether a linked list is a palindrome linked list.

2. Redis
of
zset
type corresponds to
java
What are the general types in the language?

3.hashCode
What is it mainly used for?

Description

boolean add(E e)

element
e
put in collection

void clear()

Remove all elements from the collection

boolean isEmpty()

Determine whether the set does not have any elements, commonly known as the empty set

boolean remove(Object e)

if element
e
appear in the collection, delete one of them

int size()

Returns the number of elements in the collection

Object[] toArray()

Returns an array containing all elements in the collection

2.
Map
:
key value pair
Key

Value

Pair
, hidden behind
Find
/
Search
semantics

1.
SortedMap
:
An ordered set of key-value pairs

3.2 Collection Interface Description

Collection (Java Platform SE 8)

3.3 Collection Common Methods

3.4 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());
}

Description

V get(Object k)

According to the specified
k
Find the corresponding
v

V getOrDefault(Object k, V defaultValue)

According to the specified
k
Find the corresponding
v
, if not found, replace with default value

V put(K key, V value)

will specify
k-v
put into a
Map

boolean containsKey(Object key)

Determine whether it contains
key

boolean containsValue(Object value)

Determine whether it contains
value

Set> entrySet()

Return all key-value pairs

boolean isEmpty()

Determine whether it is empty

int size()

Returns the number of key-value pairs

operation result:

3.5 Map Interface Description

Map (Java Platform SE 8)

3.6 Map 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());

4. Implementation

Stack in java

System.out.println(map.get("author"));
System.out.println(map.getOrDefault("Author", "Anonymous"));
System.out.println(map.containsKey("author"));
System.out.println(map.containsValue("Anonymous"));
map.put("Author", "Lu Xun");
map.put("Title", "Madman's Diary");
map.put("Published time", "1918");
System.out.println(map.size());
System.out.println(map.isEmpty());
System.out.println(map.get("author"));
System.out.println(map.getOrDefault("Author", "Anonymous"));
System.out.println(map.containsKey("author"));
System.out.println(map.containsValue("Anonymous"));
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
0
true
null
Anonymous
false
false
3
false
Lu Xun
Lu Xun
true
false
author
Lu Xun
issuing time
1918
title
Diary of a Madman

5.Frame

5.1. Use of collection framework

1.
Collection

2.
List

3.
ArrayList

4.
LinkedList

5.
Stack

6.
Queue

7.
PriorityQueue

8.
Deque

9.
Set

10.
HashSet

11.
TreeSet

12.
Map

13.
HashMap

14.
TreeMap

15.
Collections

2.
The theory and implementation of data structures

1.
Sequence table

2.
linked list

3.
stack

4.
Queue

5.
Binary tree

6.
pile

3.
Sorting Algorithm

1.
insertion sort

2.
Hill sort

3.
selection sort

4.
Heap sort

5.
Bubble Sort

6.
Quick sort

7.
merge sort

4.Java
grammar

1.
Generics
Generic

2.
autoboxing
autobox
and automatic unboxing
autounbox

3.
Object
of
equals
method

4.
Comparable
and
Comparator
interface