Map(I) interface:
- Map is not child interface of Collection.
- If we want to represent a group of objects as key-value pairs then we should go for Map.
Key Value 101 Kalyan 102 Chanukya 103 Hemanth - Both keys and values are objects only.
- Duplicate keys are not allowed but values can be duplicated
- Each key-value pair is called entry.
- Hence Map is considered as a Collection of entry objects.
Map interface methods:
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.
For Example>
m.put(101, "kalyan"); ---> (returns)null
m.put(102, "chanukya"); ---> (returns)null
m.put(101, "hemanth"); ---> (returns)kalyan
Collection views of Map:
Entry(I) interface:
- A Map is a group of key-value pairs and each key-value pair is called an Entry. Hence Map is considering a collection of Entry objects.
- Without existing Map object there is no chance of existing Entry Object.
- Hence Entry interface is defined inside Map interface.
- Entry specific methods and we can apply only on Entry object.
HashMap:
- HashMap is a map-based collection which is used to store entries, and it is a implementation class of Map interface and Present since JDK 1.2.
- HashMap is hash based, Hence the internal data structure is Hashtable.
- HashMap can store heterogeneous data for both key and value and does not maintain any insertion order and it is based on hashcode of keys.
- Duplicate keys are not allowed and values can be duplicated.
- Null is allowed for key (only one) and null is allowed for values (any no.of times).
- HashMap implements Serializable and Cloneable interfaces but not RandomAccess.
- HashMap is the best choice if our frequent operation is search operation.
- Initial capacity of HashMap is 16.
- Fill ratio or load factor is 75%
It has 4 overloaded constructors.
Output:
{RC=500, Prabhas=800, PK=900, NTR=300}
900
[RC, Prabhas, PK, NTR]
[500, 800, 1000, 300]
[RC=500, Prabhas=800, PK=1000, NTR=300]
RC ===== 500
Prabhas ===== 800
PK ===== 1000
NTR ===== 300
{RC=500, Prabhas=800, PK=10000, NTR=300}
How to get synchronized version of HashMap object?
By default, HashMap is non synchronized but we can get synchronized version of HashMap using synchronizedMap method of Collections class.
here, m1 is synchronized and hm is non synchronized.
LinkedHashMap:
- LinkedHashMap is a map-based collection which is used to store entries, and it is an implementation class of Map interface and it is present since JDK 1.4.
- LinkedHashMap is hash based, Hence the internal data structure is Hashtable and LinkedList.
- Initial capacity of LinkedHashMap is 16.
- Fill ratio or load factor is 75%
- LinkedHashMap maintains insertion order.
- LinkedHashMap can store single null key.
- LinkedHashMap can store heterogeneous data.
It has 4 overloaded constructors.
Output:
{PK=900, Prabhas=800, NTR=300, RC=500}
900
[PK, Prabhas, NTR, RC]
[1000, 800, 300, 500]
[PK=1000, Prabhas=800, NTR=300, RC=500]
PK ===== 1000
Prabhas ===== 800
NTR ===== 300
RC ===== 500
{PK=10000, Prabhas=800, NTR=300, RC=500}
here, the insertion order is preserved.
Note: LinkedHashSet and LinkedHashMap are commonly used for developing cache-based applications.
Differences between == operator and .equals() method:
In general == operator meant for reference comparison (address comparison) whereas.equals() method meant for content comparison.
For Example
IdentityHashMap:
- It is exactly same as HashMap including methods and constructors except the following difference.
- In the case of normal HashMap JVM will use, equals () method to identify duplicate keys, which is meant for content comparison.
- But int the case of IdentityHashMap JVM will use == operator to identify duplicate keys which is meant for reference comparison (address comparison)
I1 and I2 are duplicate keys because I1.equals(I2) returns true.
If we replace HashMap with IdentityHashMap then I1 and I2 are not duplicate keys because I1 == I2 returns true.
In this case output is {10 = pawan, 10 = kalyan}
WeakHashMap:
It is exactly same as HashMap except the following difference
- In the case pf HashMap even though the objects have doesn’t has any reference it is not eligible for garbage collection, if it is associated with HashMap i.e., HashMap dominates garbage collector.
- But in the case of WeakHashMap, if object doesn’t contain any references, it is eligible for Garbage collection even though object associated with WeakHashMap i.e., garbage collector dominates WeakHashMap.
Output:
{temp=kalyan}
{temp=kalyan}
In the above example Temp object is not eligible for garbage collection because it is associated with HashMap.
In this case Output is
{temp = "kalyan"}
{temp = "kalyan"}
Output:
{temp=kalyan}
finalize() method called
{}
In the above program we replace HashMap with WeakHashMap then Temp object eligible for garbage collection.
In this case Output is
{temp = "kalyan"}
finalize() method called
{}
SortedMap(I) interface:
- It is the child interface of Map.
- If we want represent a group of objects as group of key-value pairs according to some sorting order of keys then we should go for SortedMap.
- Sorting is based on the key but not based on value.
SortedMap defines the following specific methods
{101 = A, 102 = B, 103 = C, 107 =D, 125 = E, 136 = F}
TreeMap:
- It is one of the implementation class of Map interface. and present since JDK 1.2.
- TreeMap is used mainly for sorting data based on key.
- TreeMap implements default natural sorting order on the key using Comparable interface.
- For custom sorting we use Comparator.
- It cannot store even a single null key.
- TreeMap is homogeneous.
- NullPointerException, when we try to add null
- The underlined data structure is RED-BLACK Tree.
- Insertion order is not preserved and it is based on some sorting order of keys.
- Duplicate keys are not allowed but values can be duplicated.
- If we are depending on default natural sorting order then keys should be homogeneous and Comparable otherwise, we will get runtime exception saying ClassCastException.
- If we are defining our own sorting by Comparator then keys need not be homogenous and Comparable. We can take heterogenous non-Comparable objects also whether we are depending natural sorting order or customized sorting order there are nor restrictions for values. We can take heterogeneous non comparable objects also.
Null acceptance:
- For non-empty TreeMap if we are trying to insert an entry with null key then we will get runtime exception saying NullPointerException.
- For empty TreeMap as the first entry with the null key is allowed but after inserting that entry if we are trying to insert any entry then we will get runtime exception saying NullPointerException.
Note: The above null acceptance rule applicable until 1.6V only. From 1.7V null is not allowed for key. But for values we can use null any no.of times there is no restriction.
Constructors:
Output:
{100=kalyan, 101=hemanth, 103=chanukya, 104=siva}
Output:
{siva=40, kalyan=10, hemanth=30, chanukya=20}
Hashtable:
- Hashtable is one of the implementation class of Map interface and present since JDK 1.0 [legacy class].
- It is also a hash-based collection, hence data structure used is Hashtable.
- Insertion is not preserved and it is based on hashcode of keys.
- Duplicate keys are not allowed and values can be allowed.
- Heterogeneous objects are allowed for both keys and values.
- Hashtable is Single threaded. i.e.., methods are synchronized and Hashtable objects is Thread safe.
- Hashtable cannot store even a single null key and null value otherwise we will get runtime exception saying NullPointerException.
- It implements Serializable, Cloneable interfaces but not RandomAccess interface.
- Hashtable is best choice if our frequent operation is search operation.
- Initial capacity is 11.
- Load factor or fill ratio is 75%.
- Hashtable is slower than HashMap. Because it is Single threaded.
There are 4 overloaded constructor presents in Hashtable
Output:
{6=C, 16=F, 5=A, 15=D, 2=B, 23=E}
if we change hashcode method of Temp class then
In this case output is
{16=F, 15=D, 6=C, 23=E, 5=A, 2=B}
If we configure initial capacity as 25 then
In this case the output is
{23=E, 16=F, 15=D, 6=C, 5=A, 2=B}
Properties:
In our program if anything which changes frequently (like username, password, mobile number, mail id etc) are not recommended to hard-code in java program because if there is any change, to reflect that change recompilation, rebuild and redeploy application are required. Even sometimes server restart also required which creates a big business impact to the client.
we can overcome this problem by using properties file such type of variable things we have to configure in the properties file.
From the properties file we have to read into the java program and we can use those properties.
The main advantage of this approach is if there is a change in properties file to reflect that change just redeployment is enough which won’t create business impact to the client.
we can use java Properties object to hold properties which are coming from properties file.
In normal Map (like HashMap, Hashtable, TreeMap) Key and value can be any type but in the case of Properties Key and value should be String type.
Constructor:
Properties specific Methods:
to set a new Property if the specified property aleady available then old value will be replaced with new value and returns old value
to get value associated with the specified Property if the specified property not available then this method returns null.
returns all property names present in Properties object.
to load properties from properties file into java properties object
to store properties from java properties object into properties file.
abc.properties
username = scott
password = tiger
Output:
{username = scott, password = tiger}
tiger
abc.propertiesv
#Updated by Kadali Sai Kalyan
username = scott
password = tiger
email = kalyan@gmail.com
As a part of 1.6 version the following two concepts introduced in Collection framework
- NavigableSet
- NavigableMap
NavigableSet:
It is the child interface of SortedSet and it defines several methods for Navigation purposes.
NavigableSet specific methods:
Output:
[1000, 2000, 3000, 4000, 5000]
2000
3000
3000
2000
1000
5000
[4000, 3000, 2000]
[2000, 3000, 4000]
NavigableMap:
NavigableMap is the child interface of SortedMap.
It defines several methods for Navigation purposes.
NavigableMap is the child interface of SortedMap.
it defines several methods for Navigation purposes.
NavigableMap specific methods :
0 Comments