[Learning JAVA from scratch | Article 23] Collection Architecture

5443612af38f44a4824110f7382812f3.png

Table of contents

Foreword:

Single-column collection:     

The difference between set and list:

Two-column collection:

Features of map:

Summarize:


 

Foreword:

                JAVA provides us with a lot of collections, these collections have their own unique characteristics, so we have to learn all the collections, but before learning all the collections, we still introduce the collection architecture of JAVA, which will help you better understand the entire JAVA collection framework.

The entire collection can actually be divided into two categories: single-column collections and double-column collections

Single-column collection:     

23f2f8d0b19247fd84ff260292ee0ba5.png

A single-column collection in Java refers to a collection that contains only one element, also known as a single-element collection or a single-value collection. A single-column collection can only add one element at a time. In Java, there are mainly the following types of single-column collections:

1. Singleton Set
Singleton Set is a collection containing only one element, and the element is not allowed to be empty. There is only one element in the Singleton Set, so its size() method returns 1.

Sample code:

Set<String> singletonSet = Collections.singleton("hello");

2. Singleton List
Singleton List is also a collection containing only one element, but it is ordered and can contain repeated elements. There is only one element in the Singleton List, so its size() method returns 1.

Sample code:

List<String> singletonList = Collections.singletonList("hello");

It should be noted that the Singleton collection is immutable, that is, elements cannot be added or removed to it. If you need to add or delete elements, you need to create a new collection object and assign a value. In addition, the elements of a Singleton collection can only be accessed through iterators.

The difference between set and list:

List and set are two commonly used collection types in Java. Their main differences lie in the following aspects:

1. The element order
list is an ordered collection, that is, it maintains the insertion order of the elements, and can be accessed and operated according to the index of the elements (ie, the insertion order). set is an unordered collection, it does not maintain the insertion order of elements, and cannot be accessed and manipulated according to the index of the element.
2. The uniqueness of elements
The list can contain repeated elements, that is, the same element can appear multiple times; the set element is unique, that is, the same element can only appear once.
3. Implementation method
list can be realized by array or linked list; set can be realized by hash table or tree structure. Therefore, the access speed of list is fast, but it takes a long time to add, delete elements and find elements; while the speed of adding, deleting elements and finding elements of set is fast (average time complexity is O(1)), but it takes a long time to access elements (time complexity is O(n)) .

In short , if you need to maintain the order of elements, allow duplicate elements, and need to access and operate according to the index, you should use list; if you need to ensure the uniqueness of elements, do not need to maintain the order of elements, and the performance requirements of search, add and delete operations are high, you should use set.

Two-column collection:

A double-column collection in Java refers to a collection containing two elements, which can store key-value pairs of different types, and are usually used to build data structures such as associative arrays and maps. In Java, the two-column collection mainly has the following types:

1. Map
Map is a collection of key-value pair mappings, where both the key and the value can be objects of any type. The keys in the Map are unique, so there cannot be duplicate keys. Commonly used implementation classes include HashMap, LinkedHashMap, TreeMap, etc.

Sample code:

Map<String, Integer> map = new HashMap<>();
map.put("Java", 100);
map.put("Python", 90);
map.put("C++", 80);

Common implementation classes of map: 

1. Hashtable
Hashtable is a thread-safe key-value pair mapping collection, similar to HashMap, but all its methods are synchronized. Hashtable also does not allow keys and values ​​to be null. Hashtable is implemented through a hash table.

Sample code:

Hashtable<String, Integer> hashtable = new Hashtable<>();
hashtable.put("Java", 100);
hashtable.put("Python", 90);
hashtable.put("C++", 80);

2. LinkedHashMap
LinkedHashMap is a Map that maintains the insertion order. It records the insertion order of elements and can be traversed according to the insertion order. LinkedHashMap is implemented through a hash table and a doubly linked list.

Sample code:

Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("Java", 100);
linkedHashMap.put("Python", 90);
linkedHashMap.put("C++", 80);

3. TreeMap
TreeMap is an ordered Map that is traversed according to the natural order of the keys or the order of the comparator. By default, TreeMap traverses according to the natural order of the keys. If you need to customize the sorting rules, you can implement the Comparator interface. TreeMap is implemented by red-black tree.

Sample code:

Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Java", 100);
treeMap.put("Python", 90);
treeMap.put("C++", 80);

It should be noted that the double-column collections in Java are all implemented based on the Map interface, so they all have the basic methods of the Map interface, such as put, get, containsKey, and so on . When using double-column collections, you need to select an appropriate implementation class based on specific usage scenarios and performance requirements.

Features of map:

Map is a data structure of key-value pairs, and its characteristics are as follows:

1. Storage method : Map stores data in the form of key-value pairs, that is, each key is associated with a value. This method facilitates quick search and access of data, for example, the corresponding value can be quickly obtained through the key.

2. Unique key : The key in the Map is unique, that is, there can only be one value associated with a given key at a time. If the inserted key already exists, the newly inserted value overwrites the old value.

3. Unordered : The key-value pairs in the Map are unordered, unlike arrays and lists that have a fixed order. We can lookup and access based on the keys, regardless of their position in the Map.

4. Can store different types of values : key-value pairs in Map can store different types of data, which makes Map an ideal data structure for storing and manipulating various types of data.

5. Can be modified dynamically : Map is a dynamic data structure that can insert, delete or modify key-value pairs as needed, which makes it very flexible and practical.

Summarize:

        This article introduces single-column collections and double-column collections in detail, and builds a basic system framework for collections for us. Later, we will improve the content of these frameworks step by step.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

69e9169c980f43e0aad31ff9ada88a9c.png

 

 

Guess you like

Origin blog.csdn.net/fckbb/article/details/131407587