Showing posts with label Set. Show all posts
Showing posts with label Set. Show all posts

Thursday, September 12, 2013

TreeMap

TreeMap is a data structure that most its operations (e.g. containsKey, get,put, remove) cost log(n) time. Like HashMap, TreeMap can be used to store pairs of keys and values. However, the TreeMap uses Binary Search Tree technique in its internal structure. You can sort entries in the TreeMap (key-based sorting).  The following code fragment creates a TreeMap object with default native sorting.

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapDemo {
TreeMap<String,String> tmap;
TreeMapDemo(){
tmap=new TreeMap<String, String>(); //create TreeMap object with default sort order

}

public static void main(String[] args){
TreeMapDemo hmd=new TreeMapDemo();

}


}
You can add a new entry that contains key and value by using the put<Object key,Object value) method. The following code adds some entries to the TreeMap, tmap.
public void addEntries(){

tmap.put("85516500379","Sunrise Child Care & Development Center");
tmap.put("85523880502","Krousar Thmey - Temporary Street Children");
tmap.put("855545483","Ming Hour Home Service");
tmap.put("85515907797","Happy Kid Care");


}

The get(Object key) method can be used to return a value for a specified key input. The following code fragment accepts the key input. Then its checks whether the key contains in the TreeMap, tmap. If true, the value of an entry that contains the specified key will be returned.

public String searchMap(String key){
if(tmap.containsKey(key)) return tmap.get(key);
else return null;
}

To traverse through the TreeMap, you can use its keySet method. The keySet method returns a set object that contains all keys in the TreeMap. The set object has the iterator method that can be used to read every key in the set object. The following code below displays all keys and their values in the TreeMap, tmap.

public void showTreeMapContent(){
Set<String> keys=tmap.keySet();
Iterator<String> i=keys.iterator();
while(i.hasNext()){
String key=i.next();
System.out.println(key+"\t"+tmap.get(key));

}

}

For the native sorting, the smaller key comes first, then the next is the larger key. You can change this order by using the Comparator interface. You need to create a class that implements this interface and override the compare(Object obj1, Object obj2) method. Then create an instance of the class and supply it to the constructor of the TreeMap. The program below can be compiled and run to see the result. First, you can give -1 value to the Sorter and compile and run the program. Again give 1 value to the Sorter and compile and run the program. You will notice that different sort orders are made to the entries of the TreeMap.

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapDemo {
TreeMap<String,String> tmap;
TreeMapDemo(){
tmap=new TreeMap<String, String>(new Sorter(-1));
}

class Sorter implements Comparator<Object>{
int order=-1;
Sorter(int order){
this.order=order;
}

public int compare(Object ob1,Object ob2){
if(ob1.toString().compareTo(ob2.toString())==0) return 0;
else if(ob1.toString().compareTo(ob2.toString())<0)
return order;
else
return(-1*order);
}

}

public void addEntries(){
tmap.put("85516500379","Sunrise Child Care & Development Center");
tmap.put("85523880502","Krousar Thmey - Temporary Street Children");
tmap.put("855545483","Ming Hour Home Service");
tmap.put("85515907797","Happy Kid Care");


}

public String searchMap(String key){
if(tmap.containsKey(key)) return tmap.get(key);
else return null;
}
public void showTreeMapContent(){
Set<String> keys=tmap.keySet();
Iterator<String> i=keys.iterator();
while(i.hasNext()){
String key=i.next();
System.out.println(key+"\t"+tmap.get(key));

}

}

public static void main(String[] args){
TreeMapDemo tmd=new TreeMapDemo();
tmd.addEntries();
tmd.showTreeMapContent();

}


}

Wednesday, September 11, 2013

HashMap

HashMap is a data structure that can store pairs of keys and values. The internal structure of the HashMap is constructed based on the hashing organization technique in which its items are grouped in buckets. When creating a hash map object, you are allowed to specify the number of buckets or capacity and the load factor. The good value of the load factor is 0.75 so that the cost of lookup processes can be minimized. The lookup processes here refer to the operations to read an entry from the hash map object or to put a new entry to the hash map object.  The initial capacity should be greater than the result of the number of entries divided the load factor. This can ensure that the structure of the map is not reorganized so that the efficiency can be maintained. For example, if you plan to store 100 entries in the hash map object, its initial capacity should be greater than 133.  The code fragment below creates a hash map object with 134 of capacity and 0.75 of load factor.

import java.util.HashMap;
import java.util.Set;

public class HashMapDemo {
HashMap<String,Integer> hmap;
HashMapDemo(){
//create a hash map object with its number of buckets or capacity 134
//and loader factor 0.75
hmap=new HashMap<String, Integer>(134,(float) 0.75);
}

public static void main(String[] args){
HashMapDemo hmd=new HashMapDemo();
}

}
The HashMap has two important methods-put(Object Key, Object Value) and get(Object Key). The put method is an operation to add a new entry to the hash map object. The code fragment below adds somes country codes and country names to the hash map object.

public void initCodesCountries(){
hmap.put("Australia",61);
hmap.put("Austria",43);
hmap.put("Bhutan",975);
hmap.put("Cambodia",855);
hmap.put("Canada",1);
hmap.put("China",86);
hmap.put("Denmark",45);
hmap.put("Japan",81);
hmap.put("United Kingdom",44);
hmap.put("United States",1);

}

The get method returns the value of an entry in the hash map object for the specified key. In the code fragment below, the containKey(Object key) method is used to check whether the specified key contains in the hash map object. If this operation returns true, the searchMap method returns the value for the specified key. Otherwise, it returns -1.

public int searchMap(String key){
if(hmap.containsKey(key)) return hmap.get(key); //returns the value for the specified key input
else return -1;
}

To get all keys in the hash map object, you can use its keySet() method. The keySet method returns a set object that contains all keys stored in the hash map object. When you have the set of keys, you can read all values in the hash map as well. See the example code fragment below.

public void listCodesCountries(){
Set<String> countries=hmap.keySet(); //read all keys in the hash map
Object[] keyCs=countries.toArray(); //convert the set object to array of Objects
for(Object k:keyCs){  //display country codes and names
System.out.println(k+"\t"+hmap.get(k.toString()));
}
}