Implement ArraySet
From Java Example Source Code
Contents |
[edit] Overview - Implement ArraySet
This is a Java example program.
[edit] Java Source Code
- Package: example.array
- File: ArraySet.java
package example.array;
import java.io.Serializable;
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 setIterator 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();
}}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()) {
System.out.println(iter.next());
}}}
[edit] What Result You Can Get
Run the program, you will get:
Java Source and Support .
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
Categories: Java | Java Utilities | Arrays | Collection | Iterator | Set | ArrayList | Serialization
