Custom ArraySet implementation (extends AbstractSet) : Array Collections « Collections Data Structure « Java

Custom ArraySet implementation (extends AbstractSet)

Custom ArraySet implementation (extends AbstractSet)
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

public class ArraySet extends AbstractSet implements Cloneable, Serializable {

  private ArrayList list;

  public ArraySet() {
    list = new ArrayList();

  public ArraySet(Collection col) {
    list = new ArrayList();

    // No need to check for dups if col is a set
    Iterator itor = col.iterator();
    if (col instanceof Set) {
      while (itor.hasNext()) {
    } else {
      while (itor.hasNext()) {

  public Iterator iterator() {
    return list.iterator();

  public int size() {
    return list.size();

  public boolean add(Object element) {
    boolean modified;
    if (modified = !list.contains(element)) {
    return modified;

  public boolean remove(Object element) {
    return list.remove(element);

  public boolean isEmpty() {
    return list.isEmpty();

  public boolean contains(Object element) {
    return list.contains(element);

  public void clear() {

  public Object clone() {
    try {
      ArraySet newSet = (ArraySet) super.clone();
      newSet.list = (ArrayList) list.clone();
      return newSet;
    } catch (CloneNotSupportedException e) {
      throw new InternalError();

  public static void main(String args[]) {
    String elements[] = { "Java", "Source", "and",
        "Support", "." };
    Set set = new ArraySet(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {


Related examples in the same category

1.Array Iterator
2.Array MapArray Map
3.Array SetArray Set
4.Array Int Set
5.Remove duplicate element from array
6.Convert an Array to a List
7.Converting an Array to a Collection
8.Converting a Collection of user objects to an Array
9.Create an array containing the elements in a set
10.Convert an array to a Map
11.Converting a Collection of String to an ArrayConverting a Collection of String to an Array
12.Treating an Array as an Enumeration
13.ArrayEnumeration class (implements Enumeration)ArrayEnumeration class (implements Enumeration)
14.Custom ArrayMap implementation (extends AbstractMap)Custom ArrayMap implementation (extends AbstractMap)
15.Converts array into a java.util.Map.
16.Growable array of intsGrowable array of ints
17.Growable array of floats.
18.Acts like an java.util.ArrayList but for primitive int valuesActs like an java.util.ArrayList but for primitive int values
19.Acts like an java.util.ArrayList but for primitive long values
20.Add array to collection