Java collections

Collections are interfaces and classes that help us store more than one object. We have already seen arrays that can do that, and also ArrayList in the previous chapters, but we did not discuss in detail what other possibilities there are in the JDK. Here, we will go into more detail, but leave the streams and the functional methods for later chapters, and we will also refrain to go into details that is rather the task of a reference book.

Using implementation of the collection classes and interfaces reduces the programming effort. First of all, you do not need to program something that is already there. Secondly, these classes are highly optimized, both in implementation and in their features. They have very well designed API as well as the code is fast and uses small memory footprint. Sorry to say that their code was written long time ago and many times it is not a good style, hard to read, and understand.

When you use a collection from the JDK, it is more likely that you can interoperate with some library. If you cook your own version of linked lists, it is not likely that you will find a readymade solution that will sort your list. If you use the LinkedList class in the JDK's standard class library, you will get a readymade solution from the Collections class, right from the JDK. It is also worth mentioning that the Java language itself supports these classes, for example, you can easily iterate through the elements of a Collection with a shortened special syntax.

The collections in JDK contain interfaces that define the behavior of the different collection types, implementation classes, and algorithms that perform certain actions such as sorting. Many times, these algorithms work on different implementation versions, getting the same result, but optimized for the implementation specific class.

You can use the API given by the interface, and if you change the implementation in your code, you will get an optimized version fitting the implementation.

The collection interfaces can be categorized in two bags. One bag contains the interfaces that extend the Collection interface, and the other one contains Map, and a SortedMap extending Map. This way, Map is not really a collection, as it does not simply contain other objects but also pair values to keys.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.228