如何从ListView <hashmap <>&gt;中删除重复项

时间:2017-05-09 19:40:55

标签: android listview hashmap

列表重复了许多相同的值,我想删除它们。也许有人会检查出了什么问题或者给出了一种新方法来做到这一点?

class Duplicats implements Comparator {
    public int compare(Object obj1, Object obj2) {
        HashMap<String, String> test1 = (HashMap<String, String>) obj1;
        HashMap<String, String> test2 = (HashMap<String, String>) obj2;
        if(test1.get("name").equalsIgnoreCase(test2.get("name"))){
            return 0;
        }
        return 1;
    }
}

2 个答案:

答案 0 :(得分:0)

假设您使用obj1中的值替换obj2中的值(或者如果您切换订单则反之亦然):

Map<String, String> objCombined = new HashMap<String, String>();

//Add the 2nd object's items
objCombined.putAll( (HashMap<String, String>) obj2);

//Add the first object's items, replacing duplicates
objCombined.putAll( (HashMap<String, String>) obj1);

如果您希望obj2的值是保留的值,只需切换上面put次调用的顺序。

答案 1 :(得分:0)

那么从这个位置怎么做?

{
 ...

                    HashMap<String, String> controllers = new HashMap<>();

                    controllers.put("id", id);
                    controllers.put("name", name);

                    DevicesList.add(controllers);


                    }
相关问题