wellnwill Marathahalli Bangalore

NavigableSet:

It is an interface added in java 6. It is subtype of SortedSet interface. Objects can be accessed in both forward and reverse order.

import java.util.*;
class NaviTest
{
public static void main(String args[])
{
TreeSet ts=new TreeSet();
ts.add(1);
ts.add(20);
ts.add(12);
ts.add(9);
ts.add(6);
System.out.println(ts);
System.out.println("--------------");
Set st1=ts.descendingSet();
System.out.println(st1);
System.out.println("Ascending order");
Iterator itr=ts.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println("Descending order");
Iterator itr1=ts.descendingIterator();
while(itr1.hasNext())
{
System.out.println(itr1.next());
}}}

HashSet :

Suppose we have added 3,5,5 in hashSet then 3,5,5 will be used to get hashcode i.e.Object member is used to calculate hashcode if two objects have same hashcode then equals() method will be used to check for equality i.e. if it returns true means objects are duplicate.

Hashing algorithm

Object member Hashing algorithm Hash code equals method
3 ------- 5 -
5 ------- 2 -
5 ------- 2 Duplicate element (cannot inserted)


HashSet LinkedHashSet TreeSet
No insertion order Insertion order is pre class="prettyprint linenums:1 lang-java"served Object will be stored in sorted order
Duplicate not allowed Duplicate not allowed Duplicate not allowed
Implementation is based on HashMap. Implementation is based on HashSet and LinkedList. Implementation is based on TreeMap.
Null is allowed Null is allowed Null is not allowed because it uses compareTo() internally for sorting purpose.
hashCode() and equals() method will be used internally to identify weather object is duplicate hashCode() and equals() method will be used internally to identify weather object is duplicate hashCode() and equals() method will be used internally to identify weather object is duplicate
Footer with Map

Top