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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:09:09+00:00 2026-05-23T11:09:09+00:00

Let’s say I have an object Foo which holds a list of references to

  • 0

Let’s say I have an object Foo which holds a list of references to object Bar:

public class Foo{
    String fooStr;
    List<Bar> barList;

    //get/set barList/someStr go here
}

public class Bar {
    String barStr;
}

Using Dozer, assuming Bar to java.util.HashMap is a trivial mapping, how do I map instances of Foo to instances of java.util.HashMap such that the referenced instances of Bar are mapped to java.util.HashMap as well? That is, I want the result of the mapping to be a HashMap with key “barList” which holds an instance of ArrayList>. Each HashMap in this list should be a mapping of Bar to HashMap.

For instance, if Foo held a single reference to Bar rather than a list, I’d do:

<mapping>
    <class-a>Foo</class-a>
    <class-b>java.util.Map</class-b>

    <field>
        <a>bar</a>
        <b key="bar">this</b>
        <a-hint>Bar</a-hint>
        <b-hint>java.util.Map</b-hint>
    </field>
</mapping>

And this would produce a hashmap that’d look like this (using JSON objects to represent HashMaps):

{
    "fooStr" : "value of fooStr",
    {
       "barStr" : "value of barStr"
    }
}

But I want to know how I can express the conversion to HashMap with a list of references to Bar such that I get this:

{
    "fooStr" : "value of fooStr",
    "barList" : [{ "barStr" : "bar1" }, { "barStr" : "bar2" }, ...]
}

Answers which do not use a custom mapper are preferred, but I understand if this is the only way of achieving this behavior.

  • 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-23T11:09:09+00:00Added an answer on May 23, 2026 at 11:09 am

    You should be able to do this in a custom converter:

    public class Foo {
        String fooStr;
        List<Bar> barList;
        ...
    }
    
    public class Bar {
        String barStr;
    }
    
    public class Target {
        String fooStr;
        Map<String, Bar> barMap;   
    }
    
    
    public class TestCustomConverter implements CustomConverter {
    
      public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
        if (source == null) {
          return null;
        }
        if (source instanceof Foo) {
          Map<Bar> dest = null;
          // check to see if the object already exists
          if (destination == null) {
            dest = new Target();
          } else {
            dest = (Target) destination;
          }
          ((Target) dest).setFooStr(source.getFooStr());
          for(Bar : source.getBarList()) {
              ((Target) dest).getBarMap().put(bar.getBarStr(), bar);
          }
          return dest;
        } else if (source instanceof Target) {
          Foo dest = null;
          // check to see if the object already exists
          if (destination == null) {
            dest = new Foo ();
          } else {
            dest = (Foo) destination;
          }
          dest.getFoos().addAll(((Target)source).getBarMap().values());
          dest.setFooStr(((Target)source).getFooStr()):
          return dest;
        } else {
          throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:"
              + destination + " and " + source);
        }
      }
    

    Warning: Code may contain bugs, but should give you the idea.

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have the string: hello world; some random text; foo; How could
Let's say, we have and list of objects in variable called articles, each object
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:

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.