Tuesday, 22 August 2023

Map Interface & Child classes of Map Interface(Part-2)

 Map Interface



TreeMap
  • The underlying data structure is RED-BLACK tree.
  • Insertion order is not preserved & 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 than keys should be Homogeneous & comparable otherwise, we will get Run time exception saying class cache exception.
  • If we are defining our own sorting by Comparator than, keys need not be Homogeneous & comparable we can take Heterogeneous non comparable.
  • For Non-Empty TreeMap if we are trying to insert an entry with Nullkey than, we will get Run time Exception.
  • For Empty TreeMap as the 1st entry with Null key is allowed but, after inserting that entry if we are trying to insert any other entry than we will get Run time Exception.
Note
  • The above Null Exception rule is applicable until(1.6v) only from (1.7v) onwards Null is not allowed for key.
  • But, for values we can use null any number of times there is no restriction whether, it is (1.6v)\(1.7v). 

Constructor
  • TreeMap t = new TreeMap();
    For Default Natural Sorting of keys.
  • TreeMap t = new TreeMap(comparator c);
    For Customized Sorting.
  • TreeMap t = new TreeMap(m);
  • TreeMap t = new TreeMap(sortedMap m);
For eg:

output:



  • If we trying anything with null.
for eg:

output:
  • when we hit Heterogeneous key.
  • output is ClassCasteException.
for eg:



output:


Hashtable
  • The underlying data structure for hashtable is Hashtable.
  • Insertion order is not preserved & it is based on HashCode keys.
  • Duplicate keys are not allowed & the values can be duplicated.
  • Heterogeneous objects are allowed for both key & value, otherwise we will get Run Time Exception saying Null pointer Exception.
  • It implements Serializable & cloneable interface but not Random Access.
  • Every method present in Hashtable is Synchronized & hence, Hashtable object is thread safe.
  • Hashtable is the best choice if our frequent operation is Search operation.
Constructor
  • Hashtable h = new Hashtable();
     create an empty Hashtable object with default initial capacity       11 and default fill ratio is 0.75.
  • Hashtable h = new Hashtable(int intialCapacity);
  • Hashtable h = new Hashtable(int intialCapacity ,floatFillRatio);
  • Hashtable h = new Hashtable(map m);
for eg:

output:

  • If we trying anything with null.
for eg:

output :


  • If we configure initial capacity as 25. output is different.
for eg:


output:




Properties
  • In our program if anything which changes frequently[for eg: User name, password, mailid, mobile no etc.] are not recommended to hard core in java program.
  • Because, if this is any change to reflect that change Recompilation, Rebuild, Redeploy application are required. Even sometime server restart also required, which creates a big business impact to the client.
How can overcome of this problem? 
  • we can overcome this problem by using properties file. such type of variable things we have to configure in the properties file from that properties file we have to read into java program & we can use those properties.
  • The main advantage of this approach is:
  1. If there is a change in properties file to reflect that change just a redeployment is enough which won't create any business impact to the client. 
  2. We can use java properties to hold properties which are coming from properties file.
  3. In normal map [for eg: Hashtable, HashMap ,TreeMap etc.] key & value can be any type but in the case of properties key & value should be String Type.
Constructor
  • Properties p = new Properties();
Method of Properties
  • String setProperty(String pname, String pvalue)
          To Set a new property. If the specified property already             available than the old value will be replaced with new value         and return old value.
  • String getProperty(String pname)
       To get value associated with the specified property. If               specified properties than this method return Null.
  • Enumeration propertyNames()
Another method
  • void load(InputStream is)
      To load properties from properties file into java properties          object.
  • void store(OutputStream os , String comment)
      To store properties from java properties object into 
      properties file.


for eg:


  • This is properties file abc.properties.
  • without having properties file you can't expect any single java project.
output:


  • Now let see what happened in properties file.


  •  If we change in file.
for eg:


  • Some changes doing in password old password = Mohan new password = Mohan@123456*
  • Automatically changed in output after execution.
output:



  • After execution password has been changed.
                  


Thanks
Matri Sharma

No comments:

Post a Comment

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...