I am working with ArrayList ,Here I want to remove objects with similar values from Arraylist. I tried many solutions posted over stackoverflow but something is wrong in my code due to which code is not working . I am getting list with dupliactes .
Here is my code :
public class StateCityModel {
private String id ;
private String code ;
private String name ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof StateCityModel))
return false;
return id.equals(((StateCityModel) obj).getId());
}
@Override
public int hashCode() {
return (id == null) ? 0 : id.hashCode();
}
}
Code add values in the ArrayList
businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
businessTypeObj = clubsList.get(position);
selectedBusinessTypeList.add(businessTypeObj);
}
}
});
Code to remove objects with similar values .
Set<StateCityModel> unique = new LinkedHashSet<StateCityModel>(selectedBusinessTypeList);
selectedBusinessTypeList = new ArrayList<StateCityModel>(unique);
After doing above code I am getting objects with similar values in the selectedBusinessTypeList .
Help me please , I am not able to find what is wrong in the above code .