1

I am trying to do the following in my application:

I am getting some String values from an some Textviews. I am adding this values to ArrayList<HashMap<String,String>> in the following way,where

cart_list is of the type HashMap<String,String>> and cart is of the type ArrayList<HashMap<String,String>>

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

I want to add only unique values to cart. How can I check whether a given value is already present in the `ArrayList>. Please tell me step by step what to do.

1
  • You should choose some field to be unique, because if you want all to be unique, you would need to loop through every ArrayList item and check each item's HashMap. Commented Jul 14, 2014 at 11:21

4 Answers 4

1

Try this..

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

}

and getKeyByValue method

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

In this yourkey which you want to check given key(String like quantity) is already present

yourvalue like Itemname in your cart HashMap value

Sign up to request clarification or add additional context in comments.

4 Comments

I am using the same key, I just want to check whether the values associated with each key is different.
there is no inbuilt method named containsvalue
@Ann edited it's cart.get(i).containsValue(yourvalue)
It worked....I replaced the return entry.getkey() with return entry.getvalue() in the getkeybyvalue method. Thanks a lot.
0

Use one of the Set implementation instead of ArrayList

A Set is a Collection that cannot contain duplicate elements

Here the documentation

Comments

0
    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");
    }

Comments

0

Try this for distinct sets

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;

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.