SortedMap-TreeMap-Properties
SortedMap:
It is interface.If you want to store the elements based on some sorting order of keys we should go for the SortedMap.A SortedMap is a Map that maintains its mappings in ascending key order. It is the Map analogue of SortedSet. The SortedMap interface is used for apps like dictionaries and telephone directories.
SortedMap Methods:
1) Object firstKey() //Returns the firstkey of the sortedset
2) Object lasKey() // Returns the last key of the sortedset
3) SortedMap headMap( Object key) //Returns the SortedMap whose keys are smaller than or equal to specified key
4) SortedMap tailMap( Object key) // Returns the SortedMap whose keys are greater than or equal to specified key
5) SortedMap subMap( Object key1,Object key2) // Returns a SortedMap whose keys are greater than or equal to the key1 but less than key2
6) Comparator comparator(): //Returns Comparator Object that defines the underlying sorting technique.If the natural sorting order is used then returns null
TreeMap:
- It implements the SortedMap interface.
- The underlying datastructure for the TreeMap is Red-Black Tree
- Insertion order is not preserved
- Duplicate keys are not allowed but values may be duplicated
- Heterogeneous objects are not allowed for the keys but values may be heterogeneous.
- As first entry null key insertion is possible to the empty treemap but after if you try to add any other entry we will get the NullPointerException
- If you are trying to add an entry with the null key to an already exsting treemap we will get the NullPointerException
- There are no restriction for null values
- If natural sorting order is used then the keys must be comparable otherwise classCastException
TreeMap map=new TreeMap():
TreeMap map=new TreeMap(Comparatror c):
TreeMap map=new TreeMap(Map m):
TreeMap map=new TreeMap(SortedMap m):
TreeMap Demo program:
package myutil;
import java.util.TreeMap;
public class TreeMapEx1 {
public static void main(String[] args)
{
TreeMap map=new TreeMap();
map.put(new Integer(100),"orange");
map.put(new Integer(200) ,"apple");
map.put(new Integer(300),"grapes");
System.out.println("The TreeeMap Is "+map);
// Try to place the null you will get NullPointerException
// map.put(null,"javasun");
}
}
output:
The TreeeMap Is {100=orange, 200=apple, 300=grapes}
TreeMap Example code with Comparator:
package myutil;
import java.util.Comparator;
import java.util.TreeMap;
class Mycompare implements Comparator
{
public int compare(Object o1,Object o2)
{
Integer i1=(Integer)o1;
Integer i2=(Integer)o2;
return -i1.compareTo(i2);
}
}
public class TreeMapCom {
public static void main(String[] args)
{
TreeMap map=new TreeMap(new Mycompare());
map.put(new Integer(100),"orange");
map.put(new Integer(300),"apple");
map.put(new Integer(200),"grapes");
map.put(new Integer(400),"grapes");
System.out.println("The TreeeMap Is "+map);
// Try to place the null you will get NullPointerException
//map.put(null,"javasun");
}
}
output:
The TreeeMap Is {400=grapes, 300=apple, 200=grapes, 100=orange}
Properties:
- It is the child class of Hashtable
- For Properties both keys and values must be Strings
Properties props=new Properties();
Methods:
getProperty(String key);
setProperty(String key,String value);
propertyNames();
load(InputStream stream);
store(OutputStream stream,String comment);
Properties Demo Program:
import java.util.*;
public class PropertiesDemo {
public static void main(String[] args ){
Properties props=new Properties();
FileInputStream fis=new FileInputStream(“abc.properties”);
props.load(fis);//Load the file
System.out.println(props.getProperty(“orange”));
props.setProperty(“apple’,200);
FileOutputStream fos=new FileOutputStream(“abc.properties”));
}
}
0 comments:
Post a Comment