Java - Collections Framework and Data Structures Behind It

Objectives of this section

  • Learn what the Collections Framework is
  • Understand what it means to learn the collections framework
  • Master the collection framework related interfaces and common implementation classes
  • Learn what to learn in the next stage

1 Introduction

The official tutorial
Java Collections Framework Java Collection Framework, also known as a container , is a set of interfaces and their implementation classes containerdefined under a java.utilpackage .interfacesclasses

Its main performance is to put multiple elements in one unit, which is used for fast and convenient storage, retrieval, and management of these elements, which is commonly known as CRUD.

C: create, insert a record into the database;
R: read, query the database record;
U: update, modify the database record;
D: delete, delete the database record.

For example, a deck of playing cards (a set of cards), a mailbox (a set of mails), an address book (a set of mapping relationships between names and phone numbers), and so on.

Classes and Interfaces Overview

insert image description here
insert image description here
insert image description here

Are Maps, Sets, and Lists in order?

First of all, we should be clear about this concept: the ordering and unordering here do not refer to the ordering in the collection, but whether the objects are stored in the order in which the elements are added.

Lists are ordered by storing objects in the order in which the elements were added . His implementation classes ArrayList, LinkedList, and Vector are all ordered.

Map is unordered . Its storage structure is a hash table <key, value> key-value pair. The elements inserted in the map are stored according to the hash value calculated by the key, so it is not in the order in which the elements are added. Stores objects, so Map is unordered. Its implementation classes are: HashMap, TableMap and TreeMap.

Among them, LinkedHashMap is ordered , hashMap is used to ensure the stored value-key-value pairs, and list is used to ensure that the order of insertion is consistent with the order of storage.

Sets are unordered , and elements in a set cannot be repeated. The underlying implementation of set is actually Map, which calculates the hash value of the key to determine the storage location of the element in the array, so it is unordered. It should be that the value of the key in the Map cannot be repeated, so the elements in the set cannot be repeated. . Its implementation classes are: hashSet, TreeSet.

The LinkedHashSet is ordered , the hashSet is used to ensure that the data is unique, and the List is used to ensure that the order of insertion is consistent with the order of storage.
———————————————
Copyright Statement: This article is an original article by CSDN blogger "hust_yfang" and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting .
Original link: https://blog.csdn.net/hust_yfang/article/details/79607138

Sample interview question:
which statement is true for the class java.util.ArrayList?

 A.集合中的元素是有序的   对

 B.集合被保证为不可变的  错

 C.集合中的元素被保证为唯一的  错  有序的 所以不唯一

 D.集合中的元素使用一个唯一的键来存取   错   没有键

 E.集合中的元素被保证为同步的   错  不是同步的

2. The meaning of learning

2.1 The advantages and functions of the Java collection framework

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

2.2 Written test and interview questions

腾讯-Java后台开发面经
1. HashMap 了解不,介绍一下,如果一个对象为 key 时,hashCode 和 equals 方法的用法要注意什么?
2. HashSetHashMap 的区别是什么?
3. HashMap 是线程安全的么?那需要线程安全需要用到什么?
阿里巴巴-Java后台开发面经
1. ArrayListLinkedList 的区别是什么?
2. 有了解过 HashMap 的具体实现么?
3. HashMapConcurrentHashMap 哪个效率更高?
今日头条-Java后台开发面经
1. 编程题:判断一个链表是否是一个回文链表。
2. Redis 的 zset 类型对应到 java 语言中大致是什么类型?
3. hashCode 主要是用来做什么用的?

3. Interfaces

3.1 Basic relationship description

insert image description here

  1. Collection: used to store and manage a set of objects, these objects are generally called elements
    1. Set : The elements cannot be repeated, and the semantics of finding/searching are implied behind them
      1. SortedSet : an ordered set of non-repeatable elements
    2. List : Linear structure
    3. Queue : queue
    4. Deque : double-ended queue
  2. Map : Key-Value-Pair with hidden search/search semantics
    1. SortedMap : an ordered set of key-value pairs

3.2 Collection interface description

Collection official documentation

3.3 Description of common methods of Collection

insert image description here

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("我");
        list.add("爱");
        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("爱");
        for (String s : list) {
    
    
            System.out.println(s);
        }
        list.clear();
        System.out.println(list.size());
        System.out.println(list.isEmpty());
    }
 }

operation result:

0
true
3
false
[,, Java]
我
爱
JavaJava
0
true
public static void main(String[] args) {
    
    
    Collection<String> collection = new ArrayList<>();
    collection.add("hello");
    collection.add("hello2");
    System.out.println(collection);
    
    boolean flg = collection.remove("hello");
    System.out.println(flg);
    
    System.out.println(collection.size());
        
    Object[] objects = collection.toArray();
  //String[] objects = (String[])collection.toArray();
  //不建议进行强制类型转换,上一行注释会出现ClassCastException异常。
    System.out.println(Arrays.toString(objects));
        
    /*collection.clear();
    System.out.println("===================");
    System.out.println(collection);
    System.out.println(collection.isEmpty());*/
}
//结果为:
[hello, hello2]
true
1
[hello2]

public static void main1(String[] args) {
    
    
    Collection<String> collection = new ArrayList<>();
    collection.add("hello");
    collection.add("hello2");
    //collection.add(1);
    //尖括号当中 放的类型 一定要是 包装类 不能是简单的基本类型
    Collection<Integer> collection2 = new ArrayList<>();
    collection2.add(1);
    collection2.add(2);
    collection2.add(13);
}

Note : Casting of whole arrays is not recommended.
As shown in the figure below, Integer[]the conversion Object[]is feasible, but the coercive type conversion of the overall array is not possible.
As shown in the figure below, String[]the conversion Object[]is feasible, but the coercive type conversion of the overall array is not possible.
There are no compilation errors, but there are runtime ClassCastException exceptions.
insert image description here

3.5 Map interface description

Map official documentation

3.6 Description of common methods of Map

insert image description here

3.7 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());
    System.out.println(map.get("作者"));
    System.out.println(map.getOrDefault("作者", "佚名"));
    System.out.println(map.containsKey("作者"));
    System.out.println(map.containsValue("佚名"));
    map.put("作者", "鲁迅");
    map.put("标题", "狂人日记");
    map.put("发表时间", "1918年");
    System.out.println(map.size());
    System.out.println(map.isEmpty());
    System.out.println(map.get("作者"));
    System.out.println(map.getOrDefault("作者", "佚名"));
    System.out.println(map.containsKey("作者"));
    System.out.println(map.containsValue("佚名"));
    for (Map.Entry<String, String> entry : map.entrySet()) {
    
    
      System.out.println(entry.getKey());
      System.out.println(entry.getValue());
   }
 }
}

operation result:

0
true
null
佚名
false
false
3
false
鲁迅
鲁迅
true
false
作者
鲁迅
发表时间
1918年
标题
狂人日记
public static void main5(String[] args) {
    
    
    TreeMap<String,String> map2 = new TreeMap<>();
    map2.put("及时雨","宋江");
    map2.put("国民女神","高圆圆");
    System.out.println(map2);
//TreeMap是继承了SotedMap接口是有序的,HashMap是映射关系无序的。
    HashMap<String,String> map = new HashMap<>();
    map.put("及时雨","宋江");
    map.put("国民女神","高圆圆");
    System.out.println(map);
}
//结果为:
{
    
    及时雨=宋江, 国民女神=高圆圆}
{
    
    国民女神=高圆圆, 及时雨=宋江}

public static void main4(String[] args) {
    
    
    Map<String,String> map = new HashMap<>();
    map.put("及时雨","宋江");
    map.put("国民女神","高圆圆");
    System.out.println(map);
    System.out.println("====================");

    Set<Map.Entry<String, String>> entrySet = map.entrySet();
    for( Map.Entry<String, String> entry : entrySet) {
    
    
        System.out.println("key: "+entry.getKey()+" value:"+entry.getValue());
    }
}
//结果为:
{
    
    国民女神=高圆圆, 及时雨=宋江}
====================
key: 国民女神 value:高圆圆
key: 及时雨 value:宋江
    
public static void main3(String[] args) {
    
    
    Map<String,String> map = new HashMap<>();
    map.put("国民女神","高圆圆");
    map.put("及时雨","宋江");
    String ret2 = map.get("国民女神");
    System.out.println(ret2);
    String ret = map.getOrDefault("及时雨","博哥");
    System.out.println(ret);
    boolean flg = map.containsKey("国民女神2");
    System.out.println(flg);
}
//结果为:
高圆圆
宋江
false

Set<> collection class, the type inside the collection class isMap.Entry<String,String>
insert image description here

4. Implementationclasses

insert image description here
In addition to this, we will also learn about stacks in javaStack

5. The next stage

5.1 Objectives

  1. Learn the basic use of the collections framework
  2. Learn basic data structure knowledge
  3. Learn seven comparison-based sorting algorithms
  4. Learn relevant java knowledge points

5.2 Knowledge points

  1. Using the Collections Framework

    1. Collection
    2. List
    3. ArrayList
    4. LinkedList
    5. Stack
    6. Queue
    7. PriorityQueue
    8. About
    9. Set
    10. HashSet
    11. TreeSet
    12. Map
    13. HashMap
    14. TreeMap
    15. Collections
  2. Theory and Implementation of Data Structure

    1. sequence table
    2. linked list
    3. stack
    4. queue
    5. binary tree
    6. heap
  3. Sorting Algorithm

    1. Insertion sort
    2. Hill sort
    3. selection sort
    4. heap sort
    5. Bubble Sort
    6. quicksort
    7. merge sort
  4. Java syntax

    1. Generic
    2. autoboxing autobox and autounboxing autounbox
    3. Object's equals method
    4. Comparable and Comparator interfaces

Summary of content highlights

Interfaces in Java Collections Framework, the relationship between them and their meanings The relationship between interfaces in the
Java Collections Framework and their respective common implementation classes
The main course content of the next stage

Guess you like

Origin blog.csdn.net/qq_43398758/article/details/121530115