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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:17:18+00:00 2026-06-05T04:17:18+00:00

I am having trouble understanding some simple code relating with Lists and maps. Take

  • 0

I am having trouble understanding some simple code relating with Lists and maps. Take the code below as an example:

public class test {

private Map<Integer, List<String>> myMap ;

public test(){
    myMap = new HashMap<Integer, List<String>>();
}

public void addToMap(String ss){

    List<String> myTemp = myMap.get(ss);
    Random  r = new Random();
    if(myTemp == null){

        myTemp = new ArrayList<String>();
        myMap.put(r.nextInt(100), myTemp);
    }

    myTemp.add(ss);

}

public Map<Integer, List<String>> getMap1(){
    return myMap;
}


public  static void main(String args[]){

    test myTest = new test();
    myTest.addToMap("abdc");
    myTest.addToMap("eeer");
    System.out.println(myTest.getMap1());

}

}

How exactly does the addToMap() add a new element to the mylist Map. More specifically, how does myTemp.add(ss) add a new element to myMap when myTemp is a local variable and gets deleted once its done executing. Moreover, removing myTemp.add(ss) from the addToMap() method prints out an empty HashMap in the main method, why is this? How does the put method insert the element into the map when it is executed before the add method? Thanks.

Edit: I edited the code to make a little more sense.

  • 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-05T04:17:20+00:00Added an answer on June 5, 2026 at 4:17 am

    Unlike C++, in Java all variables are references to the real objects. So, when you do

    myTemp = new Object();
    

    you create an object in the heap whose reference is copied to the stack variable myTemp. If the reference is added to the list (or other variable), a copy of the reference is added there.

    Once myTemp is destroyed, the object continues alive. The exception is when all the references to the object are gone; then the object can not be reached by the code and Garbage Collection can (but it is not forced to) delete it from memory.

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

Sidebar

Related Questions

I am having some trouble understanding the following simple C code: int main(int argc,
I'm having some trouble understanding a bit of code. I've got 2 classes Company
I'm having trouble understanding some web part communication code, and how to make it
I'm having some trouble understanding why this code won't work. I got it from
Having some trouble understanding variable scope within a class structure. I want to create
I'm having trouble understanding some basic memory management principles in C++. This code is
I'm having some trouble understanding this piece of code that my professor used as
I'm having some trouble understanding Outlook terms (CommandBarPopup, CommandBarButton etc) like what is what
I'm having some trouble understanding the delegate/data source methodology. I understand that they exist
I am having some trouble understanding designated initializers. I am studying Objective C from

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.