[英]Remove objects with similar values from ArrayList
I am working with ArrayList ,Here I want to remove objects with similar values from Arraylist. 我正在使用ArrayList,在这里我想从Arraylist中删除具有相似值的对象。 I tried many solutions posted over stackoverflow but something is wrong in my code due to which code is not working .
我尝试了许多通过stackoverflow发布的解决方案,但是由于哪些代码不起作用,我的代码中出现了问题。 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
代码在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 . 完成上述代码后,我在selectedBusinessTypeList中获取具有相似值的对象。
Help me please , I am not able to find what is wrong in the above code . 请帮帮我,我找不到上面代码中的错误。
You can also do in this way 你也可以这样
Step 1: Only insert those data in arraylist which id's are not same so after that you don't need to remove duplicate elements from arraylist. 步骤1:仅将ID不相同的那些数据插入到arraylist中,因此在此之后,您无需从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);
}
}
});
Replace it by 替换为
businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
}
StateCityModel stateCityModel = new StateCityModel();
stateCityModel = clubsList.get(position);;
if (!selectedBusinessTypeList.contains(stateCityModel)){
selectedBusinessTypeList.add(stateCityModel);
}
}
}); });
Step2 : Remove this not needed 第二步:删除不需要的
Set<StateCityModel> unique = new LinkedHashSet<StateCityModel>(selectedBusinessTypeList);
selectedBusinessTypeList = new ArrayList<StateCityModel>(unique);
I guess you are seeing duplicate values in the list right ? 我猜您在列表中看到重复的值了吗? Since reference of selectedBusinessTypeList is changed.
由于selectedBusinessTypeList的引用已更改。 Change adapter also by creating new adapter by passing new selectedBusinessTypeList and set that adapter to listview again inside that onClick
通过传递新的selectedBusinessTypeList来创建新适配器,从而更改适配器,并将该适配器再次设置为该onClick内部的listview
You can replace the code to add values by below lines: 您可以替换代码以通过以下几行添加值:
businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
businessTypeObj = clubsList.get(position);
if (!selectedBusinessTypeList.contains(businessTypeObj)) {
selectedBusinessTypeList.add(businessTypeObj);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.