Set Implementations

Set:

It's the child interface of collection 

If we want to represents a group individual objects where duplicates are not allowed and and insertion order not preserved then best choice is set.

HashSet:

The underling data structure is HashTable insertion order is not preserved and it's based on hashcode of objects.
Duplicate objects are not allowed if trying to insert duplicate objects we wont get any compile time or run time error simple add method of hashtable returns false.
Heterogeneous objects are allowed for insertion null insertion also possible.



Construtors:

1.HashSet<String> elements=new HashSet<String>();

Creates an empty HashSet object with default initial capacity 10 and default fill ratio 0.75(meas after filling 0.75% of elements it again increase the capacity to 10)

2.HashSet<String> elements=new HashSet<String>(20);


Creates an empty HashSet object with initial capacity 20 and default fill ration is 0.75


3. HashSet<String> elements=new HashSet<String>(10,9);


Creates an empty HashSet object with initial capacity 20 and fill ration is 9
4.HashSet<String> elements=new HashSet<String>(Collection c);
Create HashSet object with given collections

Example:

package com.collections;
import java.util.HashSet;
public class HashDemo {
public static void main(String[] args) {  

HashSet<String> elements=new HashSet<String>();  

elements.add("one"); 

elements.add("one");  

elements.add("one"); 

elements.add("two"); 

elements.add("three");  

System.out.println(elements);  

}

}

output:[two, one, three]

it's not fallow insertion order and it's not allowed
duplicate values.

LinkedHashSet:

It's child class of  HashSet it exactly same as HashSet except the fallowing difference
HashSet                                                          LinkedHashSet
1.the underlying datastructure Hashtable        1.the underlying datastructure is combination of Hashtable
                                                                        Hashtable and linkedlsit
2.insertion order is not preserved                   2.insertion order is preserved
3.introduced in jdk 1.2                                    3.introduced in jdk 1.4


Example:

package com.collections;

import java.util.HashSet;

import java.util.LinkedHashSet;

public class HashDemo {

public static void main(String[] args) {  

LinkedHashSet<String> elements=new LinkedHashSet<String>();  

elements.add("one"); 

elements.add("one");  

elements.add("one");  

elements.add("two"); elements.add("three");  

System.out.println(elements);  

}

}




output:[one, two, three] same like above program but difference in output is
it fallows insertion order.

SortedSet:(Interface)

It's child interface of Set if we want to represent a group of unique individual objects according to some sorting order the we should go for SortedSet implementations 

TreeSet:

1.It's implementation of SortedSet interface . underlying datastructure is balancedtree
2.insertion order is not preserved and it's based on some sorting order
3.duplicate objects are not allowed
4.heterogeneous objects are not allowed otherwise we will get classcastexception
TreeSet<String> list=new TreeSet<String>();
Created an empty treeset where all objects are inserted according to default natural sorting order
TreeSet<String> list=new TreeSet<String>(Comparator c);
Creates an empty treeset where all elemnts will be inserted according to customized sorting order

5.The objects in TreeSet should be homogeneous and Comparble other wise we will get RuntimeException
6.An object is said to be Comparbale if and only if the corresponding class implements Comparable interface
7.String and all wrapper classes  already implements Comparable interface
Example:


package com.collections;
public class InnerTest {


public class InnerOne{ 

public void hai(){  

System.out.println("im inner method and public");  

}  

}

}

 output:    [1, 10, 99]:::TreeSet elemnts

elements fallows sorting order (default sorting order)
More about Comparble and Comparble





Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

0 comments: