Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package com.google.code.joto.util;
  
  import java.util.HashMap;
 import java.util.Set;

This class is based on java.util.HashSet, but instead of using object equality, it uses object identity (instead of equals, it usess ==).

Author(s):
epere4
liliana.nu
 
 public class IdentitySet<E> extends AbstractSet<E> implements Set<E>, Cloneable {
 
 	private HashMap<Integer, E> map;

Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
 
 	public IdentitySet() {
 		 = new HashMap<Integer, E>();
 	}

Constructs a new set containing the elements in the specified collection. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection.

Parameters:
c the collection whose elements are to be placed into this set.
Throws:
java.lang.NullPointerException if the specified collection is null.
 
 	public IdentitySet(Collection<? extends E> c) {
 		 = new HashMap<Integer, E>(Math.max((int) (c.size() / .75f) + 1, 16));
 		addAll(c);
 	}

Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

Parameters:
initialCapacity the initial capacity of the hash map.
loadFactor the load factor of the hash map.
Throws:
java.lang.IllegalArgumentException if the initial capacity is less than zero, or if the load factor is nonpositive.
 
 	public IdentitySet(int initialCapacityfloat loadFactor) {
 		 = new HashMap<Integer, E>(initialCapacityloadFactor);
 	}

Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor, which is 0.75.

Parameters:
initialCapacity the initial capacity of the hash table.
Throws:
java.lang.IllegalArgumentException if the initial capacity is less than zero.
 
 	public IdentitySet(int initialCapacity) {
 		 = new HashMap<Integer, E>(initialCapacity);
 	}

Returns an iterator over the elements in this set. The elements are returned in no particular order.

Returns:
an Iterator over the elements in this set.
See also:
java.util.ConcurrentModificationException
 
 	public Iterator<E> iterator() {
 		return .values().iterator();
 	}

Returns the number of elements in this set (its cardinality).

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

Returns true if this set contains no elements.

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

Returns true if this set contains the specified element.

Parameters:
o element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.
	public boolean contains(Object o) {
	}
		return o == null ? null : System.identityHashCode(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(getKeyForObject(o), 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(getKeyForObject(o)) == o;
	}

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

Returns a shallow copy of this IdentitySet instance: the elements themselves are not cloned.

Returns:
a shallow copy of this set.
	@SuppressWarnings("unchecked")
	public Object clone() {
		try {
			IdentitySet<E> newSet = (IdentitySet<E>) super.clone();
			newSet.map = (HashMap<Integer, E>) .clone();
			return newSet;
			throw new InternalError();
		}
	}
New to GrepCode? Check out our FAQ X