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

Summary

This chapter introduced you to the Java collections framework, which is a very powerful tool within the Java language that can be used to store, sort, and filter data. The framework is massive and offers tools in the form of interfaces, classes, and methods, some of which are beyond the scope of this chapter. We have focused on Arrays, Lists, Sets, Maps, and Properties. But there are others, such as queues and dequeues, that are worth exploring on your own.

Sets, like their mathematical equivalents, store unique copies of items. Lists are like arrays that can be extended endlessly and support duplicates. Maps are used when dealing with key-value pairs, something very common in contemporary computing, and do not support the use of two of the same keys. Properties work very much like HashMap (a specific type of Map) but offer some extra features, such as the listing of all their contents to streams, which simplifies the printing out of the contents of a list.

Some of the classes offered in the framework are sorted by design, such as TreeHash and TreeMap, while others are not. Depending on how you want to handle data, you will have to decide which is the best collection.

There are standard techniques for looking through data with iterators. These iterators, upon creation, will point to the first element in a list. Iterators offer some basic methods, such as hasNext() and next(), that state whether there is more data in the list and extract data from the list, respectively. While those two are common to all iterators, there are others, such as listIterator, that are much more powerful and allow, for example, the addition of new elements to a list while browsing through it.

We have looked at a chapter-long example that used many of these techniques, and we have introduced the use of the console to read data through the terminal. In the next chapter, we will cover exceptions and how to handle them.

主站蜘蛛池模板: 扎兰屯市| 梧州市| 铁力市| 永吉县| 晴隆县| 衡南县| 田阳县| 龙胜| 迭部县| 武汉市| 博白县| 武定县| 彰化市| 郓城县| 容城县| 中卫市| 柳河县| 东光县| 红原县| 巴林左旗| 塘沽区| 肇州县| 旺苍县| 田阳县| 安新县| 宣威市| 嵊泗县| 丽水市| 阆中市| 慈溪市| 江门市| 保山市| 剑河县| 永年县| 苏尼特右旗| 龙江县| 湖南省| 平乐县| 会泽县| 邵阳市| 泸定县|