根据包含的对象的属性值从ArrayList中过滤唯一对象

135*_*355 0 java arraylist

如何从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()

135*_*355 6

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)