Recent Posts

Tuesday, 2 August 2016

Collection Framework Tutorial Part 5


7. Hashtable
* The underlying data structure is Hashtable
* Insertion order is not preserved and it is based on hash code of the keys.
* Heterogeneous objects are allowed for both keys and values.
* null key (or) null value is not allowed otherwise we will get NullPointerException.
* Duplicate keys are allowed but values can be duplicated.
* Every method present inside Hashtable is syncronized and hence Hashtable objet is Thread-safe.

Constructors
1. Hashtable h=new Hashtable();
     Creates an empty Hashtable object with default initialcapacity 11 and default fill ratio 0.75.

2. Hashtable h=new Hashtable(int initialcapacity);
   
3. Hashtable h=new Hashtable(int initialcapacity,float fillratio);

4. Hashtable h=new Hashtable (Map m);
E.g
package com.ashok.collections;

import java.util.Hashtable;

public class MyHashTable {
   public static void main(String[] args) {
      Hashtable hashtable = new Hashtable();

      hashtable.put("Key1", "Ashok");
      hashtable.put("Key2", "Vinod");
      hashtable.put("Key3", "Dillesh");
      hashtable.put("Key4", "Naresh");
      hashtable.put("Key5", "Thiru");

      System.out.println(hashtable);
   }
}
9. Properties
* Properties class is the child class of Hashtable.

* In our program if anything which changes frequently like DBUserName, Password etc., such type of values not recommended to hardcode in java application because for every change we have to recompile, rebuild and redeployed the application and even server restart also required sometimes it creates a big business impact to the client.

* Such type of variable things we have to hardcode in property files and we have to read the values from the property files into java application.

* The main advantage in this approach is if there is any change in property files automatically those changes will be available to java application just redeployment is enough.

* By using Properties object we can read and hold properties from property files into java application.

Constructors
Properties p=new Properties();
     In properties both key and value "should be String type only".

Methods
1. String getPrperty(String propertyname) ;
     Returns the value associated with specified property.

2. String setproperty(String propertyname,String propertyvalue);
     To set a new property.

3. Enumeration propertyNames();

4. void load(InputStream is);//Any InputStream we can pass.
     To load Properties from property files into java Properties object.
 
5. void store(OutputStream os,String comment);//Any OutputStream we can pass.
    To store the properties from Properties object into properties file.
E.g
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

class MyProperties {
   public static void main(String[] args) throws Exception {
      Properties prop = new Properties();
      FileInputStream fis = new FileInputStream("db.properties");
      prop.load(fis);
      String url = prop.getProperty("url");
      String username = prop.getProperty("username");
      String password = prop.getProperty("password");
      Connection con = DriverManager.getConnection(url, username, password);
      ---------------------------------------------------------
      --------------------------------------------------------
      FileOutputStream fos = new FileOutputStream("db.properties");
      prop.store(fos, "Updated by Ashok");
   }
}

Next Tutorial  Generics Tutorial

Previous Tutorial  Collection Framework Tutorial Part 4

No comments:

Post a Comment