简体   繁体   English

如何从Map映射到List

[英]How to map from a Map to List

I'm working on a project where I need to map from a hierarchical Map<String, Object> where the second parameter is expected to be a String or Map<String, Object> . 我正在开发一个项目,我需要从分层Map<String, Object>其中第二个参数应该是StringMap<String, Object> Thus far, ModelMapper has worked awesome, but I'm having trouble with a specific conversion. 到目前为止,ModelMapper工作得很棒,但我遇到了特定转换的问题。 Given the following, where Model is simply a marker interface: 鉴于以下内容, Model只是一个标记接口:

class Client implements Model {
    String firstName;
    String lastName;
    List<Organization> organizations = new ArrayList<>();
}

Assuming Organization implements my marker interface Model but is otherwise arbitrarily structured, I would like to be able to map the following to Client : 假设Organization实现了我的标记接口Model但是其他方面是任意结构的,我希望能够将以下内容映射到Client

Map source = new HashMap(){{
    put("firstName", "John"),
    put("lastName", "Smith"),
    put("organizations", new HashMap(){{
        put("0", new HashMap(){{
            put("id", "1234");
        }});
        put("abc", new HashMap(){{
            put("id", "5678");
        }});
    }});
}};

In summary: how might I go about having ModelMapper drop the map keys and use only the value list when pushing into the organizations property? 总结:在推入组织属性时,我如何让ModelMapper删除映射键并仅使用值列表? Failing this, the organizations property comes up empty. 如果做不到这一点,组织的财产就会变空。 You may assume that I have a good reason for wishing to do this, and a poor example! 你可能认为我有充分的理由希望这样做,而且是一个糟糕的例子!

Thanks in advance for your input! 提前感谢您的意见!

Edit 编辑

I attempted to build a Converter that would manage converting from a Map to a Collection<Model> to no avail. 我试图构建一个Converter来管理从Map转换为Collection<Model>无效。 Given: 鉴于:

new Converter<Map, Collection<DataTransportObject>>() {
    @Override
    public Set convert(MappingContext<Map, Collection<DataTransportObject>> context) {
        LOG.debug("Here");
        Set<DataTransportObject> result = new HashSet<>();
        return result;
    }
}

There are two problems with this: 这有两个问题:

  1. Apparently ModelMapper doesn't look at inheritance, therefore when any implementation of Map is given as the source, the converter is not run. 显然,ModelMapper不会查看继承,因此当给出Map任何实现作为源时,转换器不会运行。 If, for example, I change the converter to accept HashMap and pass a HashMap as the source, then it works. 例如,如果我将转换器更改为接受HashMap并传递HashMap作为源,那么它可以工作。
  2. context.getGenericDestinationType() returns List or Set classes without the generic information. context.getGenericDestinationType()返回没有通用信息的ListSet类。 How then does ModelMapper manage to construct complex model graphs under normal circumstances? 那么ModelMapper如何设法在正常情况下构建复杂的模型图?

In your case I think you can use additional setter for Pojo class. 在你的情况下,我认为你可以为Pojo类使用额外的setter。 And then for convert map to object use libraries as ObjectMapper/BeanUtils/... . 然后将转换映射到对象使用库作为ObjectMapper / BeanUtils / .... For example: 例如:

void setOranizations(Map<String, Organization> map) {
    this.organizations = map.values();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM