Collection Framework
Need to collection
If we need to store one value one variable is required 2 values two variables are required but if we need 10000 values to stored 10000 variables are required.
To overcome this problem Arrays are available.
Eg:-
But there are limitations of arrays ie:-
- Arrays are fixed in size.
Like called for 100 people for batch having 100 chairs but only 2 people arrive. So rest space and chair wasted,
Call 2 people for batch but 100 people arrive that is failure
- Arrays can hold only homogeneous data
But we can solve this problem by using Object type of array :-
Object[] a =new Object[10000]
a[0]=new Student();
a[1]=new Customer();
- Underlying DS eg:- to check the element exist on the array it doesn’t have any predefined method
Overcome these problem we have Collections
Collections
To overcome above problems we have collections
- Collections are growable in nature
- Store homogeneous and heterogeneous
- Standard Data Structure
Difference between Arrays and Collections
Collection Framework
Collection
Collection Framework
If we want to represent a group of individual objects as a single entity several classes and interfaces are required. A group of classes and interfaces are classes framework
Collections are also available in other languages also with different names like below :-
9 key interfaces of Collection Framework(9 interfaces)
- Collections (I) –
Most common methods like add an object, delete,insert etc. which are required available in this interfaces
Difference between Collection and Collections
- List
- Set(I)
Difference between List and Set
- Sorted Set (I)
- Navigable Set
- Queue(I)
- Map
- Sorted Map
- Navigable Map
Collection Overview
Collection Interfaces Details
- Collections
- List (I)
ArrayList(C)
LinkedList (C )
Difference between ArrayList and Linked List
- Vector ( I )
Stack ( C )
Three Cursors of Java
– Enumeration
-Iterator
-ListIterator
Implementation Classes of the three Cursors of Java
- Set
HashSet ( C )
Linked HashSet ( C )
SortedSet (C )
TreeSet ( C )
Comparable Interface
Map ( I )
- Map is not child interface of collections
- If we want to represent a group of objects as key-value pairs then we should go for Map
- Both keys and values are objects only
- Duplicate keys are not allowed but value can be duplicated
- Each key value pair is called entry . Hence map is considered as a collection of entry objects.
- Map does not follow the collection rules and methods
Methods
-Object put(Object key,Object value)
- To add one key value pair to the map.
- If the key is already present then old value will be replaced with new value and returns old value eg:-
In this example first durge will insert for key 101 but when try to insert some value with same key then durga will replace with Ravi
Set KeySet()
Collection values()
Set entrySet()
These 3 methods are called collection views of Map
Entry
- A map is a group of key value pairs and each key value pair is called an entry. Hence is considered as a collection of entry objects.
- Without existing map object there is no chance of existing entry object.Hence Entry interface define inside map interface.
Above 3 methods are entry specific methods and we can apply only on entry object.
- The underlying data structure is hash table.
- Insertion order is not preserved and it is based on hashcode of keys
- Duplicate keys are not allowed
- Duplicate values are allowed
- Heterogeneous objects are allowed for both key and values
- Null is allowed for key (only once)
- Null is allowed for values(any number of times)
- HashMap implements Serializable and cloneable interfaces but not random access
- Hashmap is best choice if our frequent operation is Search
HashMap m=new HashMap() – Create an empty hashmap object with default initial capacity 16 and default filled ratio 0.75.
HashMap m=new HashMap(int initialCapacity)-create empty hashmap with specified initial capacity and default filled ratio 0.75
HashMap m=new HashMap(int initialCapacity,float filledRatio)
HashMap m=new HashMap(Map m)
Differences between HashMap and HashTable
HashMap | HashTable |
Every method present in hashmap is Not synchronized | Synchronized |
Not thread safe | Thread safe |
Relatively performance is high because threads are not required to wait to operate on hashmap object | Performance is low |
Null is allowed as key and value | Null is not allowed for keys and values otherwise we will get null pointer exception |
Introduce in 1.2 v and it is not legacy | Introduced in 1.0 and it is legacy |
By default hashMap is non synchronized but we can get synchronized version of hashmap by using synchronizedMap() of collections class
Sorted Map
- Sorting is based on keys but not based on values
- SortedMap defines the following specific methods
Navigable Map