Arrays:

An Array is an indexed collection of fixed number of homogeneous data elements.
The main advantage of Arrays is we can represent multiple values by using single variable, so that readable of the code will be improved.

Limitation of Arrays:

1. Arrays are fixed in size i.e., once we create an array there is no chance of increasing or decreasing the size based on our requirement. Due to this to use Array’s concept compulsory we should know the size advance which may not possible always.
2. Array can hold only homogeneous data type elements.
For Example

Student[] s = new Student[1000];
s[0] = new Student();
s[1] = new Customer();

Compile time error : incompatible types found: Customer required: Student.
We can solve this problem by using Object type arrays.
For Example>:

Object[] a = new Object[1000];
a[0] = new Student();
a[1] = new Customer();

3. Array’s concept is not implemented based on some standard data structure and hence readymade method support is not available. for every requirement we have to write the code explicitly which increases complexity of programming.
To overcome above problems of Arrays we should go for Collection’s concept.

Collections:

1. Collections are Growable in nature i.e., based on our requirement we can increase or decrease the size.
2. Collections can hold both homogenous and heterogeneous elements.
3. Every Collection class is implemented based on some standard data structure. hence for every requirement readymade support is available.
4. Being a programmer we are responsible to use those methods and we are not responsible to implement those methods.

Collection: If we want represent a group of individual objects as a single entity then we should go for Collection.

Collections Framework: It contains several classes and interfaces which can be used to represent a group of individual objects as a single entity.

Differences between Arrays and Collections

Arrays Collections
Arrays are fixed in size i.e., once we create an array, we can’t increase or decrease the size based on our requirement. Collections are Growable in nature i.e., based on our requirement we can increase or decrease the size.
With respect to memory Arrays are not recommended to use. With respect to memory Collections are recommended to use.
With respect to performance Arrays are recommended to use. With respect to performance Collections are not recommended to use.
Arrays can hold only homogenous data type elements. Collections can hold both homogenous and heterogenous data type elements.
There is no underlined data structure for Arrays and hence readymade support is not available for every requirement we have to write the code explicitly which increases complexity of programming. Every Collection class is implemented based on some standard data structure and hence for every requirement readymade method support is available. Being a programmer, we can use these methods directly and we are not responsible to implement those methods
Arrays can hold both primitives and objects. Collections can hold Objects but not primitives.

9 key interfaces of Collection Framework:

  1. Collection
  2. List
  3. Set
  4. SortedSet
  5. NavigableSet
  6. Queue
  7. Map
  8. SortedMap
  9. NavigableMap

1. Collection(I):

1. If we want to represent a group of individual objects as a single entity then we should go for Collection.
2. Collection interface defines the most common methods which are applicable for any Collection object.
3. In general, Collection interface is considered as root interface of Collection Framework

CollectionDiagram

There is no concrete class which implements Collection interface directly.

Difference between Collection and Collections:

1. Collection is an interface. if we want to represent a group of individual objects as a single entity then we should go for Collection.
2. Collections is an utility class present in java.util package to define several utility methods for Collection Object like sorting, searching etc.,

2. List(I):

1. It is the child interface of Collection.
2. If we want to represent a group of individual objects as a single entity where duplicate is allowed and insertion order must be preserved then we should go for List.

ListDiagram

Note: In 1.2V Vector and Stack class are reengineered to implement List interface.

3. Set(I):

1. It is the child interface of Collection.
2. If we want to represent a group of individual objects as a single entity where duplicates are not involved an insertion order not required then we should go for Set interface.

SetDiagram

4. SortedSet(I):

1. It is the child interface of Set.
2. If we want to represent a group of individual objects as a single entity where duplicates are not allowed and all objects should be inserted according to some sorting order then we should go for SortedSet.

SortedSetDigram

5. NavigableSet(I):

It is the child interface of SortedSet. It contains several methods for Navigation purposes.

NavigableSetDiagram

6. Queue(I):

1. It is the child interface of Collection. If we want represent a group of individual objects prior to processing then we should for Queue.
2. Usually, Queue follows First In First Order. But based on our requirement we can implement our own priority order also.
For Example Before sending a mail all mail ids we have to store in some data structure in which order we ordered mail ids in the same order only mail should be delivered. For this requirement Queue is best choice.

QueueDiagram

Note: All the above interfaces Collection, List, Set SortedSet, NavigableSet, Queue meant for representing a group of individual objects.
If we want to represent a group of objects as key value pairs then we should go for Map.

7. Map(I):

1. Map is not child interface of Collection. if we want represent a group of objects as key value pairs then we should go for Map.

Key Value
101 Kalyan
102 Chanukya
103 Hemanth

2. Both key and value are objects only.
3. Duplicate keys are not allowed but values can be duplicated.

MapDiagram

8. SortedMap(I):

1. It is the child interface of Map.
2. If we want to represent a group of key value pairs according to some sorting order of keys then we should go for SortedMap.
3. In SortedMap the sorting should be based on key but not based on value.

SortedMapDiagram

9. NavigableMap(I):

1. It is the child interface of SortedMap.
2. It defines several methods for Navigation purposes.

NavigableMap

Note: The following legacy collections present in collection framework are

  • Enumeration (Interface)
  • Dictionary (Abstract Class)
  • Vector (Class)
  • Stack (Class)
  • Hashtable (Class)
  • Properties (Class)

Collection(I):

If we want to represent a group of individual objects as a single entity then we should go for Collection.
Collection interface defines the most common methods which are applicable for any Collection Object.

boolean add(Object o)
boolean addAll(Collection c)
boolean remove(Object o)
boolean removeAll(Collection c)
void clear()
boolean retainAll(Collection c) to remove all objects except those present in c
boolean contains(Object o)
boolean containsAll(Collection c)
boolean isEmpty()
int size()
Object[] toArray()
Iterator iterator()

Note: There is no concrete class which implements Collection interface directly.