Am I missing something or is this a bug?? "Contains" method not passing requirements. Thanks for the help!
package com.codegym.task.task37.task3707;
import java.io.Serializable;
import java.util.*;
import static java.lang.StrictMath.max;
public class AmigoSet<E> extends AbstractSet implements Serializable, Cloneable, Set {
private static final Object PRESENT = new Object();
private transient HashMap<E, Object> map;
public AmigoSet(){
map = new HashMap<>();
}
public AmigoSet(Collection<? extends E> collection){
map = new HashMap<>(max(16, (int) (collection.size()/.75f + 1)));
for (E e : collection) map.put(e, PRESENT);
}
public boolean add(Object e){
if (!map.containsKey(e)) {
map.put((E) e, PRESENT);
return true;
}
return false;
}
@Override
public Iterator<E> iterator() {
return map.keySet().iterator();
}
@Override
public int size() {
return map.size();
}
public boolean isEmpty(){
return map.isEmpty();
}
public boolean contains(Object o){
/*for (Map.Entry<E, Object> entry : map.entrySet()){
if (entry.getValue().equals(o)){
return true;
}
}
return false;*/
return map.containsValue(o);
}
public void clear(){
map.clear();
}
public boolean remove(Object o){
boolean deleted = false;
for (Map.Entry<E, Object> entry : map.entrySet()){
if (entry.getValue().equals(o)){
map.remove(entry.getKey(), o);
deleted = true;
break;
}
}
return deleted;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
AmigoSet<?> amigoSet = (AmigoSet<?>) o;
return Objects.equals(map, amigoSet.map);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), map);
}
}