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.
- 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();
- TreeMap t = new TreeMap(comparator c);
- TreeMap t = new TreeMap(m);
- TreeMap t = new TreeMap(sortedMap m);
- 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.
- Hashtable h = new Hashtable();
- Hashtable h = new Hashtable(int intialCapacity);
- Hashtable h = new Hashtable(int intialCapacity ,floatFillRatio);
- Hashtable h = new Hashtable(map m);
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.
- 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:
- 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.
- We can use java properties to hold properties which are coming from properties file.
- 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)
- String getProperty(String pname)
- Enumeration propertyNames()
- void load(InputStream is)
- void store(OutputStream os , String comment)
properties file.
for eg:
- This is properties file abc.properties.
- without having properties file you can't expect any single java project.
No comments:
Post a Comment