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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:40:37+00:00 2026-05-25T12:40:37+00:00

I am getting this strange output in HashMap . I have two ArrayList<String> one

  • 0

I am getting this strange output in HashMap.

I have two ArrayList<String> one containing the key and another containing value.

My HashMap<String,String> will store only string as key and value pair. But key itself is getting stored in value. I have checked my value arraylist, it’s printing the value. But during putting it’s setting it as key itself.

Code snippet is:

public HashMap<String,String> getLstBarring()
    {
        ArrayList<String> temparrLst=setPreParameters(fetchPreDetails, 1);
        System.out.println("KEY"    +  temparrLst);

        ArrayList<String> tempArrLstId=setPreParameters(fetchPreDetails, 14);
        System.out.println("VALUE"      +tempArrLstId);
        int length=tempArrLstId.size();

        for(int index=0;index<length;index++)
        {
            System.out.println("VALUE IN KEY"   +  temparrLst.get(index));
            System.out.println("VALUE IN VALUE"   +   tempArrLstId.get(index));
            this.lstBarring.put(temparrLst.get(index), tempArrLstId.get(index));
        }
        System.out.println("INSIDE ODB....>>>>>>>>>>>>>>"   + lstBarring);

    return this.lstBarring;
    }

Problem is:

  • 1st SOP is KEY-printing all the key correctly.
  • 2nd SOP is VALUE-printing all the value correctly.
  • 3rd SOP is VALUE IN KEY—-printing all the values.
  • 4th SOP is VALUE IN VALUE–printing all the values.

Hence after ever iteration I am getting value,value in HashMap whereas it should be key,value.

Here’s look at my Method:-

public ArrayList<String> setPreParameters(HashMap<Integer,String> fetchPreDetails,int index)
    {   
        switch(index)
        {

        case 1:
        {
            arrLstData.clear();
            splittedString=fetchPreDetails.get(1).split(",");
            Collections.addAll(arrLstData, splittedString); 
            break;

        }

return arrLstData;

Please guide me as to where am I going wrong.

  • 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-25T12:40:37+00:00Added an answer on May 25, 2026 at 12:40 pm

    My guess is that either fetchPreDetails is a collection being mutated by setPreParameters() or else setPreParameters() is mutating some other shared state so that the collection referenced by your temparrLst is being changed on the second call to setPreParameters(). I.e.

    List<String> strings = new ArrayList();
    strings.add("a");
    strings.add("b");
    List<String> otherStrings = strings;
    otherStrings.add("c");
    

    I expect your code assumes that strings would contain “a” and “b” and that otherStrings would contain “a”, “b”, and “c”. This isn’t how object references work in Java. The line List<String> otherStrings = strings; makes both strings and otherStrings point to the same collection, and thus changes made using either name affect the same thing.

    Edit: Your newly-posted code seems to prove my hypothesis. You have a variable called arrLstData that you clear, populate, and return on each call to setPreParameters(). You’re returning the same collection every time you call this method. Therefore you just have multiple handles to the same collection instead of multiple collections. You need to create a new collection and return it each time you call setPreParameters().

    Edit again: Maybe this will make it clearer. Here’s what you’re doing:

    public static void main(String[] args) {
        Foo f = new Foo();
        List<String> list1 = f.getList("a", "b");
        System.out.println(list1);
        List<String> list2 = f.getList("c", "d");
        System.out.println(list2);
        System.out.println(list1);
    }
    
    static class Foo {
        private List<String> myList = new ArrayList<String>();
        public List<String> getList(String... strings) {
            myList.clear();
            myList.addAll(Arrays.asList(strings));
            return myList;
        }
    }
    

    Note that this exhibits exactly the behavior that you’re describing, and the correct way to solve it is something like this:

    public static void main(String[] args) {
        Foo f = new Foo();
        List<String> list1 = f.getList("a", "b");
        System.out.println(list1);
        List<String> list2 = f.getList("c", "d");
        System.out.println(list2);
        System.out.println(list1);
    }
    
    static class Foo {
        public List<String> getList(String... strings) {
            List<String> result = new ArrayList<String>();
            result.addAll(Arrays.asList(strings));
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

STDOUT is getting some strange output: ignored attr: {}abstract All of my SOAP calls
I'm getting a strange error. I have a function of the following signature: template
This is a really strange behavior, and I've set up some demo code to
I'm having an issue with a trigger that inserts a value into the same
Bit of a strange problem here... I've got an update query that isn't working,
I am very aware of compiling C++ programs with g++ in linux environment. But,
I am trying to get the last line of a file. The file, ff.log,
I'm parsing an xml file and i've been trying to strip out the whitespace
import csv with open('test.csv', 'rb') as f: data = list(csv.reader(f)) import collections counter =
I am trying to use MediaWiki's API to get articles in XML format and

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.