Related to recommend

Java Basics · Exceptions

[Java Basics · Inner Classes]

14.1 The concept of sets

A Java collection is like a container that can store any type of data, or it can be combined with generics to store concrete type objects. While the program is running, the Java collection can be dynamically expanded, growing as more elements are added. In Java, collection classes usually reside in the java.util package.

14.2 Structure of collections

(1) Java Collection is mainly composed of two systems, namely Collection system and Map system, in which Collection and Map are the top-level interfaces of the two systems respectively.

(2) Collection mainly has three sub-interfaces, namely List, Set and Queue. The elements in List and Queue are ordered and repeatable, while the elements in Set are unordered and non-repeatable.

(3) List mainly includes ArrayList and LinkedList; Set is a HashSet implementation class; Queues are new collections that have been around since JDK1.5, mainly in the form of arrays and linked lists.

(4) Map belongs to the java.util package and is a part of the Collection, but it is independent of Collection and has no relationship with it. All maps exist in the form of key-values, where keys must be unique. There are three implementation classes: HashMap, HashTable, and TreeMap.

14.3 ArrayList

(1) ArrayList concept

① In the Collection, the List is ordered, and Developer has precise control over the insertion position of each element. You can access and traverse elements through indexes.

② The bottom layer of ArrayList is implemented through arrays, which can be dynamically expanded with the increase of elements.

ArrayList is the most used class in Java collection framework. It is an array queue.

(2) Characteristics of ArrayList

(1) The capacity is not fixed and is dynamically expanded as the capacity increases (the threshold will not be reached).

② Ordered sets (insertion order == output order).

③ Insert elements can be null.

(4) Add, delete, modify and check more efficiently (compared to LinkedList).

⑤ Threads are not safe.

(3) ArrayList common methods

1) add: add

(2) set: modification

(3) the get: the acquisition

④isEmpty: check whether it isEmpty

⑤ Contains: Check whether it contains

⑥size: Gets the set length

⑦toArray: Convert collections to arrays

End up remove: delete

(4) Examples

summary

1. The concept of sets.

2. Structure of sets.

3, the ArrayList.