01. Collections
An array is an indexed Collection of a fixed number of homogeneous data elements. The Main advantage of Arrays is we can represent multiple values with a single variable. So that reusability of the code will be improved.
Arrays are fixed in size i.e. once create an array with some size there is no chance of increasing or decreasing its size based on our requirement. Hence to use arrays compulsory we should know the size in advance which may not always be possible always.
Arrays can hold only homogeneous data elements.
Student [] s = new Student [10000];
s[0] = new Student; (correct)
s[1] = new Customer(); (wrong)
But We can resolve this problem by using object Arrays.
Object [] o = new Object [10000];
o[0] = new Student();
o[1] = new Customer();
Arrays Concept is not implemented based on some standard data structure hence readymade method support is not available for every requirement we have to write the code explicitly. Which is the complexity of programming.
To Overcome the above limitations of Arrays we should go for Collections.
Collections are growable in nature. I.e Based on our requirement we can increase or decrease the size.
Collection can hold both homogeneous and Heterogeneous elements.
Every Collection class is implemented based on some standard data structure. Hence readymade method support is available for every requirement. Being a programmer we have to use this method and we are not responsible to provide implementation
02. Collection Framework
Collection — Interface :
- If want to represent a group of individual objects as a single entity then should go for Collection.
- Collection interface defines the most common methods which are applicable for any Collection object.
- In general the collection interface is considered as the root interface of Collection Framework.
There is no concrete class which implements the collection interface directly.
List — Interface
- List is the child interface of collection.
- If Want to represent a group of individual object as a single entry where duplicate are allowed and insertion order preserved then should go for list
- Differentiate duplicate by using index
- Preserve insertion order by using index, hence index play very important role in list interface.
Vector and Stack classes are re-engineer in 1.2 to implement list interface.
Set — Interface
- It is the child interface of Collection.
- If want to represent a group of individual objects as a single entity where duplicates are not allowed and insertion order not preserved then should go for Set.
- The interface doesn’t contain any new methods. So we have to use only Collection interface methods.
Queue — Interface
- It is child interface of Collection.
- If we want to represent a group of individual objects prior to processing then we should go for Queue.
- For this First in First out requirement Queue is best coise.
* Usually Queue follows First in First out order but based on our requirement we can implement our own Priority order also. (PriorityQueue).
Map — Interface
- Map is not the child interface of Collection.
- If we want to represent a group of individual objects as key value pairs then should go for Map.
- Duplicated keys are not allowed but values can be duplicated
- Both Key and Value are Object Only
- Each key value pair is call Entry
03. List
ArrayList
- The underlined data structure Resizable Array or Growable Array
- Duplicates are allowed.
- Insertion order is preserved.
- Heterogeneous objects are allowed [except TreeSet & TreeMap everywhere heterogeneous objects are allowed].
- Null insertion is possible.
LinkedList
- The underlying data structure is Double Linked List.
- Insertion order is preserved
- Duplicates are allowed.
- Heterogeneous Objects are allowed.
- Null insertion is possible.
- LinkedList implements Serializable and Cloneable interfaces but not RandomAccess interface.
- LinkedList is the best choice if our frequent operation is insertion or deletion in the middle.
- LinkedList is the worst choice if our frequent operation is retrieval operation.
Vector
- The underlying Data structure for the vector is resizable array or growable array.
- Duplicate objects are allowed.
- Insertion order is preserved.
- ‘null’ insertion is possible.
- Heterogeneous objects are allowed.
- Vector class implemented Serializable, Closeable and RandomAccess Interfaces.
- Most of the methods present in Vector are synchronized. Hence the Vector object is Thread-safe.
- Best choice if the frequent operation is retrieval.
Stack
- It is a child class of Vector.
- It is Specially designed class for Last in First Out order (LIFO)
04. Set
SortedSet
- It is the child interface of Set.
- If want to represent a group of individual objects as a single entity where duplicates are not allowed but all objects should be inserted according to some sorting orde then should go for SortedSet.
NavigableSet
- It is the child interface of SortedSet that defines several methods for navigation purposes.
SortedSet
- It is the child interface of Set
- If want to represent a group of individual objects according to some sorting order and duplicates are not allowed then should go SortedSet.
- Default natural sorting order for numbers Ascending order and for String alphabetical order.
- Can Apply the above methods only SortedSet implemented class object. That is on the TreeSet object.
TreeSet
- The underlying data structure for TreeSet is Balanced Tree.
- Duplicate objects are not allowed.
- Insertion order not preserved, but all objects will be inserted according to some sorting order.
- Heterogeneous objects are not allowed. If we are trying to insert heterogeneous objects then we will get a runtime exception saying ClassCastException.
- For empty TreeSet as the first element null insertion is possible. But After inserting that null if we are trying to insert any other element will get NullPointerException.
- For Non empty TreeSet if trying to insert Null then will get NullPointerException.
HashSet
- The underlying data structure is Hashtable.
- Duplicates are not allowed. If trying to insert duplicates, won’t get any compile time or runtime errors. add() method simply returns false.
- Insertion order is not preserved and all objects will be inserted based on the hash-code of objects. * Heterogeneous objects are allowed.
- null ‘ insertion is possible.
- implements Serializable and Cloneable interfaces but not RandomAccess.
- HashSet is the best choice, if our frequent operation is Search operation.
LinkedHashSet
- It is the child class of Hashset
- The Underlying data structure is Hash Table + Linked List
- Insertion Order is Preserved
- Best choice to develop cache based applications, where duplicates are not allowed and Insertion order must be preserved.
05. Queue
PriorityQueue
- If we want to represent a group of individual objects prior to processing according to some priority then we should go PriorityQueue.
- The Priority can be default natural sorting order or customized sorting order defined by comparator.
- Insertion order is not preserved under it based on some Priority.
- Duplicate objects are not allowed.
- If we are depending on default natural order compulsory object should be Homogeneous or comparable otherwise we will get a RuntimeException saying ClassCastException.
- If we are defining our own sorting by comparator then objects need not be Homogeneous under comparable.
- Null is not allowed.
06. Map
SortedMap
- It it the child interface of map
- If want to represent a group of key value pairs according to some sorting order of keys then should go for SortedMap
NavigableMap
- It is the child interface of SortedMap.
- It defines several utility methods for navigation purposes.
HashMap
- Underlying data structure is HashTable
- Insertion order is not preserved. Under it is base base on Hash Code is Key
- Duplicate Keys are not allowed , but Values can be duplicated.
- Heterogeneous objects are allowed , for both key and values.
- Null allowed for key , Only one time
- Null allowed values, any number of times
- Hashmap implements Serilizeble , Cloneable interface but not RandomAccess
- Empty Hashmap Default Initial Capacity 16.
- The default load factor of HashMap is 0.75
- Every Method presenting HashMap is Not a Synchronizer.
- Multiple threads are allowed to operate on Hashmap objects. It is Not thread safe.
LinkedHashMap
- It is the Child class of HashMap
- It is exactly same as HashMap including Method and Construction.
- Underlying data structure is LinkedList and HashTable.
- Insertion order is preserved.
IdentityHashMap
It is exactly the same as the HashMap Including method and Constructor. Except following deference.
In the case of normal Hashmap JVM will use the .equal method used to identify duplicate keys. But in the case of IdentityHashMap JVM will be used == operator to Identify duplicate keys( Reference Comparisons).
WeekHashMap
The exact same HashMap is accepted following the difference.
In the case of HashMap even though objects don’t have any reference it is eligible for garbage collector it is associated with HasMap.That is HashMap dominant Garbage Collector.
But in the case of WeekHashMap if Object doesn’t contain any references it is eligible Garbage Collector evon those objects associated with WeekHashMap that is Garbage Collector dominate WeekHashMap.
SortedHashMap
- It is child interface of Map
- If want to represent a group Key,Value pairs according to some sorting of keys then we should go SorterdMap.
- Sorting is based on the Key but not based on Values.
TreeMap
- Underlying data structure is RedBlackTree
- Insertion Order is not preserved, under it is based on some sorting order of keys.
- Duplicate objects are not allowed, but values can be duplicated.
- Depending on Default Natural sorting order. The n key should be Homogeneous under Comparable otherwise we will get a runtime exception saying ClassCastException.
- If we are defining our own sorting by comparator then keys need not be Homogeneous under Comparable, we can take Heterogeneous non comparable objects also.
- Depending on default natural sorting order or customized sorting order there are restrictions for values. we can take Heterogeneous non comparable objects also.
- New Empty TreeMap if we are trying to insert an Entry with a null key then we will get a RuntimeException , saying NullPointerException.
- For Empty TreeMap add the first entry with the null key is allowed. But after inserting that entry if we are trying to insert any other entry then we will get a RuntimeException saingNullpointException.
- Null is Not Allowed for Key, But for Values we can use any no of times.
NavigableMap
The NavigableMap child interface of SortedMap defines several methods for navigation purposes.
HashTable
- Underlying data structure is HashTable
- Insertion order is not preserved. Under it is based on Hash Code on Keys.
- Duplicate keys are not allowed. Values can be duplicated..
- Heterogeneous objects are allowed. Both keys and values.
- Null is not allowed for both key and values. Otherwise we will get a RuntimeException saying NullPointException.
- It implements Serializeble and Cloneable interfaces. But not RandomAccess.
- Every method presenting Hashtable is Synchronized. Under HashTable is Thread Safe.
Properties
- In our program if anything which changes frequently like UserName Password Email, MobileNo , etc is not recommended to hard code in the program because if there is any change to reflect that change recompilation, rebuild, on the redeploy application are required, even some time server restart also required.
- Can overcome this problem with the Properties file. Search type of variable thinks we have to configure in the property files. From the property file we have to read into the java program under which we can access those properties. The main advantage of this approach is effort. There is a change in the property file to reflect that change just redeployment is enough.
- Can use Java Property Objects which are coming from Property Files.
- In Normal Map Like HashMap, HashTable TreeMap Key Value can be any Object Type , but in the case of Properties key and Value should be String type.