Android 如何仅向ArrayList添加唯一值<;HashMap<;字符串,字符串>>;?

Android 如何仅向ArrayList添加唯一值<;HashMap<;字符串,字符串>>;?,android,arraylist,hashmap,Android,Arraylist,Hashmap,我尝试在我的应用程序中执行以下操作: 我从一些文本视图中获取一些字符串值。我以以下方式将此值添加到ArrayList,其中 cart\u列表的类型为HashMap>,cart的类型为ArrayList 我只想将唯一值添加到购物车。如何检查“ArrayList>中是否已存在给定值。请一步一步地告诉我该怎么做。使用集合实现之一,而不是ArrayList A Set is a Collection that cannot contain duplicate elements 文档试试这个 for(i

我尝试在我的应用程序中执行以下操作:

我从一些
文本视图
中获取一些
字符串
值。我以以下方式将此值添加到
ArrayList
,其中

cart\u列表
的类型为
HashMap>
,cart的类型为
ArrayList


我只想将唯一值添加到
购物车
。如何检查“ArrayList>中是否已存在给定值。请一步一步地告诉我该怎么做。

使用
集合
实现之一,而不是ArrayList

A Set is a Collection that cannot contain duplicate elements
文档

试试这个

for(int i = 0; i < cart.size(); i++){
     if(cart.get(i).containsKey(yourkey))
        Toast.makeText(getBaseContext(), "The key already present in HashMap.", Toast.LENGTH_SHORT).show();

     if(cart.get(i).containsValue(yourvalue)){
           String key = getKeyByValue(cart.get(i), yourvalue);
           if(key.equals(yourkey)){
                 Toast.makeText(getBaseContext(), "The keys are same having same value.", Toast.LENGTH_SHORT).show();
           }
     }

}
for(int i=0;i
和getKeyByValue方法

public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
    for (Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}
public static T getKeyByValue(映射映射,E值){
for(条目:map.entrySet()){
if(value.equals(entry.getValue())){
return entry.getKey();
}
}
返回null;
}
在此中,您要检查给定键(类似字符串的数量)的键已存在

yourvalue喜欢购物车中的Itemname
HashMap
value

Set st=cart\u list.keySet();
    Set<String> st = cart_list.keySet();
    if(!st.contains("newKey")){
        cart_list.put("YourKey", "Yourvalue");
        cart.add(cart_list);    
    }
    else{
        System.out.println("Dupliation not allowed");
    }
如果(!st.contains(“newKey”)){ cart_list.put(“你的钥匙”、“你的价值”); 购物车添加(购物车列表); } 否则{ System.out.println(“不允许复制”); }
在不同的集合中尝试此选项

public ArrayList<HashMap<String,String>> DistN(ArrayList<HashMap<String,String>> col)
    {

        ArrayList<HashMap<String,String>> DistN;
        DistN = new ArrayList<HashMap<String,String>>();

        for(int i = 0; i < col.size(); i++) {

                final HashMap<String, String> chval = new HashMap<String, String>();
                chval.put(NO, col.get(i).get(NO));
                chval.put(IE, col.get(i).get(IE));

                if (!DistN.contains(chval)) {

                    DistN.add(chval);
                }

        }

        return DistDN;

    }
public ArrayList DistN(ArrayList col)
{
ArrayList区;
DistN=新的ArrayList();
对于(int i=0;i
我使用的是同一个键,我只想检查与每个键关联的值是否不同。没有名为containsvalue@Ann编辑它的
cart.get(i).containsValue(yourvalue)
它工作了……我用
return entry.getvalue()替换了
return entry.getkey()
getkeybyvalue
方法中的
。非常感谢。您应该选择一些字段作为唯一字段,因为如果您希望所有字段都是唯一的,则需要遍历每个
ArrayList
项并检查每个项的HashMap。
public ArrayList<HashMap<String,String>> DistN(ArrayList<HashMap<String,String>> col)
    {

        ArrayList<HashMap<String,String>> DistN;
        DistN = new ArrayList<HashMap<String,String>>();

        for(int i = 0; i < col.size(); i++) {

                final HashMap<String, String> chval = new HashMap<String, String>();
                chval.put(NO, col.get(i).get(NO));
                chval.put(IE, col.get(i).get(IE));

                if (!DistN.contains(chval)) {

                    DistN.add(chval);
                }

        }

        return DistDN;

    }