Thursday, January 16, 2014

Java Collection : Set

Following are the properties of Java Set interface

- Set do not allow duplicates . Elements are unique
- Only one null value is allowed
- add() and addAll() doesn't stores duplicate

HashSet and LinkedHashSet Classes 

- HashSet implements Set interface
- HashSet uses HashTable for its implementation
- HashSet maintains no ordering
- LinkedHashSet extends HashSet maintains insertion order.Linked HashSet is a good choice if frequent traversal is required .
- HashSet depends on hashCode() and equals() implementation on elements

package collections;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class TraverseHashSetAndLinkedHashSet {

    public static void main(String[] args) {

        HashSet<String> set1 = new HashSet<String>();
        set1.add("Test1");
        set1.add("Test2");
        set1.add("Test3");

        for (String s : set1) {
            System.out.println(s);

        }

        Set<String> set2 = new LinkedHashSet<>();
        set2.add("Lset");
        set2.add("Lset1");
        set2.add("Lset2");

        for (String s : set2) {
            System.out.println(s);

        }

    }
}

SortedSet<E> and NavigableSet<E> Interface

SortedSet<E>

- SortedSet extends Set
- SortedSet is sorted
- We can use for loop or iterator to iterate over SortedSet
- Supports first last , range view  and comparator access operations 

NavigableSet<E> Interface 

- NavigableSet extends SortedSet
- NavigableSet replaces SortedSet and is the preferred choice of implementation
- Supports first last , range view ,comparator,closest match,reverse order access operations

TreeSet Class

- TreeSet class implements NavigableSet
- By default TreeSet maintains the natural ordering of elements
- There is a constructor available accepting the comparator for ordering
- TreeSet uses balanced trees for excellent (logarithmic) performance
- HashSet is faster than TreeSet due to hashing algorithm
- If there is a requirement to maintain elements in sorted order ,fast insertion and retrieval is required Treeset is a more preferred option

package collections;

import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class SetNaviagation {
    public static void main(String[] args) {
        NavigableSet<String> strSet = new TreeSet<String>();
        Collections.addAll(strSet, "Test1", "Test2", "Test3", "Test4");

        System.out.println(strSet);
        System.out.println(strSet.headSet("Test2", true));
        System.out.println(strSet.tailSet("Test2", false));
        System.out.println(strSet.tailSet("Test2", true));
        System.out.println(strSet.tailSet("Test2", false));

        System.out.println(strSet.subSet("Test1", false, "Test3", true));

        System.out.println(strSet.ceiling("Test"));
        System.out.println(strSet.floor("t3"));

        System.out.println(strSet.higher("Tes"));
        System.out.println(strSet.lower("t3"));

        System.out.println(strSet.descendingSet());

        System.out.println(strSet.pollFirst());
        System.out.println(strSet.pollLast());
        for (String s : strSet) {
            System.out.println(s);
        }

    }
}

No comments: