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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:35:40+00:00 2026-06-04T03:35:40+00:00

I am not entirely sure what title to put on this problem to get

  • 0

I am not entirely sure what title to put on this problem to get the right minds. This seems like a Java slight of hand but it’s only occuring using the Guava Collections2.transform. The transformer is providing a completely different instance of my object during iteration on ‘results’ then what is contained when ‘results’ is finally return. And thus, the ‘setDateStamp()’ doesn’t actually appear to work because it’s being set on instances that seem to just appear and vanish like a ghost.

When I implement what the logical equivalent of the Collections2.transform() method is doing (commented out code) I get the results I expect. I have stepped through the google code, breakpoints and all, and no where is a new instance being created through any method other than my underlying Function.

I get what their implementation is doing: transform-as-needed. Not complicated. So why the hell doesn’t this work?

Here is the code in question along with some debug

@Component
public class SurveyResultToQuestionResults implements Function<SurveyResult, Collection<QuestionResult>> {

@Autowired
private QuestionResultDtoToDomain dtoToDomain;

@Override
public Collection<QuestionResult> apply(@Nullable SurveyResult input) {
    Collection<QuestionResult> results = new HashSet<QuestionResult>();
    if (input != null) {
           // substitute this
//            for (QuestionResultDto dto : input.getResults()) {
//                QuestionResult result = dtoToDomain.apply(dto);
//                results.add(result);
//            }
        // for this
        results = Collections2.transform(input.getResults(), dtoToDomain);
        for (QuestionResult result : results) {
            long time = input.getSurveyTime().getTime();
            Timestamp dateStamp = new Timestamp(time);
            result.setDateStamp(dateStamp);
        }

    }
    return results;
    }
}

next class

@Component
public class QuestionResultDtoToDomain implements Function<QuestionResultDto, QuestionResult> {

@Override
public QuestionResult apply(@Nullable QuestionResultDto input) {
    QuestionResult result = null;
    if (input != null)
        result = new QuestionResult(input.getAnswerOriginId(),input.getAnswer(),input.getQuestionId());
    return result;
}

}

And a test

@RunWith(MockitoJUnitRunner.class)
public class SurveyTransformerTest {

    @Spy
    private QuestionResultDtoToDomain dtoToDomain = new QuestionResultDtoToDomain();

    @InjectMocks
    private SurveyResultToQuestionResults surveyResultToQuestionResults = new SurveyResultToQuestionResults();        
    @Test
    public void testSurveyToQuestionResults() throws Exception {
        Set<QuestionResultDto> answers = new HashSet<QuestionResultDto>();
        answers.add(new QuestionResultDto(17L,"question 2 answer"));
        answers.add(new QuestionResultDto(18L,"question 3 answer"));
        answers.add(new QuestionResultDto(19L,"question 4 answer"));
        SurveyResult result = new SurveyResult(10L,16L,new Date(),answers);
        Collection<QuestionResult> qresults = surveyResultToQuestionResults.apply (result);
        System.out.println(qresults);       
        for (QuestionResult qresult : qresults) {
            assertNotNull(qresult.getDateStamp());
        }

    }
}


Debug:
Bad implementation
[QuestionResult{questionResultId=null, answer='question 4 answer', dateStamp=null}, QuestionResult{questionResultId=null, answer='question 2 answer', dateStamp=null}, QuestionResult{questionResultId=null, answer='question 3 answer', dateStamp=null}]

Good implementation:
[QuestionResult{questionResultId=null, answer='question 4 answer', dateStamp=2012-05-17 00:02:18.615}, QuestionResult{questionResultId=null, answer='question 3 answer', dateStamp=2012-05-17 00:02:18.615}, QuestionResult{questionResultId=null, answer='question 2 answer', dateStamp=2012-05-17 00:02:18.615}]
  • 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-04T03:35:42+00:00Added an answer on June 4, 2026 at 3:35 am

    You’re surprised that the writes on the new objects aren’t writing through to the backing collection?

    Collections.transform doesn’t just do the transform “as needed” — it doesn’t store anything at all. That’s what “view” means in its documentation. Any time you walk through a Collections2.transformed collection, it applies the function again, freshly. Once that for loop in the apply method is done with result, that object is gone; never seen again.

    If you want to do what you’re doing, make an explicit copy of the transformed collection in e.g. an ArrayList.

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

Sidebar

Related Questions

I'm not entirely sure why this crash is happening and I'd like to get
Not entirely sure how to adequately title this problem, but it entails a need
Not entirely sure of a good title for this, feel free to edit it
I'm not entirely sure what's breaking here on this page: http://grandoaks.org/floor.html but it seems
I'm not sure if my Title for this question entirely describes accurately enough what
I'm not entirely sure what is the best approach to this problem, so hopefully
Not entirely sure if something like this exists or not. If it does, im
I'm not entirely sure how to phrase this as a concise question title. I
Pretty clear in the title, I think. I'm not entirely sure on this, and
Not entirely sure my question title describes what I want to do, but couldn't

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.