Latest web development tutorials

Java Collections Framework

Long before in Java 2, Java provides ad hoc category. For example: Dictionary, Vector, Stack, and Properties classes used to store and manipulate groups of objects.

Although these classes are very useful, but they lack a central, unifying theme. For this reason, use the Vector class manner and use the Properties class has a very different way.

Collections Framework is designed to meet the following objectives.

  • The framework must be high performance. The basic set (dynamic arrays, linked lists, trees, hash tables) implementation must also be efficient.
  • The framework allows for different types of collections, in a similar manner with a high degree of interoperability.
  • Extension and adaptation of a collection must be simple.

To this end, the entire collection framework is designed around a set of standard interfaces. You can use these standard interfaces to achieve, such as: LinkedList, HashSet, TreeSet and the like, in addition you can also implement your own collection via these interfaces.

Collections framework is a unified architecture used to represent and manipulate collections. All collections framework contains the following:

  • Interface: It is an abstract data type represents collection. Interface allows independent manipulation of the collection details of its representatives. In object-oriented languages, interfaces generally form a hierarchy.
  • Implementation (class): is the realization of a collection of interfaces. Essentially, they are reusable data structures.
  • Algorithm: some useful computational object that implements the interface in the collection method of performing, for example: searching and sorting. These algorithms are called multi-state, it is because the same method can have different implementations on a similar interface.

In addition to the collection, the framework also defines several Map interfaces and classes. Map is stored in key / value pairs. Despite Map not collections, but they are fully integrated in the collection.


Collection Interfaces

Collection framework defines a number of interfaces. This section provides an overview of each interface:

No. Interface Description
1 Collection interface allows you to use a group of objects, is the root interface Collection hierarchy.
2 List interface elements inherited from Collection and a List instance store an ordered collection.
3 Set
Inherited from Collection, it is a collection that contains no duplicate elements.
4 SortedSet
Set in an orderly succession to save the set.
5 Map
The only maps keys to values.
6 Map.Entry
A description element (key / value pairs) in a Map. Map is an inner class.
7 SortedMap
Inherited from Map, so Key held in ascending order.
8 Enumeration
This is a traditional definition of interfaces and methods by which you can enumerate (once obtained a) objects in the collection elements. This tradition has been replaced by the interface iterator.

Collections

Java provides a set of standard class implements the Collection interface. Some of them are specific classes that can use it, while others are abstract classes, provides a partial implementation of the interface.

Standard collection classes are summarized in the following table:

No. Class Description
1 AbstractCollection
To achieve most of the collection interfaces.
2 AbstractList
Inheritance in AbstractCollection and achieved most of the List interface.
3 AbstractSequentialList
Inherited from AbstractList, it provides access to data elements in the chain rather than random access.
4 LinkedList
Inherited from AbstractSequentialList, it implements a linked list.
5 ArrayList
Through inheritance AbstractList, dynamic arrays.
6 AbstractSet
Inheritance in AbstractCollection and achieved most of the Set interface.
7 HashSet
He inherited AbstractSet, and use a hash table.
8 LinkedHashSet
With predictable iteration order and linked list hash table Set interface.
9 TreeSet
Inherited from AbstractSet, using elements of the natural order of the elements in order.
10 AbstractMap
To achieve most of the Map interface.
11 HashMap
HashMap is a hash table, which stores the contents of key-value pairs (key-value) mapping.
HashMap inherited AbstractMap, realized Map, Cloneable, java.io.Serializable interface.
12 TreeMap
He inherited AbstractMap, and the use of a tree.
13 WeakHashMap
Inherited AbstractMap class, use a hash table weak keys.
14 LinkedHashMap
Inherited from HashMap, using the elements of the natural order of the elements to be sorted.
15 IdentityHashMap
AbstractMap class inheritance, using reference-equality when comparing documents.

In the previous tutorial we have been discussed by java.util classes defined in the package, as follows:

No. Class Description
1 Vector
Vector class implements a dynamic array. And ArrayList and similar, but the two are different.
2 Stack
Stack is a subclass of Vector, which implements a standard LIFO stack.
3 Dictionary
Dictionary class is an abstract class that is used to store key / value pairs, similar to a class action and Map.
4 Hashtable
Hashtable is part of the original java.util is a Dictionary concrete implementation.
5 Properties
Properties inherits from Hashtable. Represents a persistent set of properties. Each key and its corresponding value in the property list is a string.
6 BitSet
A Bitset class creates a special type of array to hold the bit value. BitSet with the size of the array will need to increase.

A Bitset class creates a special type of array to hold the bit value. BitSet with the size of the array will need to increase.


Collection algorithm

Collections Framework defines several algorithms can be used for the collection and mapping. These algorithms are static methods of the class is defined as a collection.

When trying to compare incompatible types, some methods can throw a ClassCastException. When attempting to modify a non-modifiable set, an UnsupportedOperationException exception.

Collection defines three static variables: EMPTY_SET EMPTY_LIST, EMPTY_MAP of. These variables are immutable.

No. Algorithm Description
1 Collection Algorithms
Here is a list of all algorithms.

How to use the iterator

Typically, you will want to traverse a collection of elements. For example, each element of the collection is displayed.

Do it the easiest way is to use an iterator, it is an object that implements the Iterator interface or ListIterator interface.

Iterators, so that you can remove the element to get or set by circulation. ListIterator inherited Iterator, allowing two-way traverse the list and modify elements.

All methods and listIterator Iterator interface provided listed here by way of example.

How to use the comparator

TreeSet and TreeMap sort order according to the storage element. However, this is by comparing the device to precisely defined in terms of what sort order.

This interface allows us in different ways to sort a set.

No. Comparison of methods described
1 Use Java Comparator
Comparator interface provides all the methods listed here by way of example

to sum up

Java Collections Framework provides the programmer with prepackaged data structures and algorithms to manipulate them.

A collection is an object that can hold references to other objects. Collection operation interface declaration for each type of collection can be performed.

Collections Framework classes and interfaces are in the java.util package.