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

  • SEARCH
  • Home
  • 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 7018399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:02:45+00:00 2026-05-27T23:02:45+00:00

EDIT: forgot a line of code that is likely causing the issue (night’s sleep

  • 0

EDIT: forgot a line of code that is likely causing the issue (night’s sleep helped 😉

Ok it’s late so I’m probably missing something fundamental here but this is my issue:

I have query my db and get a data set that looks like this:

retail_store_id basket_item_id  price_txt
1           2       8.99
1           1       1.5
1           6       11.09
1           4       3.99
2           6       10.99
2           2       9
2           1       1.79
3           4       2.99
3           1       1.5
4           1       1.39
4           6       9.99
4           4       3.5
4           2       7.99

Now I have a Store class which contains a List items and each basketitem has a price.

now to get that into my dto’s I do this:

    Map<Integer, Store> shoppedBasketHash = new HashMap<Integer, Store>();
    while (rs.next()){
            int storeID = rs.getInt("retail_store_id");

            basketItem = new BasketItem();
                            basketItem = GlobalHashOfItems.get(rs.getInt("basket_item_id"));
            basketItem.price = new BigDecimal(rs.getString("price_txt"));

            if(shoppedBasketHash.containsKey(storeID)){
                shoppedBasketHash.get(storeID).items.add(basketItem);                   
            }else{

                Store store = new Store();
                store.retailStoreID = rs.getInt("retail_store_id");

                store.items = new ArrayList<BasketItem>();
                store.items.add(basketItem);

                shoppedBasketHash.put(storeID, sb);
            }
        }
   return new ArrayList<Store>(shoppedBasketHash.values());

Now for some reason, later on when I take this list:

    for(Store s: listOfStores){
        for(BasketItem b: s.items){
                         System.out.println(b.price);
                    }
        }

I get this:
9.99

3.5

7.99

1.39

1.39

9.99

7.99

1.39

3.5

3.5

7.99

1.39

9.99

Which is not the same list of prices I took in. What am I doing wrong?

I realize now the issue is in the
basketItem = GlobalHashOfItems.get(rs.getInt(“basket_item_id”));

I have a hash of items (without prices) that contains other item info not pulled from the DB that I need to use then add the price too. But it’s not copying by value it’s using an object reference. How do I make basketItem get assigned to the value of the GlobalHashOfItems item and not a reference?

  • 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-05-27T23:02:45+00:00Added an answer on May 27, 2026 at 11:02 pm

    Your problem is that HashMap does not preserve insertion/iteration order when it is converted into an ArrayList to return from your method.

    You can solve this by building the ArrayList to return while using the Map for addition.

    Another way would be to use a LinkedHashMap which does preserve insertion/iteration order.

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

Sidebar

Related Questions

EDIT Sorry I forgot the most important part here. Each key can have more
Edit: From another question I provided an answer that has links to a lot
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
[ EDIT - I long since forgot this was here until I got the
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT What small things which are too easy to overlook do I need to
Edit : Solved, there was a trigger with a loop on the table (read

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.