Overview of Collection interface and Map interface
Java collection is like a container, which can store any type of data. In Java, collection classes usually exist in the Java.util package.
(Recommended tutorial: java introductory tutorial)
Java collections are mainly composed of two major systems, namely: Collection system and Map system. Among them, the Collection system and the Map system are the top-level interfaces in the two systems respectively.
Collection mainly has three sub-interfaces, namely List, Set, and Queue.
Ordered and repeatable: List, Queue
Unordered and repeatable: Set
Collection interface
1. List has Order, repeatable
1. ArrayList
Advantages: The underlying data structure is an array, which is fast to query and slow to add and delete.
Disadvantages: Thread-unsafe, but high efficiency.
2. Vector
Advantages: The underlying data structure is an array, which is fast to query and slow to add and delete.
Disadvantages: Thread safety, low efficiency
3. LinkedList
Advantages: The underlying data structure is an array, the query is slow, and blocks are added and deleted.
Disadvantages: Thread unsafe, high efficiency
2. Set is unordered, the only one
1.HashSet
The underlying data structure is a hash table. (Unordered, unique)
How to ensure the uniqueness of elements?
Depends on two methods: hashCode() and equals()
2, LinkedHashSet
The underlying data structures are linked lists and hash tables. (FIFO insertion is ordered and unique)
Ⅰ. The elements are guaranteed to be ordered by the linked list
II. The elements are guaranteed to be unique by the hash table
3. TreeSet
The underlying data structure is a red-black tree. (Unique, ordered)
How to ensure the sorting of elements?
Natural sorting, comparator sorting.
How to ensure the uniqueness of elements?
Decide according to whether the return value of the comparison is 0.
(Video tutorial recommendation: java course)
Map interface
1. The Map interface has three important implementations The classes are: HashMap, TreeMap, and HashTable.
2. Orderly: TreeMap. Unordered: HashMap, HashTable.
3. The main difference between HashTable and HashMap:
The method of Hashtable is synchronous, but the method of HashMap is not synchronous.
4. Hashtable is thread-safe, but HashMap is not thread-safe.
5. HashMap is more efficient than Hashtable.
If there are no requirements for synchronization or compatibility with legacy code, it is recommended to use HashMap. Looking at the source code of Hashtable, we can find that, except for the constructor, all public method declarations of Hashtable have the synchronized keyword, but there is no such thing in the source code of HashMap.
6. Hashtable does not allow null values, and HashMap allows null values (both key and value are allowed).
7. Different parent classes: The parent class of Hashtable is Dictionary, and the parent class of HashMap is AbstractMap.
The above is the detailed content of Overview of Collection interface and Map interface. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When we assemble the computer, although the installation process is simple, we often encounter problems in the wiring. Often, users mistakenly plug the power supply line of the CPU radiator into the SYS_FAN. Although the fan can rotate, it may not work when the computer is turned on. There will be an F1 error "CPUFanError", which also causes the CPU cooler to be unable to adjust the speed intelligently. Let's share the common knowledge about the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard. Popular science on the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard 1. CPU_FANCPU_FAN is a dedicated interface for the CPU radiator and works at 12V

As a modern and efficient programming language, Go language has rich programming paradigms and design patterns that can help developers write high-quality, maintainable code. This article will introduce common programming paradigms and design patterns in the Go language and provide specific code examples. 1. Object-oriented programming In the Go language, you can use structures and methods to implement object-oriented programming. By defining a structure and binding methods to the structure, the object-oriented features of data encapsulation and behavior binding can be achieved. packagemaini

Optimizing the performance of Go language map In Go language, map is a very commonly used data structure, used to store a collection of key-value pairs. However, map performance may suffer when processing large amounts of data. In order to improve the performance of map, we can take some optimization measures to reduce the time complexity of map operations, thereby improving the execution efficiency of the program. 1. Pre-allocate map capacity. When creating a map, we can reduce the number of map expansions and improve program performance by pre-allocating capacity. Generally, we

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

The reason for the error is in python. The reason why NotImplementedError() is thrown in Tornado may be because an abstract method or interface is not implemented. These methods or interfaces are declared in the parent class but not implemented in the child class. Subclasses need to implement these methods or interfaces to work properly. How to solve this problem is to implement the abstract method or interface declared by the parent class in the child class. If you are using a class to inherit from another class and you see this error, you should implement all the abstract methods declared in the parent class in the child class. If you are using an interface and you see this error, you should implement all methods declared in the interface in the class that implements the interface. If you are not sure which

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

As a new operating system launched by Huawei, Hongmeng system has caused quite a stir in the industry. As a new attempt by Huawei after the US ban, Hongmeng system has high hopes and expectations. Recently, I was fortunate enough to get a Huawei mobile phone equipped with Hongmeng system. After a period of use and actual testing, I will share some functional testing and usage experience of Hongmeng system. First, let’s take a look at the interface and functions of Hongmeng system. The Hongmeng system adopts Huawei's own design style as a whole, which is simple, clear and smooth in operation. On the desktop, various

When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.
