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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:17:17+00:00 2026-06-03T07:17:17+00:00

I have a Map containing Strings for keys and Lists as values. After putting

  • 0

I have a Map containing Strings for keys and Lists as values. After putting several entries into the Map and randomly expanding the List values in the Map I get the following problem:

    list3 = map.get("string1");
    list3.get(0).setName("testobject");
    for(TestObject to : list3){
        System.out.println(to.getName());
    }

Instead of only setting the first TestObject with name “testobject” it also sets other TestObjects in the List (list3) with the name “testobject”, ie:

**Output**
testobject
nonameset
nonameset
nonameset
testobject
nonameset
nonameset
nonameset
testobject
nonameset

Full code is written underneath. I guess the problem has something to do with randomly expanding the List values in the map (randomlyExpand function) and referencing, but as my Java knowledge is still beginning to develop, I do not really understand what is happening. It’s probably something really stupid. So my question is: what is going on here? And how can I fix the problem while retaining same functionality.

import java.util.*;
public class Test {
public static void main(String[] args) {
    Map<String, List<TestObject>> map = new HashMap<String,    List<TestObject>>();
    List<TestObject> list1 = new ArrayList<TestObject>();
    List<TestObject> list2 = new ArrayList<TestObject>();
    List<TestObject> list3 = new ArrayList<TestObject>();

    for(int i = 0; i < 5; i++){
        list1.add(new TestObject());
    }
    for(int i = 0; i < 5; i++){
        list2.add(new TestObject());
    }       
    map.put("string1", list1);
    map.put("string2", list2);

    map = randomlyExpand(map); 

    list3 = map.get("string1");
    list3.get(0).setName("testobject");
    for(TestObject to : list3){
        System.out.println(to.getName());
    }
}

//Randomly expand list values in Map from size 5 to size 10
public static Map<String, List<TestObject>> randomlyExpand(Map<String, List<TestObject>> map){
    Random ran = new Random();
    for (String key : map.keySet()){
        List<TestObject> tempList = new ArrayList<TestObject>();
        List<TestObject> values = map.get(key);
        for (int i = 0; i < 10; i++){
            int randomInt = ran.nextInt(values.size());
            tempList.add(values.get(randomInt));
        }
        map.put(key, tempList);
    }
    return map;
}
}

TestObject class:

public class TestObject {
private String name = "nonameset";

TestObject(){
}

public String getName(){
    return this.name;
}

public void setName(String s){
    this.name = s;
}
}
  • 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-03T07:17:18+00:00Added an answer on June 3, 2026 at 7:17 am

    The problem is in your randomlyExpand function. You are iterating trough your list of 5 distinguished objects that you have previously created with this:

    for(int i = 0; i < 5; i++){
        list2.add(new TestObject());
    }
    

    Now you are doing this:

     int randomInt = ran.nextInt(values.size());
     tempList.add(values.get(randomInt));
    

    You are getting one of this distinguished objects and you add it again on another index. Now this single objects occurs two times in your list (or even more). So you have the same object multiple times in the list. That’s way you see or object multiple times.

    The solution is simple: create new objects in your loop.

    for (int i = 0; i < 10; i++){
        int randomInt = ran.nextInt(values.size());
    
        TestObject obj = new TestObject();
        obj.setName(values.get(randomInt).getName());
    
        tempList.add(obj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Haskell Map, containing strings as keys and some lambda functions as
I'm trying to map a Dictionary containing Lists. I have the following set of
Possible Duplicate: Scala map containing mix type values I have a situation where in
In C++, I have a map<string, string> , containing an unknown number of entries.
i have to use stl map containing 3 values : float(key) , int ,int.
I have a list containing 305899 Strings (which is the username for a website).
I have a view containing a UIWebView which is loading a google map (so
I have a map declared as follows: map < string , list < string
I have an Android activity that displays a list of log entries (using a
I have a Ruby array containing some string values. I need to: Find all

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.