Hashtable and NavigableSet in java

Hashtable

Constructors

1.
Hashtable h = new Hashtable();
2.
Hashtable h = new Hashtable(int initialCapacity);
3.
Hashtable h = new Hashtable(int initialCapacity, float fillRatio);
4.
Hashtable h = new Hashtable(Map map);

Working of HashCodes behind the scene

import java.util.Hashtable;
public class HashtableDemo {
    public static void main(String[] args){
        Hashtable hashtable = new Hashtable();
        hashtable.put(new TempClass1(5), "A");
        hashtable.put(new TempClass1(2), "B");
        hashtable.put(new TempClass1(6), "C");
        hashtable.put(new TempClass1(15), "D");
        hashtable.put(new TempClass1(23), "E");
        hashtable.put(new TempClass1(16), "F");
        System.out.println(hashtable); // {6=C, 16=F, 5=A, 15=D, 2=B, 23=E}
    }
}
class TempClass1{
    int tempHashCode;
    public TempClass1(int tempHashCode){
        this.tempHashCode = tempHashCode;
    }
    public int hashCode(){
        return tempHashCode;
    }
    public String toString(){
        return tempHashCode + "";
    }
}
HashCode working under the hood
HashCode working under the hood

NOTE: The number of buckets depends on the capacity of the HashTable class.

Properties

If any information changes frequently in the application like username, password, mail-id, etc., they are not recommended to be hard-coded in java file because if in case of any minor change also, to reflect that change; recompilation, rebuild and redeployment of the application is required. Even sometimes, server restart is also needed which creates a huge business impact to the client.

We can overcome this problem by using properties file. Such type of variable things can be configured in the properties file. From that properties file, we can read the variables in the java program. The main advantage of this approach is if there is a change in properties file, redeployment is enough to reflect the change in the application without causing much business damage to the client.

We can use java Properties object to hold properties which are coming from properties file. In normal Map(like HashMap, Hashtable, etc.) object, key and value can be any type but in the case of Properties, key and value should be String type only.

Constructor
Properties properties = new Properties();
Methods in Properties class
String getProperty(String propertyName); // Gets the property value corresponding to the propertyName.
String setProperty(String propertyName, String propertyValue);// If key is already present, replace the old value with new one and return the old propertyValue.
Enumeration propertyNames(); // Gets the property names.
void load(InputStream is); // to load all the properties from properties file into java properties object.
void store(OutputStream os, String comment); // to store properties from java properties object into properties file.

Implementation demo

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertiesDemo {
    public static void main(String[] args) throws Exception{
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream("your_path/info.properties");
        properties.load(fis);
        System.out.println(properties);
        String string = properties.getProperty("SomeOtherInformation");
        System.out.println(string);
        properties.setProperty("DBPass", "SomePass");
        FileOutputStream fileOutputStream = new FileOutputStream("your_path/info.properties");
        properties.store(fileOutputStream, "Updated info.properties and performed housekeeping");
    }
}

NOTE: For maintaining db connections and various other enterprise applications, we use Properties class.