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 220237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:55:11+00:00 2026-05-11T18:55:11+00:00

If I create a new HashMap and a new List, and then place the

  • 0

If I create a new HashMap and a new List, and then place the List inside the Hashmap with some arbitrary key and then later call List.clear() will it affect what I’ve placed inside the HashMap?

The deeper question here being: When I add something to a HashMap, is a new object copied and placed or is a reference to the original object placed?

Thanks!

  • 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-11T18:55:11+00:00Added an answer on May 11, 2026 at 6:55 pm

    What’s happening here is that you’re placing a pointer to a list in the hashmap, not the list itself.

    When you define

    List<SomeType> list;
    

    you’re defining a pointer to a list, not a list itself.

    When you do

    map.put(somekey, list);
    

    you’re just storing a copy of the pointer, not the list.

    If, somewhere else, you follow that pointer and modify the object at its end, anyone holding that pointer will still be referencing the same, modified object.

    Please see http://javadude.com/articles/passbyvalue.htm for details on pass-by-value in Java.

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

Sidebar

Related Questions

I want to create new node of BeanTreeView, and when I add some node
given the following code? final Map<String, List<E>> map = new HashMap<String, List<E>>(); List<E> list
My code does this - List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>(); every time
after I create a new object : Map<Float,Integer> m = new HashMap()<Float,Integer>; I have
let say I have this code Map<String, String> list = new HashMap<String, String>(); list.put(number1,
I want to create a top 5 list of unique key value pairs sorted
I create new desktop with CreateDesktop and want to get it's DC & RC.
I create new blank solution. Add exist web site. Structure: Solution E:...\projectname Folders: bin
I can't create New Project on my Visual Web Developer 2008 Express with SP1.
I tryed to create new components from one base Windows Form, also I found

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.