Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.equivalence;
  
  import java.util.Iterator;

Custom hash-based set which accepts no null values, where equality and hash code calculations are done based on passed Equivalence function implementations for values, as opposed to relying on their own equals/hashCode/toString implementations. This is handy when using key/values whose mentioned methods cannot be overriden, i.e. arrays, and in situations where users want to avoid using wrapper objects.

Author(s):
Galder ZamarreƱo
Since:
5.3
 
 public class EquivalentHashSet<E> extends AbstractSet<E> {

   
Equivalence function for the entries of this hash set, providing functionality to check equality, calculate hash codes...etc of the stored entries.
 
    private final Equivalence<E> entryEq;

   
The underlying map. Uses Boolean.TRUE as value for each element.
 
    private final EquivalentHashMap<E, Booleanm;

   
Constructs a new, empty set, with a given equivalence function

Parameters:
entryEq the Equivalence function to be used to compare entries in this set.
 
    public EquivalentHashSet(Equivalence<E> entryEq) {
       this. = entryEq;
        = new EquivalentHashMap<E, Boolean>(entryEq.);
    }

   
Constructs a new, empty set, with a given initial capacity and a particular equivalence function to compare entries.

Parameters:
initialCapacity this set's initial capacity
entryEq the Equivalence function to be used to compare entries in this set.
 
    public EquivalentHashSet(int initialCapacityEquivalence<E> entryEq) {
       this. = entryEq;
        = new EquivalentHashMap<E, Boolean>(
             initialCapacityentryEq.);
    }

   
Returns an iterator over the elements in this set.

Returns:
an iterator over the elements in this set
 
    @Override
    public Iterator<E> iterator() {
       return .keySet().iterator();
    }

   
Returns the number of elements in this set. If this set contains more than Integer.MAX_VALUE elements, it returns Integer.MAX_VALUE.

Returns:
the number of elements in this set
 
    @Override
    public int size() {
       return .size();
    }

   
Returns true if this set contains no elements.

Returns:
true if this set contains no elements
 
    @Override
    public boolean isEmpty() {
       return .isEmpty();
    }

   
Returns true if this set contains the specified element.

Parameters:
o the object to be checked for containment in this set
Returns:
true if this set contains the specified element
 
    @Override
    public boolean contains(Object o) {
       return .containsKey(o);
    }

   
Adds the specified element to this set if it is not already present.

Parameters:
o element to be added to this set
Returns:
true if the set did not already contain the specified element
   public boolean add(E o) {
      return .put(o.) == null;
   }

   
Removes the specified element from this set if it is present.

Parameters:
o object to be removed from this set, if present
Returns:
true if the set contained the specified element
   public boolean remove(Object o) {
      return .remove(o) != null;
   }

   
Removes all of the elements from this set.
   public void clear() {
      .clear();
   }

   
Returns the hash code value for this set using the Equivalence function associated with it. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hash code of a null element is defined to be zero. This ensures that Equivalence.equals(java.lang.Object,java.lang.Object) implies that Equivalence.hashCode(java.lang.Object)==Equivalence.hashCode(java.lang.Object) for any two sets s1 and s2, as required by the general contract of java.lang.Object.hashCode().

This implementation iterates over the set, calling the hashCode method on each element in the set, and adding up the results.

Returns:
the hash code value for this set
   public int hashCode() {
      int h = 0;
      Iterator<E> i = iterator();
      while (i.hasNext()) {
         E obj = i.next();
         if (obj != null)
            h += .hashCode(obj);
      }
      return h;
   }
New to GrepCode? Check out our FAQ X