ca.mcgill.cs.postina.util
Class LinkedHashSet<E>

java.lang.Object
  extended by ca.mcgill.cs.postina.util.LinkedHashSet<E>
Type Parameters:
E - The type of the elements of the structure.
All Implemented Interfaces:
java.io.Serializable

public class LinkedHashSet<E>
extends java.lang.Object
implements java.io.Serializable

The datastructure LinkedHashSet combines the advantages of a LinkedList and of the HashSet. Internally it keeps the data both as a HashSet and as a LinkedList, so the restrictions for HashSets still apply. This structure is useful when a list has to be ordered but also requires many checks if a specified element is in the list (i.e. calls of contains()). The call of contains() on a LinkedList is of complexity O(n) whereas the same operation on a HashSet is of O(1).

Version:
18-Jan-08
Author:
Dominik Zindel
See Also:
Serialized Form

Field Summary
private  java.util.HashSet<E> hashSet
          The internal representation of the list as a HashSet.
private  java.util.LinkedList<E> linkedList
          The internal representation of the list as a LinkedList.
private static long serialVersionUID
           
 
Constructor Summary
LinkedHashSet()
          The default constructor does not take any argument.
 
Method Summary
 void add(E e)
          This method adds an element to the list.
 boolean contains(E e)
          This method checks if a given element is in the list.
 E getFirst()
          This method returns the first element in the ordered list but does not delete it in the list.
 boolean isEmpty()
          This method indicates whether the list is empty.
 E removeFirst()
          This method returns the first element in the ordered list and removes it from the list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

hashSet

private final java.util.HashSet<E> hashSet
The internal representation of the list as a HashSet.


linkedList

private final java.util.LinkedList<E> linkedList
The internal representation of the list as a LinkedList.

Constructor Detail

LinkedHashSet

public LinkedHashSet()
The default constructor does not take any argument.

Method Detail

add

public void add(E e)
This method adds an element to the list.

Parameters:
e - The element to be added.

contains

public boolean contains(E e)
This method checks if a given element is in the list.

Parameters:
e - The element for which it should be indicated if it is in the list.
Returns:
True if the element is in the list, false else.

getFirst

public E getFirst()
This method returns the first element in the ordered list but does not delete it in the list.

Returns:
The first element in the ordered list.

isEmpty

public boolean isEmpty()
This method indicates whether the list is empty.

Returns:
True if the list is empty, false else.

removeFirst

public E removeFirst()
This method returns the first element in the ordered list and removes it from the list.

Returns:
The first element in the ordered list.