Java Collections framework

Purpose of Collections:

A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections.
In Arrays also we can store group of object or elements then what is the disadvantage of storing elements
in arrays and advantage of storing elements in collection i listed   out below

Drawbacks of Arrays:


  • Arrays are fixed in size once array is created size of that array can't be increase or decrease.
  • There is no predefined method support in order  to perform operations on group of objects everything developed by programmer
  • There is underlying datastructure   for organization of elements
  • Arrays can hold homogeneous data   elements or objects(ie only particular elements collection only like integer collection or string collection ).


Benefits Of Collections:


  • Dynamically size will increase no need to define size before
  • So many predefined methods are available like methods for sorting removing elements.
  • Reduces the programmer effort
  • Increase performance speed and quality. 
  • Collections can hold heterogeneous elements or objects
  • There is underlying datastructure for every collections.  

Core interfaces in Java Collection Framework:


 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  above figure, the core collection interfaces form a hierarchy.


Collection : The root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations more 

specific sub interfaces, such as Set and List.


List:


An ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).insertion order is preserved.


Set:

A collection that cannot contain duplicate elements.it unordered  collection where insertion order not preserved.


Queue :

 A collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
 Interface section.

Deque :

 A collection used to hold multiple elements prior to processing. Besides basic Collection operations, a deque provides additional insertion, extraction, and inspection operations.
deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both ends. Also see The Deque Interface section.


Map :

An object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. 

SortedSet :

A Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. Also see The SortedSet Interface section.


SortedMap : 

A Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories. Also see The SortedMap Interface section.


These are main core interfaces based on our requirement we use their implementation classes in our programs

Factors that could help on deciding a Collection

There are various factors that can be considered when selecting an appropriate collection for a particular problem. These factors are:
Ordering - Some sort of ordering in the elements. For example, sorted order, insertion order or no specific ordering.
Duplicates - May or may not want to allow duplicate elements in a collection.
Thread Safe - Ensure the safety of the elements in a collections in case there are multiple threads accessing it.
Key-Value pair - Store in key-value pairs.
Blocking operations - Wait for the collection to become non-empty when retrieving an element.
Random Access - Instant retrieval of an element.
Upper Bounds - To limit the maximum number of elements a collection can hold.
There are also other factors like priority, delay etc..


Concrete classes in collections in Collections Framework:



List Implementations

  • ArrayList
  • Vector
  • LinkedList

More about list imeplentations and examples click below link

http://javabook1.blogspot.in/p/list-implementations.html


Queue Implementations

  • PriorityQue


Set Implementations

  • HashSet
  • TreeSet
  • LinkedHashSet


More about Set imeplentations and examples click below linkhttp://javabook1.blogspot.in/p/set-implementations.html


Map Implementations

  • HashMap
  • TreeMap
  • HashTable
  • LinkedHashMap
  



















Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

0 comments: