Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7931481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:43:54+00:00 2026-06-03T20:43:54+00:00

I work on Java/HTML project. I’ve set a hashmap as a session attribute. I

  • 0

I work on Java/HTML project. I’ve set a hashmap as a session attribute. I request the hashmap from session and put key/value in it
map.put("some string", "1")
. When this action is performed the second time, I print the hashmap content and the only value, that was ‘1’ after the last operation on the hashmap, becomes ‘-1’.

Hashmap is the best data structure, in my opinion, for this project. Can anyone help?

public class Cart {
    private HashMap<String, asd> list;

    public Cart(){
        list = new HashMap<String, asd>();
    }

    public HashMap<String, asd> getMap(){
        return list;
    }

    /*
     * Parameter: code
     *      -1: increase quantity by 1
     *      0: delete product from the product list
     *      else: set the product quantity to the passed value
     */
    public void alterProduct(int code, String product){
        if(list.containsKey(product)) {
            if(code == -1) plusOne(product);
            if(code == 0) remove(product);
            else setValue(product, code);
        }else {
            asd asd = new asd();
            asd.a = 1;
            list.put(product, asd);
        }
    }

    private void plusOne(String product){
        asd asd = list.get(product);
        asd.a = asd.a + 1;
        list.put(product, asd);
    }

    private void remove(String product){
        list.remove(product);
    }

    private void setValue(String product, int code){
        asd asd = new asd();
        asd.a = code;
        list.put(product, asd);
    }
}

class asd{
    int a;

    public String toString(){
        return ""+a;
    }
}

JSP code where I set Cart object as session attribute:

<%
    Cart myCart = new Cart();
    session.setAttribute("cart",myCart);
%>

Servlet code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.alterProduct(-1, (String) request.getSession().getAttribute("name"));
        doGet(request, response);
    }

After I call alterProduct the second time for the same (String) request.getSession().getAttribute("name") the hashmap value for the same key is ‘-1’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:43:56+00:00Added an answer on June 3, 2026 at 8:43 pm

    What is type/value of product? How it is connected to the “cart”?

    I guess what’s happen that you mess up data types. Another option is you have bug in the Cart.toString() method. I suggest you change the code with “plain” data type and recheck it. If it fails, use your Cart class without messy conversion and debug the code.

    You have bug here:

      public void alterProduct(int code, String product){
        if(list.containsKey(product)) {
          if(code == -1) plusOne(product);
          if(code == 0) remove(product);
          else setValue(product, code);
      }
    
    
      private void setValue(String product, int code){
        asd asd = new asd();
        asd.a = code;
        list.put(product, asd);
      }
    

    Consider what happen when you call art.alterProduct(-1, "something") second time.
    list.containsKey(product) is true (you use the same product”), code is -1. So

    if(code == -1) plusOne(product); is executed as expected.

    But then you have something weired

    if(code == 0) remove(product);
    else setValue(product, code);

    code ==0 is evaluated to false, so else instruction is called. You are calling setValue(product, -1)

    As you can see above setValue will assign -1 to the asd.a that is observed by you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to work with a third-party Java library from .NET. Can anyone recommend
I have a project that generates HTML pages using a Velocity template and Java.
I work on a Java EE project which is planned to become quite large
I am doing tests on an ejb3-project using ejb3unit http://ejb3unit.sourceforge.net/Session-Bean.html for testing. All my
Is there any way to make this work in Java? public static void change(List<?
I'm a .NET programmer doing some Hadoop work in Java and I'm kind of
Class.forName(boolean.class.getName()); This doesn't work in Java - the virtual machine slaps you with a
Many parts of the Java API work differently depending on platform defaults, for example,
But it doesn't work -meaning the java code is not executed. Although the batch
I need to work around a Java bug in JDK 1.5 which was fixed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.