Collections

suggest change

Versions

[{“Name”:“Java SE 1.2”,“GroupName”:null},{“Name”:“Java SE 1.3”,“GroupName”:null},{“Name”:“Java SE 1.4”,“GroupName”:null},{“Name”:“Java SE 5”,“GroupName”:null},{“Name”:“Java SE 6”,“GroupName”:null},{“Name”:“Java SE 7”,“GroupName”:null},{“Name”:“Java SE 8”,“GroupName”:null},{“Name”:“Java SE 9 (Early Access)”,“GroupName”:null}]

Introduction

The collections framework in java.util provides a number of generic classes for sets of data with functionality that can’t be provided by regular arrays.

Collections framework contains interfaces for Collection<O>, with main sub-interfaces List<O> and Set<O>, and mapping collection Map<K,V>. Collections are the root interface and are being implemented by many other collection frameworks.

Remarks

Collections are objects that can store collections of other objects inside of them. You can specify the type of data stored in a collection using Generics.

Collections generally use the java.util or java.util.concurrent namespaces.

Java 1.4.2 and below do not support generics. As such, you can not specify the type parameters that a collection contains. In addition to not having type safety, you must also use casts to get the correct type back from a collection.

In addition to Collection<E>, there are multiple major types of collections, some of which have subtypes.

Java 5 adds in a new collection type:

Java 6 adds in some new subtypes of collections.

Note that the above items are all interfaces. In order to use them, you must find the appropriate implementing classes, such as ArrayList, HashSet, HashMap, or PriorityQueue.

Each type of collection has multiple implementations that have different performance metrics and use cases.

Note that the Liskov Substitution Principle applies to the collection subtypes. That is, a SortedSet<E> can be passed to a function expecting a Set<E>. It is also useful to read about Bounded Parameters in the Generics section for more information on how to use collections with class inheritance.

If you want to create your own collections, it may be easier to inherit one of the abstract classes (such as AbstractList) instead of implementing the interface.

Prior to 1.2, you had to use the following classes/interfaces instead:

These classes are obsolete and should not be used in modern code.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents