EDITED/SOLVED still looking for better answer I do have a answer here How do I remove repeated elements from ArrayList? and it is working my question is relation to ONLY list and not Set. Since through out application flow list has been implemented and it is difficult for me to change all List references to Set reference as https://docs.oracle.com/javase/7/docs/api/java/util/List.html has get(..) Set doesn't have get(..).
Each object getting added is a new Instance. hi i Have a model class
public final class MyClass implements Comparable<MyClass> {
public static final int APP = 0;
public static final int FILE = 1;
public static final int FOLDER = 2;
private String name;
private String path;
private String pkg;
private Long size;
private boolean selected;
private Integer type;
public MyClass(String name, String path, String pkg, Long size, boolean selected, int type) {
this.name = name;
this.path = path;
this.pkg = pkg;
this.size = size;
this.selected = selected;
this.type = type;
if (!TextUtils.isEmpty(path)) {
File file = new File(path);
if (file.exists()) {
this.size = file.length();
}
}
}
public String getName() {
return name;
}
public String getPath() {
return path;
}
public String getSize() {
return FileUtils.getReadableFileSize(size);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public Integer getType() {
return type;
}
public String getPkg() {
return pkg;
}
@Override
public String toString() {
return "{Pkg=" + pkg + ", Path=" + path + ", size=" + size + ", hashcode: " + hashCode() +"}";
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
MyClass myclass = (MyClass) o;
if (path != null ? !path.equals(myclass.path) : myclass.path != null)
return false;
if (pkg != null ? !pkg.equals(myclass.pkg) : myclass.pkg != null)
return false;
return size != null ? size.equals(myclass.size) : myclass.size == null;
}
@Override
public int hashCode() {
int result = path != null ? path.hashCode() : 0;
result = 31 * result + (pkg != null ? pkg.hashCode() : 0);
result = 31 * result + (size != null ? size.hashCode() : 0);
return result;
}
@Override
public int compareTo(MyClass myclass) {
return myclass.getType().compareTo(this.type);
}
}
But when i create common object, these object are getting added to list, even though their hashcode is same it is duplicate in list. Anything i may be doing wrong here.
Output of toString of List is as follows:
[{
Pkg = com.a.bc,
Path = /data/app / com.a.bc - 1 / base.apk,
size = 1800820,
hashcode: -908060882
}, {
Pkg = com.a.b.c,
Path = /data/app / com.a.b.c - 1 / base.apk,
size = 21279534,
hashcode: 1116685502
}, {
Pkg = com.a.b.c,
Path = /data/app / com.a.b.c - 1 / base.apk,
size = 21279534,
hashcode: 1116685502
}]
Here is some dirty implementation but this is not what i was looking for...
private final class DuplicateFilterArrayList<E> extends ArrayList<E> {
private DuplicateFilterArrayList() {
}
@Override
public boolean add(E object) {
if (object instanceof MyClass) {
if (!contains(object)) {
return super.add(object);
} else {
Logger.error(TAG, "Object already exists go home");
return false;
}
} else {
throw new IllegalArgumentException("Unsupported Object type " + object.getClass().getSimpleName());
}
}
@Override
public boolean contains(Object object) {
if (object instanceof MyClass) {
MyClass otherMyClassObjec = (MyClass) object;
for (E myClassItem : this) {
MyClass newMyClass = (MyClass) myClassItem;
if (newMyClass.equals(otherMyClassObjec) && newMyClass.hashCode() == otherMyClassObjec.hashCode()) {
return true;
}
}
}
return false;
}
}