Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of the Java collection framework

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the knowledge points of Java collection framework". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Collections Framework (Collections Framework)

First of all, let's be clear that a collection represents a set of objects (just like an array, but the array length cannot change, while a collection can). The collection framework in Java defines a set of specifications for representing and manipulating collections, decoupling specific operations from implementation details.

In fact, to put it bluntly, a collection can be regarded as a micro-database, and the operation is nothing more than four kinds of operations of "adding, deleting and checking." When we learn to use a specific collection class, we need to make clear the space-time complexity of these four operations, and basically we can say that we have mastered this class.

design concept

The main idea can be summarized in one sentence: to provide a set of "small but beautiful" APIs. APIs need to be programmer-friendly, adding new features that programmers can quickly pick up on.

To ensure that the core interface is small enough, the topmost interfaces (i.e. Collection and Map interfaces) do not distinguish subtle differences between mutability, modifiability, and resizability of the collection. In contrast, some operations are optional, and throwing an UnsupportedOperationException at implementation time indicates that the operation is not supported by the collection. The implementer of the collection must declare in the documentation which operations are unsupported.

To ensure that the topmost core interfaces are small enough, they can only contain methods that:

Basic operations, like "add, delete, change and search" as mentioned earlier

There is a compelling performance reason why an important implementation would want to override it.

In addition, all collection classes must be able to provide friendly interoperability, including array objects that do not inherit the Collection class. Therefore, the framework provides a set of methods that allow collection classes and arrays to be transformed into each other and that allow Maps to be viewed as collections.

Two basic classes Collection and Map

In the collection framework's class inheritance hierarchy, there are two interfaces at the top:

Collection represents a set of pure data

Map represents a set of key-value pairs

Collection classes, which typically inherit from Collection or Map, provide two "standard" constructors:

Constructor with no arguments, creating an empty collection class

A constructor of the same type as the base class (Collection or Map) creates a new collection class with the same elements as the given parameter

The above two constructor conventions are not mandatory because interfaces cannot contain constructors, but in the current collection framework, all subclasses that inherit from Collection or Map follow this convention.

Collection

java-collection-hierarchy

As shown above, the Collection class has three main interfaces:

A collection that contains no duplicate elements.

An ordered collection (also known as a sequence)

Queue JDK 1.5 is new, and the difference between the above two collection classes is that Queue is mainly used to store data, not to process data.(A collection designed for holding elements prior to processing.)

Map

MapClassHierarchy

Maps are not true collections, but the interface provides three "collection views" that allow you to manipulate them like collections, as follows:

Map's contents to be viewed as a set of keys

Map's contents to be viewed as a collection of values

Map's contents to be viewed as a set of key-value mappings

"What are the knowledge points of Java Collection Framework" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report