При отправке задачи на проверку валика не устраивает равенство объектов. При компиляции ошибка при присвоении значений capacity и loadFactory. Хотелось бы понять где тут собака сдохла?
package com.javarush.task.task37.task3707;
import java.io.*;
import java.util.*;
public class AmigoSet<E> extends AbstractSet<E> implements Set<E>, Serializable, Cloneable{
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<>(Math.max(16, (int)Math.ceil(collection.size()/.75f)));
for (E e: collection){
map.put(e, PRESENT);
}
}
@Override
public boolean add(E e) {
return map.put(e, PRESENT) == null;
}
@Override
public Iterator<E> iterator() {
return map.keySet().iterator();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean contains(Object o) {
return map.containsKey(o);
}
@Override
public void clear() {
map.clear();
}
@Override
public boolean remove(Object o) {
Object obj = map.remove(o);
return obj !=null;
}
@Override
public Object clone() {
try {
AmigoSet copy = (AmigoSet)super.clone();
copy.map = (HashMap) map.clone();
return copy;
} catch (Exception e) {
throw new InternalError(e);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AmigoSet)) 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);
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(HashMapReflectionHelper.callHiddenMethod(map, "capacity"));
out.writeObject(HashMapReflectionHelper.callHiddenMethod(map, "loadFactor"));
out.writeInt(map.keySet().size());
for(E e : map.keySet()) {
out.writeObject(e);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
int capacity = in.readInt();
float loadFactor = in.readFloat();
map = new HashMap(capacity, loadFactor);
int size = in.readInt();
for(int i = 0; i<size; i++) {
map.put((E)in.readObject(), PRESENT);
}
}
public static void main(String[] args) throws Exception {
AmigoSet initialAmigoSet = new AmigoSet<>();
for (int i = 0; i < 10; i++) {
initialAmigoSet.add(i);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(initialAmigoSet);
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
AmigoSet loadedAmigoSet = (AmigoSet) objectInputStream.readObject();
System.out.println(initialAmigoSet.size() + " " + loadedAmigoSet.size());
}
}