Array Set extends AbstractSet : Set : Collections Data Structure : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  Collections Data Structure   » [  Set  ]  Screenshots 
 



Array Set extends AbstractSet




import java.io.Serializable;
import java.util.*;

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()) {
        list.add(itor.next());
      }
    else {
      while(itor.hasNext()) {
        add(itor.next());
      }
    }
  }

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

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

  public boolean add(Object element) {
    boolean modified;
    if (modified = !list.contains(element)) {
      list.add(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() {
    list.clear();
  }

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


           
       
Related examples in the same category
1.  Things you can do with Sets Things you can do with Sets
2.  Putting your own type in a Set Putting your own type in a Set
3.  Use set Use set
4.  Another Set demo
5.  Set substraction Set substraction
6.  Working with HashSet and TreeSet Working with HashSet and TreeSet
7.  TreeSet Demo TreeSet Demo
8.  Show the union and instersection of two sets
9.  Demonstrate the Set interface
10.  TreeSet Test
11.  Sync Test
12.  Set Copy
13.  Set and TreeSet
14.  Tail
15.  What you can do with a TreeSet What you can do with a TreeSet
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.