Help please. I'm stuck with implementing the clone() method. I can get either the clone or the exception requirements to verify, but not both. I was unsure whether I needed to use super.clone().
Thanks in advance.
package com.codegym.task.task37.task3707;
import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import java.io.Serializable;
import java.util.*;
public class AmigoSet<E> extends AbstractSet implements Set, Serializable, Cloneable {
private static final Object PRESENT = new Object();
private transient HashMap<E, Object> map;
public AmigoSet() {
this.map = new HashMap<>();
}
public AmigoSet(Collection<? extends E> collection) {
try {
int capacity = Math.max(16, (int) (collection.size() / .75f + 1));
this.map = new HashMap<>(capacity);
for (E o : collection) {
map.put(o, PRESENT);
}
} catch (Exception e) {
throw new InternalError("Initialisation exception", e);
}
}
@Override
public Iterator iterator() {
Set keys = this.map.keySet();
return keys.iterator();
}
@Override
public boolean add(Object o) {
if (this.map.containsKey(o)) {
return false;
} else {
this.map.put((E) o, PRESENT);
return true;
}
}
@Override
public Object clone() {
try {
return new AmigoSet<E>(this.map.keySet());
} catch (InternalError e) {
throw new InternalError("Cloning exception", e);
}
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean contains(Object o) {
return map.containsKey(o);
}
@Override
public boolean remove(Object o) {
if (map.containsKey(o)) {
map.remove(o);
return true;
}
return false;
}
@Override
public void clear() {
map.clear();
}
@Override
public int size() {
return map.size();
}
}