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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:40:22+00:00 2026-06-05T13:40:22+00:00

I would like to have an arrayList that holds reference to object inside the

  • 0

I would like to have an arrayList that holds reference to object inside the reduce function.

@Override
public void reduce( final Text pKey,
                    final Iterable<BSONWritable> pValues,
                    final Context pContext )
        throws IOException, InterruptedException{
    final ArrayList<BSONWritable> bsonObjects = new ArrayList<BSONWritable>();

    for ( final BSONWritable value : pValues ){
        bsonObjects.add(value);
        //do some calculations.
    }
   for ( final BSONWritable value : bsonObjects ){
       //do something else.
   }
   }

The problem is that the bsonObjects.size() returns the correct number of elements but all the elements of the list are equal to the last inserted element.
e.g. if the

{id:1}

{id:2}

{id:3}

elements are to be inserted the bsonObjects will hold 3 items but all of them will be {id:3}.
Is there a problem with this approach? any idea why this happens?
I have tried to change the List to a Map but then only one element was added to the map.
Also I have tried to change the declaration of the bsonObject to global but the same behavior happes.

  • 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-05T13:40:23+00:00Added an answer on June 5, 2026 at 1:40 pm

    This is documented behavior. The reason is that the pValues Iterator re-uses the BSONWritable instance and when it’s value changes in the loop all references in bsonObjects ArrayList are updated as well. You’re storing a reference when you call add() on bsonObjects. This approach allows Hadoop to save memory.

    You should instantiate a new BSONWritable variable in that first loop that equals the variable value (deep copy). Then add the new variable into bsonObjects.

    Try this:

    for ( final BSONWritable value : pValues ){
        BSONWritable v = value; 
        bsonObjects.add(v);
        //do some calculations.
    }
    for ( final BSONWritable value : bsonObjects ){
       //do something else.
    }
    

    Then you will be able to iterate through bsonObjects in the second loop and retrieve each distinct value.

    However, you should also be careful — if you make a deep copy all the values for the key in this reducer will need to fit in memory.

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

Sidebar

Related Questions

I have an ArrayList that i would like to join with a delimiter of
I have an ArrayList of strings that look as below, I would like to
Let's say I have a Java ArrayList, that is sorted. Now I would like
I have a List<class> that I would like to convert into a json object
I have an Hashmap <integer, Arraylist<Double>> . I would like to make a copy
So in my project i would like have a nice treeview that has images.
I would like to have a submit code that works with different forms like:
I have an arraylist that holds a subset of names found in my database.
If I have an ArrayList that has lines of data that could look like:
in c# if i have an arraylist populated like (ID, ITEMQUANTITY), and i would

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.