官术网_书友最值得收藏!

Introduction

Java comes with a built-in Collections API, allowing you to manipulate data structures with very little effort. A collection is an object that contains multiple elements. Collections are used to store, share, process, and communicate aggregated data. We call this system the Java collections framework.

As part of this framework, there are different components that are used to optimize our interaction with the actual data:

  • Interfaces: Abstract data types that represent collections
  • Implementations: Specific implementations of the collection interfaces
  • Algorithms: Polymorphic methods used to process the data within a collection for operations such as sorting and searching

    Note

    Other programming languages have their own collection frameworks. For example, C++ has the Standard Template Library (STL). Java boasts simplicity when it comes to its collection framework.

Using the collections framework has many benefits, including a reduction in the complexity of creating programs that deal with data structures, an increase in the performance of programs, a simplification of API creation and use, and an increase in the reuse of functioning software.

The collections framework is relevant even when handling data that can be accessed by several processes simultaneously, as this would be the case in multithreaded programming scenarios. However, it is not the intention of this chapter to deal with concurrent programming.

The Collections API comes with five main interfaces:

  • Set: A collection that contains no duplicates
  • List: An ordered collection or sequence, allowing for duplicates
  • Queue: A collection that sorts data in the order of its arrival, typically handled as a First In First Out (FIFO) process
  • Deque: Essentially a queue that allows for data insertion at both ends, meaning that it can be handled both as FIFO and Last In First Out (LIFO)
  • Map: Relates keys—which must be unique—to values

In this chapter, we will define the main interfaces (lists, sets, and maps), and explore examples of their respective uses. The framework has even more interfaces than the ones listed previously, but the others are either just variations of those listed or are outside the scope of this chapter. Furthermore, we will look at how arrays work in much more depth than we have previously.

The definition of a simple collection—in this case, a specific type of set would be as follows:

Set mySet = new HashSet();

Note

The different available classes for sets, lists, queues, deques, and maps are named after the interfaces. The different classes present different properties, as we will see later in the chapter.

主站蜘蛛池模板: 德安县| 古浪县| 兰溪市| 凤凰县| 玉溪市| 阿克陶县| 南川市| 邵阳市| 宜兴市| 东山县| 中超| 雷山县| 瑞金市| 揭东县| 房产| 遂平县| 乌拉特后旗| 彩票| 博客| 衡阳县| 绥棱县| 山东| 大悟县| 香格里拉县| 文昌市| 牡丹江市| 定陶县| 涞水县| 扎兰屯市| 马尔康县| 盐边县| 民和| 教育| 英吉沙县| 贺兰县| 来凤县| 玉林市| 西畴县| 黄龙县| 扶风县| 和顺县|