如何从arraylist中过滤唯一对象.
List<LabelValue> uniqueCityListBasedState = new ArrayList<LabelValue>();
for (LabelValue city : cityListBasedState) {
if (!uniqueCityListBasedState.contains(city)) {
uniqueCityListBasedState.add(city);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码.但问题是我不需要过滤对象,而是过滤该对象内属性的值.在这种情况下,我需要排除具有该名称的对象.
那是 city.getName()
List<LabelValue> uniqueCityListBasedState = new ArrayList<LabelValue>();
uniqueCityListBasedState.add(cityListBasedState.get(0));
for (LabelValue city : cityListBasedState) {
boolean flag = false;
for (LabelValue cityUnique : uniqueCityListBasedState) {
if (cityUnique.getName().equals(city.getName())) {
flag = true;
}
}
if(!flag)
uniqueCityListBasedState.add(city);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8155 次 |
| 最近记录: |