Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 10 October 2016

What is Java Collections Framework ?

This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.


Here, we learn What is Collections Framework In Java.
The Collections Framework

The Java platform provides a collections framework. It provides a well-designed set of interfaces and classes for storing and manipulating groups of data as single unit called collection.

A collection is an object that represents a group of objects.A collections framework is a unified architecture for representing and manipulating collections.It reduces programming effort while increasing performance.

Collections that do not support modification operations (such as add, remove and clear) are referred to as unmodifiable. Collections that are not unmodifiable are modifiable.

Collections that additionally guarantee that no change in the Collection object will be visible are referred to as immutable. Collections that are not immutable are mutable.

Lists that guarantee that their size remains constant even though the elements can change are referred to as fixed-size. Lists that are not fixed-size are referred to as variable-size.

Collection Implementations Interfaces And their Classes:-

Interface
Implementation Classes
Java.util.List
Java.util.Set
Java.util.Map
Java.util.Queue


Advantages of a collections framework :-

Reduces programming effort, by providing data structures and algorithms so you don't have to write them yourself.
Increases performance, by providing high-performance implementations of data structures and algorithms. Because the various implementations of each interface are interchangeable, programs can be tuned by switching implementations.
Provides interoperability between unrelated APIs, by establishing a common language to pass collections back and forth.
Reduces the effort required to learn APIs, by requiring you to learn multiple ad hoc collection APIs.

Reduces the effort required to design and implement APIs, by not requiring you to produce ad hoc collections APIs. 

JDK 5 New Features :-

When JDK 5 was released, some fundamental changes were made in the Collections Framework.These changes include the addition of generics, autoboxing / unboxing,Enums and the for-each style for loop.

The collections framework consists of :-

Collection interfaces - The primary means by which collections are manipulated.

A group of objects. No assumptions are made about the order of the collection (if any) or whether it can contain duplicate elements.
The familiar set abstraction. No duplicate elements permitted. May or may not be ordered. Extends the Collection interface.
Ordered collection, also known as a sequence. Duplicates are generally permitted. Allows positional access. Extends the Collection interface.
A collection designed for holding elements before processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.
A double ended queue, supporting element insertion and removal at both ends. Extends the Queue interface.
 A mapping from keys to values. Each key can map to one value.
A set whose elements are automatically sorted, either in their natural ordering (see the Comparable interface) or by a Comparator object provided when a SortedSet instance is created. Extends the Set interface.
A map whose mappings are automatically sorted by key, either using the natural ordering of the keys or by a comparator provided when a SortedMap instance is created. Extends the Map interface.
A SortedSet extended with navigation methods reporting closest matches for given search targets. A NavigableSet may be accessed and traversed in either ascending or descending order.
A SortedMap extended with navigation methods returning the closest matches for given search targets. A NavigableMap can be accessed and traversed in either ascending or descending key order.
BlockingQueue 
A Queue with operations that wait for the queue to become nonempty when retrieving an element and that wait for space to become available in the queue when storing an element. (This interface is part of the java.util.concurrent package.)
TransferQueue 
A BlockingQueue in which producers can wait for consumers to receive elements. (This interface is part of the java.util.concurrent package.)
BlockingDeque 
A Deque with operations that wait for the deque to become nonempty when retrieving an element and wait for space to become available in the deque when storing an element. Extends both the Deque and BlockingQueue interfaces. (This interface is part of the java.util.concurrent package.)
A Map with atomic putIfAbsent, remove, and replace methods. (This interface is part of the java.util.concurrent package.)
 A ConcurrentMap that is also a NavigableMap.

General-purpose implementations - The primary implementations of the collection interfaces.

Hash table implementation of the Set interface. The best all-around implementation of the Set interface.
Red-black tree implementation of the NavigableSet interface.
Hash table and linked list implementation of the Set interface. An insertion-ordered Set implementation that runs nearly as fast as HashSet.
Resizable array implementation of the List interface (an unsynchronized Vector). The best all-around implementation of the List interface.
ArrayDeque 
Efficient, resizable array implementation of the Deque interface.
Doubly-linked list implementation of the List interface. Provides better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Also implements the Dequeinterface. When accessed through the Queue interface, LinkedList acts as a FIFO queue.
Heap implementation of an unbounded priority queue.
Hash table implementation of the Map interface (an unsynchronized Hashtable that supports null keys and values). The best all-around implementation of the Map interface.
Red-black tree implementation of the NavigableMap interface.
Hash table and linked list implementation of the Map interface. An insertion-ordered Map implementation that runs nearly as fast as HashMap. Also useful for building caches (see removeEldestEntry(Map.Entry) ).

Legacy implementations - Older collection classes were added in collection interfaces.

Synchronized resizable array implementation of the List interface with additional legacy methods.
Synchronized hash table implementation of the Map interface that does not allow null keys or values, plus additional legacy methods.

Special-purpose implementations

An implementation of the Map interface that stores only weak references to its keys. Storing only weak references enables key-value pairs to be garbage collected when the key is no longer referenced outside of theWeakHashMap. This class is the easiest way to use the power of weak references. It is useful for implementing registry-like data structures, where the utility of an entry vanishes when its key is no longer reachable by any thread.
Identity-based Map implementation based on a hash table. This class is useful for topology-preserving object graph transformations (such as serialization or deep copying). To perform these transformations, you must maintain an identity-based "node table" that keeps track of which objects have already been seen. Identity-based maps are also used to maintain object-to-meta-information mappings in dynamic debuggers and similar systems. Finally, identity-based maps are useful in preventing "spoof attacks" resulting from intentionally perverse equals methods. (IdentityHashMap never invokes the equals method on its keys.) An added benefit of this implementation is that it is fast.
CopyOnWriteArrayList 
A List implementation backed by an copy-on-write array. All mutative operations (such as add, set, and remove) are implemented by making a new copy of the array. No synchronization is necessary, even during iteration, and iterators are guaranteed never to throw ConcurrentModificationException. This implementation is well-suited to maintaining event-handler lists (where change is infrequent, and traversal is frequent and potentially time-consuming).
CopyOnWriteArraySet 
A Set implementation backed by a copy-on-write array. This implementation is similar to CopyOnWriteArrayList. Unlike most Set implementations, the add, remove, and contains methods require time proportional to the size of the set. This implementation is well suited to maintaining event-handler lists that must prevent duplicates.
A high-performance Set implementation backed by a bit vector. All elements of each EnumSet instance must be elements of a single enum type.
A high-performance Map implementation backed by an array. All keys in each EnumMap instance must be elements of a single enum type.

Concurrent implementations –  These implementations are part of java.util.concurrent.

ConcurrentLinkedQueue 
 An unbounded first in, first out (FIFO) queue based on linked nodes.
LinkedBlockingQueue 
An optionally bounded FIFO blocking queue backed by linked nodes.
ArrayBlockingQueue 
 A bounded FIFO blocking queue backed by an array.
PriorityBlockingQueue 
 An unbounded blocking priority queue backed by a priority heap.
DelayQueue 
A time-based scheduling queue backed by a priority heap.
SynchronousQueue 
A simple rendezvous mechanism that uses the BlockingQueue interface.
LinkedBlockingDeque 
An optionally bounded FIFO blocking deque backed by linked nodes.
LinkedTransferQueue 
An unbounded TransferQueue backed by linked nodes.
A highly concurrent, high-performance ConcurrentMap implementation based on a hash table. This implementation never blocks when performing retrievals and enables the client to select the concurrency level for updates. It is intended as a drop-in replacement for Hashtable. In addition to implementing ConcurrentMap, it supports all of the legacy methods of Hashtable.
ConcurrentSkipListSet 
 Skips list implementation of the NavigableSet interface.
ConcurrentSkipListMap 
Skips list implementation of the ConcurrentNavigableMap interface.

Abstract implementations – 

AbstractCollection 
Skeletal Collection implementation that is neither a set nor a list (such as a "bag" or multiset).
Skeletal Set implementation.
Skeletal List implementation backed by a random access data store (such as an array).
AbstractSequentialList 
Skeletal List implementation backed by a sequential access data store (such as a linked list).
Skeletal Queue implementation.
Skeletal Map implementation.

Algorithms - The Collections class contains these useful static methods.

sort(List) 
Sorts a list using a merge sort algorithm, which provides average case performance comparable to a high quality quicksort, guaranteed O(n*log n) performance (unlike quicksort), and stability (unlike quicksort). A stable sort is one that does not reorder equal elements.
binarySearch(List, Object)
Searches for an element in an ordered list using the binary search algorithm.
reverse(List)
Reverses the order of the elements in a list.
shuffle(List) 
Randomly changes the order of the elements in a list.
fill(List, Object)
Overwrites every element in a list with the specified value.
copy(List dest, List src)
Copies the source list into the destination list.
min(Collection)
Returns the minimum element in a collection.
max(Collection) 
Returns the maximum element in a collection.
rotate(List list, int distance)
Rotates all of the elements in the list by the specified distance.
replaceAll(List list, Object oldVal, Object newVal)
Replaces all occurrences of one specified value with another.
indexOfSubList(List source, List target) 
Returns the index of the first sublist of source that is equal to target.
lastIndexOfSubList(List source, List target) 
Returns the index of the last sublist of source that is equal to target.
swap(List, int, int) 
Swaps the elements at the specified positions in the specified list.
frequency(Collection, Object)
Counts the number of times the specified element occurs in the specified collection.
disjoint(Collection, Collection)
Determines whether two collections are disjoint, in other words, whether they contain no elements in common.
addAll(Collection<? super T>, T...)
Adds all of the elements in the specified array to the specified collection.


Share this Blog with yours Friends !!

No comments:

Post a Comment