A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:
Interfaces
The core collection interfaces encapsulate different types of collections, which are shown in the figure
below. These interfaces allow collections to be manipulated independently of the details of their representation. Core collection interfaces are the foundation of the Java Collections Framework. As you
can see in the following figure,
the core collection interfaces form a hierarchy.
The core collection interfaces.
Note that all the core collection interfaces are generic. For example,
this is the
declaration of the Collection interface.
public interface Collection<E>...
The <E> syntax tells you
that the interface is generic.
When you declare a Collection instance you can and should specify the type of object
contained in the collection. Specifying the type allows the
compiler to verify (at compile-time) that the type
of object you put into the
collection is correct, thus reducing
errors at runtime. For information on generic types, see the Generics (Updated)
lesson.
Summary of Interfaces
The core collection
interfaces are the foundation of the Java Collections Framework.
The Java Collections Framework hierarchy consists of two distinct interface trees:
These interfaces allow collections to be manipulated
independently of the details of their representation.
Summary of Implementations
The Java Collections Framework provides several general-purpose implementations of the core interfaces:
Each of the general-purpose
implementations provides
all optional operations contained in its
interface.
Comparison of Lists |
||
ArrayList |
LinkedList |
|
Description |
Dynamic (a.k.a.
growable, resizable) array |
Doubly-linked list |
Random Access with index (get()) |
O(1) |
|
Insertion (add())
/ removal at the back |
amortized O(1) |
O(1) |
Insertion / removal
at the front |
O(n) |
O(1) |
One step
of iteration through an Iterator |
O(1) |
O(1) |
Insertion / removal
in the middle through an Iterator
/ ListIterator |
O(n) |
O(1) |
Insertion / removal
in the middle through the index |
O(n) |
O(n) |
Search contains()
/ removal remove()
by object |
O(n) |
O(n) |
Comparison of Sets |
||
HashSet |
TreeSet |
|
Description |
Hash table |
Self-balancing binary search tree |
Requirements |
Objects need to
implement a hash function (hashCode()) which is consistent with equals() |
Either objects
need to implement the Comparable
interface and be able to compare
to each other; or a Comparator
for the objects must be provided; the ordering must satisfy a "total ordering" |
Test for membership
(contains()) |
O(1) average |
O(log n) |
Insertion (add())
/ removal (remove()) |
O(1) average |
O(log n) |
One step
of iteration through an Iterator |
O(1) |
O(1) |
Iterates through
elements in ascending order |
No |
Yes |
Метод entrySet(), определенный
в интерфейсе
Map, позволят получить все элементы
ассоциативного
массива в виде
множества
объектов
типа Map.Entry.
Map.Entry предоставляет
три основных
метода:
Map<Integer, String> map = new HashMap<Integer,
String>();
Map.Entry<Integer, String> entry;
for ( entry : map.entrySet()) {
Integer key =
entry.getKey();
String value = entry.getValue();
}