简体   繁体   中英

Store / unset multiple values with same key in HashMap

I am working with on line store project. At the moment I am trying to add possibility to add products to the shopping cart, no matter if user is logged in or not. I am using session bean method to do it.

@Inject ShoppingCartSessionBean shoppingCartSessionBean; 

@POST
public boolean addToCart(@PathParam("productid") int newProductId, @PathParam("qu") int newProductQuantity) {
    shoppingCartSessionBean.setCartItems(newProductId);
    shoppingCartSessionBean.setProductQuantity(newProductQuantity);
    return true;    
 }

I would like to store id's in hash map. However, at the moment I can set only one id for my setter method.

@Stateful
@SessionScoped
public class ShoppingCartSessionBean implements Serializable{

HashMap<Integer, Integer> newmap = new HashMap<Integer, Integer>();

public int addToHashMap() {  

return array of productId's.
}

private static final long serialVersionUID = -5024959800049014671L;

private int productId;

private int productQuantity;

//getters and setters

Map<Integer, ShoppingCartSessionBean> hm = new HashMap<Integer, ShoppingCartSessionBean>();

Later I am using entity manager to check which id's / id were set and send back all information about that id to the user. I am not storing all values in session bean because of space issues.

Query q = em.createQuery("SELECT c FROM Items c WHERE c.productId = :itemid");
        q.setParameter("itemid", shoppingCartSessionBean.addToHashMap());

So I have a few questions:

  1. Is it good choice to store such information in hash map? Or should I use cookies instead?

  2. How my addToHashmap method should look like to store multiple id's in hash map? (I tried a simply int[] array = {123, 456} to print out using my entity manager, however I got JSON error...).

  3. What is the best way to remove / unset such information from hash map?

I hope my information is clear, if you are missing something - let me now.

Point 2 and 3. You need to check if there is a hash collision, in positive case you need to treat it. Look the code below.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class HashMapTest {

    private static HashMap<String, List<String>> map = new HashMap<String, List<String>>();

    public static void insert(String key, String value){
        List<String> list = map.get(key);
        if (list == null){
            list = new ArrayList<String>();
            map.put(key, list);
        }
        list.add(value);
    }



    public static void main(String[] args){

        insert("10", "V1");
        insert("10", "V2");
        insert("20", "V3"); 
        insert("20", "V4");
        insert("30", "V5");
        List<String> values10 = map.get("10");
        System.out.println(values10);
        List<String> values20 = map.get("20");
        System.out.println(values20);
        List<String> values30 = map.get("30");
        System.out.println(values30);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM