【问题标题】:How to add only unique values to a ArrayList<HashMap<String,String>>?如何仅向 ArrayList<HashMap<String,String>> 添加唯一值?
【发布时间】:2014-07-14 11:04:28
【问题描述】:

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

我从一些Textviews 中得到一些String 值。我通过以下方式将此值添加到ArrayList&lt;HashMap&lt;String,String&gt;&gt;,其中

cart_list 的类型为 HashMap&lt;String,String&gt;&gt;,而购物车的类型为 ArrayList&lt;HashMap&lt;String,String&gt;&gt;

    cart_list.put("quantity",""+qty);
    cart_list.put("item_id"+item_id_number,""+item_id_number);
    cart_list.put("Category",Itemname);
    cart_list.put("Details",Item_details);
    cart_list.put("Price",Item_price);
    cart_list.put("Currency",Item_currency);
    cart_list.put("images",images);
    cart.add(cart_list);

我只想向cart 添加唯一值。如何检查给定值是否已存在于 `ArrayList> 中。请一步一步告诉我该怎么做。

【问题讨论】:

  • 你应该选择一些唯一的字段,因为如果你想让所有的都是唯一的,你需要遍历每个ArrayList项目并检查每个项目的HashMap。

标签: android arraylist hashmap


【解决方案1】:

试试这个..

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();
           }
     }

}

和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;
}

在这个 yourkey 中,您要检查给定的键(类似字符串的数量)已经存在

yourvalue 喜欢 cart 中的 Itemname HashMap value

【讨论】:

  • 我用的是同一个key,只是想看看每个key关联的值是否不同。
  • 没有名为 containsvalue 的内置方法
  • @Ann 编辑它是cart.get(i).containsValue(yourvalue)
  • 它起作用了....我在getkeybyvalue 方法中用return entry.getvalue() 替换了return entry.getkey()。非常感谢。
【解决方案2】:

使用Set 实现之一而不是ArrayList

A Set is a Collection that cannot contain duplicate elements

Here 文档

【讨论】:

    【解决方案3】:
        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");
        }
    

    【讨论】:

      【解决方案4】:

      试试这个不同的集合

      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;
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-22
        • 1970-01-01
        • 2013-10-05
        • 2014-01-02
        • 2012-07-13
        • 1970-01-01
        相关资源
        最近更新 更多