Show Mobile Navigation

Webring

Powered by WebRing.

Collections framework in Java :complete guide part-2

Sachin R Kukale - 20:42
Set
Set is interface available in java.util package and it also implements/inherits java.util.Collection.
Set collection and its subclasses do not allow duplicate elements in it, like Lists.
Set collection does not have any extra methods, like ‘List’ has with ‘ListIterator’.
You can use the methods available in collection interface with ‘Set’ to manage the elements of ‘Set’.
We have hashCode() and equals() methods in ‘Set’ to identify the unique objects in ‘Set’.
You will find following subclasses in ‘Set’:
        i.            HashSet: ‘HashSet’ does not maintain the order of inserted elements and it also does not allow duplicate elements to be entered in it. Internally ‘HashSet’ will be implemented as hash table data structure.
Searching and retrieving of element operations are fast, in ‘HashSet’.
Elements are stored on the basis of hashCode() i.e. hashCode() of element will be used to decide the position of the element in ‘HashSet’. So if you change the hashCode() of the object then it is possible to add same element again as hashCode() for both of these elements will be different.
      ii.            LinkedHashSet: ‘LinkedHashSet’ is used to store the data in order, as entered by the user i.e. ‘LinkedHashSet’ maintains the order of elements in which they are inserted.
‘LinkedHashSet’ is subclass of ‘HashSet’.
    iii.            TreeSet: Internally ‘TreeSet’ is implemented as red-black tree data structre. It does not allow duplicate elements. ‘TreeSet’ always stores inserted elements in sorted order. You can not add null values in ‘TreeSet’.
Only similar type of elements should be stored/inserted in ‘TreeSet’ it does not allow different types of elements to be stored.
‘TreeSet’ uses compareTo() method with object to compare the objects that you are inserting in ‘TreeSet’. So the objects you are adding to ‘TreeSet’ must implement comparable.
‘TreeSet’ is internally implemented as tree data structure and it uses tree data structure to decide the position of inserted elements and to search for the elements.
java.lang.Comparable: ‘java.lang.Comparable’ interface is available in java.lang package.
The main use of this interface is to imply that the object implementing this interface can be compared. You can compare the objects for sorting purpose or o decide whether the objects are same or not or to decide which is greater or smaller of the objects. For this you can use following method available in this interface:
public  int compareTo(Object obj)
The object you are passing to this method i.e. object that you are going to compare must be subtype of comparable interface and it must provide the implementation for compareTo() method.
If object is not subtype of comparable then following exception will be thrown at runtime:
java.lang.ClassCastException.
java.util.Comparator: As you can see this ‘Comparator’ interface comes under the java.util package. It is used when you want to compare the objects which are not implementing the ‘Comparable’ interface.
In order to use this interface you need to write the separate class which implements ‘Comparable’ interface and you have to override following two methods:
public boolean  equals(Object obj)
public int compare(Object obj1,Object obj2)
Within the compare method you can type cast the object into corresponding type and then you can compare the required objects.

You can compare various properties of the object by using the comparator.

Map
You will find ‘Map’ interface in java.util package and it is introduced from Java2 so it is not part of legacy classes but it is part of collection framework.
Though Map is part of collection framework it does not inherits the collection interface. It is situated at the root of its own hierarchy.
Map subclasses stores data in key-value pairs where key is always unique in a map. Values may have duplicates but at time there must be one unique value for each key. Every key-value pair is called as ‘entry’ in case of Map. ‘Entry’ is inner interface available in map and it is used to store key-value pairs internally.
Some important methods of Map interface:
        i.            int size(): This method will return the size of map i.e. number of entries in the map.
      ii.            boolean isEmpty(): This method checks whether the map is empty or not. If map is empty this method will return ‘true’, otherwise it returns false.
    iii.            boolean containsValue(Object obj): Checks whether the object passed as argument to this method is available in the map or not.
     iv.            Object get(Object key): This method returns the object(value) whose key is passed as argument.
       v.            Object put(Key,Value): This method is used to insert/replace key-value pairs in map.
     vi.            Object remove(Object key): This method removes the object from map which is specified by key.
   vii.            void  putAll(Map m): Adds the entries of map specified by variable ‘m’ to the current map.
 viii.            void clear(): This method removes all the entries from current map.
     ix.            Set keySet(): This method will return all the key values in a current map as Set object.
       x.            Collection values(): This method will return all the values of map as object of collection.
     xi.            Set entrySet(): This method will return all entries in a map as Set object.
   xii.            boolean equals(Object): This method checks whether the two are equal or not, based on entries in the map.
You will find following subclasses in Map interface:
        i.            HashMap: HashMap uses hashing to find appropriate place for key-value pair, so searching and inserting operations are fast in ‘HashMap’. Internally ‘HashMap’ will be implemented as hash-table data structure. The one disadvantage of ‘HashMap’ is that it does not store key-value pairs in any order, but based on hashing concept.
      ii.            LinkedHashMap: ‘LinkedHashMap’ preserves the order of entries specified by the user. It is subclass for HashMap.
    iii.            TreeMap: Internally ‘TreeMap’ will be implemented as red-black tree data structure. So it uses the tree-data structure to find the entries specified by the user. Data in ‘TreeMap’ is always stored in sorted order of keys.
You can use compareTo() method for comparing objects for sorting or other purposes, So objects you are inserting in ‘TreeMap’ must implement the comparable interface.
Null values can not be inserted in ‘TreeMap’.

0 comments:

Post a Comment