import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArraySet<T> implements Iterable<T> {
private int size; // the next item to be added will be at position size
private T[] items;
public ArraySet() {
items = (T[]) new Object[100];
size = 0;
}
/*
* Returns true if this map contains a mapping for the specified key.
*/
public boolean contains(T x) {
for (int i = 0; i < size; i += 1) {
if (items[i].equals(x)) {
return true;
}
}
return false;
}
/*
* Associates the specified value with the specified key in this map.
* Throws an IllegalArgumentException if the key is null.
*/
public void add(T x) {
if (x == null) {
throw new IllegalArgumentException("can't add null");
}
if (contains(x)) {
return;
}
items[size] = x;
size += 1;
}
/* Returns the number of key-value mappings in this map. */
public int size() {
System.out.println(this);
return size;
}
/** returns an iterator (a.k.a. seer) into ME */
public Iterator<T> iterator() {
return new ArraySetIterator();
}
private class ArraySetIterator implements Iterator<T> {
private int wizPos;
public ArraySetIterator() {
wizPos = 0;
}
public boolean hasNext() {
return wizPos < size;
}
public T next() {
T returnItem = items[wizPos];
wizPos += 1;
return returnItem;
}
}
@Override
public String toString() {
List<String> list = new ArrayList<>();
for (T t : this) {
list.add(t.toString());
}
return String.join(", ", list);
}
public static <E> ArraySet<E> of(E... e) {
ArraySet<E> returnSet = new ArraySet<>();
for (E x : e) {
returnSet.add(x);
}
return returnSet;
}
@Override
public boolean equals(Object other) {
if (this == other)
return true;
if (other == null)
return false;
if (this.getClass() != other.getClass())
return false;
ArraySet<T> o = (ArraySet<T>) other;
if (o.size != this.size)
return false;
for (T x : this) {
if (!o.contains(x))
return false;
}
return true;
}
public static void main(String[] args) {
ArraySet<Integer> aset = new ArraySet<>();
aset.add(5);
aset.add(23);
aset.add(42);
// iteration
for (int i : aset) {
System.out.println(i);
}
// toString
System.out.println(aset);
// equals
ArraySet<Integer> aset2 = new ArraySet<>();
aset2.add(5);
aset2.add(23);
aset2.add(42);
System.out.println(aset.equals(aset2));
System.out.println(aset.equals(null));
System.out.println(aset.equals("fish"));
System.out.println(aset.equals(aset));
System.out.println(ArraySet.of("Hi", "I'm", "zdkk"));
}
}