Vector, Stack & Set Interface
Vector, Stack & Set Interface
The SortedSet interface present in java.util package extends the Set interface present in
the collection framework. It is an interface that implements the mathematical set
the navigable set extends the sorted set interface. Since a set doesn’t retain the insertion order,
the navigable set interface provides the implementation to navigate through the Set
The SortedSet interface is declared as:
All the elements of a SortedSet must implement the Comparable interface (or be accepted by the
specified Comparator) and all such elements must be mutually comparable. Mutually Comparable
simply means that two objects accept each other as the argument to their compareTo method.
The class which implements the SortedSet interface is TreeSet. t behaves like a simple set with
the exception that it stores elements in a sorted format. TreeSet uses a tree data structure for
storage. Objects are stored in sorted, ascending order.
Adding Elements: use the add() method. the insertion order is not retained in the TreeSet.
Internally, for every element, the values are compared and sorted in the ascending order. So that
null values and duplicate values are not accepted.
Accessing the Elements: contains(), first(), last(),
Removing the Values: using the remove() method.
Iterating through the SortedSet: use the enhanced for loop.
Methods
comparator() Returns the comparator which is used to order the elements in the
given set. Also returns null if the given set uses the natural ordering
of the element.
headSet(E toElement) Returns a view of the portion of the given set whose elements are
strictly less than the toElement.
last() Returns the reverse order view of the mapping which present in the
map.
spliterator() Returns a key-value mapping which is associated with the least key
in the given map. Also, returns null if the map is empty.
subSet(E fromElement, Returns a key-value mapping which is associated with the greatest
E toElement) key which is less than or equal to the given key. Also, returns null if
the map is empty.
tailSet(E fromElement) Returns a view of the map whose keys are strictly less than the
toKey.
Example:
import java.util.*;
class Test{
System.out.println(ts);
String check = "D";
System.out.println(check + " " + ts.contains(check));
System.out.println();
}
}
Navigable Set
The NavigableSet interface inherits from the SortedSet interface. It behaves like a SortedSet with
the exception that we have navigation methods available in addition to the sorting mechanisms of
the SortedSet.
The classes that implement this interface are, TreeSet and ConcurrentSkipListSet
It provide method for navigating the set in both forward and backward direction
METHOD DESCRIPTION
Returns the least element in this set greater than or equal to the
ceiling?(E e)
given element, or null if there is no such element.
descendingSet() Returns a reverse order view of the elements contained in this set.
Returns the greatest element in this set less than or equal to the
floor?(E e)
given element, or null if there is no such element.
headSet?(E toElement, boolean Returns a view of the portion of this set whose elements are less
inclusive) than (or equal to, if inclusive is true) toElement.
Returns the least element in this set strictly greater than the given
higher?(E e)
element, or null if there is no such element.
Returns the greatest element in this set strictly less than the given
lower?(E e)
element, or null if there is no such element.
METHOD DESCRIPTION
subSet?(E fromElement, boolean Returns a view of the portion of this set whose elements range
fromInclusive, E toElement, from fromElement to toElement.
boolean toInclusive)
subSet?(E fromElement, E Returns a view of the portion of this set whose elements range
toElement) from fromElement, inclusive, to toElement, exclusive.
tailSet?(E fromElement, boolean Returns a view of the portion of this set whose elements are
inclusive) greater than (or equal to, if inclusive is true) fromElement.
Example
import java.util.*;
//NavigableSetExample
public class Main {
public static void main(String[] args) {
// Create a new TreeSet
NavigableSet treeSet = new TreeSet<>();
Example 2
import java.util.NavigableSet;
import java.util.TreeSet;
1. HashSet()
HashSet<E> hs = new HashSet<E>();
3. HashSet(int initialCapacity)
HashSet<E> hs = new HashSet<E>(int initialCapacity);
4. HashSet(Collection)
this constructor is used when any conversion is needed from any Collection object to the HashSet object.
Methods in HashSet
Method Description
Used to check whether the set is empty or not. Returns true for empty and
isEmpty()
false for a non-empty condition for set.
Used to verify the equality of an Object with a HashSet and compare them.
equals() The list returns true only if both HashSet contains the same elements,
irrespective of order.
This method is used to remove all the elements from the collection which are
removeAll(collection) present in the set. This method returns true if this set changes as a result of
the call.
Example
import java.util.HashSet;
public class Book {
private String title;
private String author;
@Override
public String toString() {
return title " by " author;
}
bookSet.add(book1);
bookSet.add(book2);
bookSet.add(book3);
System.out.println("HashSet: " bookSet);
System.out.println("Size: " bookSet.size());
LinkedHash Set
Java LinkedHashSet class is a Hashtable and Linked list implementation of the Set interface. It inherits the
HashSet class and implements the Set interface.
LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements.
When the iteration order is needed to be maintained this class is used.
. When cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which
they were inserted.
Properties of LinkedHashset
2. LinkedHashSet(Collection C): Used in initializing the HashSet with the elements of the collection C.
LinkedHashSet<E> hs = new LinkedHashSet<E>(Collection c);
3. LinkedHashSet(int size): Used to initialize the size of the LinkedHashSet with the integer mentioned in the
parameter.
LinkedHashSet<E> hs = new LinkedHashSet<E>(int size);
4. LinkedHashSet(int capacity, float fillRatio): Can be used to initialize both the capacity and the fill ratio, also
called the load capacity of the LinkedHashSet with the arguments mentioned in the parameter.
LinkedHashSet<E> hs = new LinkedHashSet<E>(int capacity, int fillRatio);
Method
spliterator() Creates a late-binding and fail-fast Spliterator over the elements in this set
Example:
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class LinkedHashSetExample {
public static void main(String[] args) {
LinkedHashSet<Book> hs=new LinkedHashSet<Book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to hash table
hs.add(b1);
hs.add(b2);
hs.add(b3);
//Traversing hash table
for(Book b:hs){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}