猿问

如何在 Java 中合并两个对象的属性?(用户定义类的属性和映射的键值)

我正在开发 spring boot 应用程序并且有两个对象,一个是自定义类的列表,另一个是字符串键和字符串值的映射。前任。


DTO object with fixed fields fieldA,fieldB,fieldc. 


[{"fieldA": "lookupval1","fieldB": "val2","fieldc":"val3"},

{"fieldA": "lookupval1","fieldB": "val5","fieldc":"val6"}

{"fieldA": "lookupval2","fieldB": "val8","fieldc":"val9"}]

另外请注意,此字段 A 的前两个对象具有重复值


然后我有Map<String,Map<String,String>如下图的地图


{

    lookupval1= {fieldA:"lookupval1",fieldD:"xxx",.............},

    lookupval2= {fieldA:"lookupval2",fieldD:"yyy",.............}.

    .

    .

    .

}

我想要的是,我想将列表中的每一项中的字段与Map中的字段组合起来,我可以通过获取fieldA的查找值在map中找到相应的键。


字段A:lookupval1


然后我想将该响应发送到 REST 客户端,执行此操作的有效方法和数据结构是什么?


预期结果:


[

{"fieldA": "lookupval1","fieldB": "val2","fieldc":"val3",fieldD:"xxx"},

{"fieldA": "lookupval1","fieldB": "val2","fieldc":"val3",fieldD:"xxx"}

{"fieldA": "lookupval2","fieldB": "val8","fieldc":"val9",fieldD:"yyy"}]

]

更新:


下面是 DTO 对象的结构和 Map 的确切类型。


class DTO {

    int id;

    String name;

    String desc;

}


List<DTO> dtoList = getDTOList(); // returns list of DTO object


Map<String,Map<String,String> mapCollection

    = getMapCollection(); // return String, Map<String,String> pairs

此处 mapCollection 中的键始终等于 DTO 的 id,并且 DTO 列表将具有重复值。

我已经按照我的方式发布了答案,但正在寻找更好的替代方法来减少计算量。


撒科打诨
浏览 233回答 2
2回答

动漫人物

List<DTO> dtoList = getDTOList(); // returns list of DTO objectMap<String,Map<String,String> mapCollection&nbsp; &nbsp; = getMapCollection(); // return String, Map<String,String> pairsList<Map<String,String>> result =&nbsp; new HashMap<>();// iterate over dto listfor(DTO singleDTO : dtoList) {&nbsp; &nbsp; Map<String,String> singleResult = new HashMap<>();&nbsp; &nbsp; // Add all fields in map&nbsp; &nbsp; singleResult.put("id",singleDTO.getId());&nbsp; &nbsp; singleResult.put("name",singleDTO.getName());&nbsp; &nbsp; singleResult.put("desc",singleDTO.getDesc());&nbsp; &nbsp; // fetch single map by id lookup&nbsp; &nbsp; Map<String,Strig> singleMap = mapCollection.get(singleDTO.getId());&nbsp; &nbsp; // fetch all entries from map&nbsp; &nbsp; Set<Map.Entry<String,String>> entrySet = singleMap.entrySet();&nbsp; &nbsp;// add them in hashmap&nbsp; &nbsp; for(Map.Entry<String,String> singleEntry : entrySet) {&nbsp; &nbsp; &nbsp; &nbsp; singleResult.put(singleEntry.getKey(),singleEntry.getValue());&nbsp; &nbsp; Add map in the list&nbsp; &nbsp; result.add(singleEntry);}}//result&nbsp;[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id":"1", "name":"abc", "desc":"some desc abc", "ID":"1", "mapField1":"field1 value",&nbsp; "mapField2":"field1 value"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id":"1", "name":"xyz", "desc":"some desc xyz", "ID":"1", "mapField1":"field1 value",&nbsp; "mapField2":"field1 value"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id":"2", "name":"pqr", "desc":"some desc pqr", "ID":"2", "mapField1":"field1 value 2",&nbsp; "mapField2":"field1 value2"&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;]

慕慕森

假设假设您的初始对象的类型Item如下所述。class Item {&nbsp; &nbsp; String fieldA,fieldB,fieldC;&nbsp; &nbsp; // Getters / Setter}假设您的初始列表Item被称为items假设您的初始地图被调用map并且是类型Map<String, Map<String,String>>解决方案向 Item 类添加一个方法以返回Map<String, String>对象的表示public Map<String, String> toMap() {&nbsp; &nbsp; return new HashMap<String, String>() {{&nbsp; &nbsp; &nbsp; &nbsp; put("fieldA", fieldA);&nbsp; &nbsp; &nbsp; &nbsp; put("fieldB", fieldB);&nbsp; &nbsp; &nbsp; &nbsp; put("fieldC", fieldC);&nbsp; &nbsp; }};}创建一个方法来获取fieldD给定fieldA值的值private String getFieldDValueForFieldAValue(String fieldAValue) {&nbsp; &nbsp; return map.get(fieldAValue).get("fieldD");}创建一种方法以将正确的fieldD值添加到项映射private Map<String, String> addFieldD(Map<String, String> itemMap) {&nbsp; &nbsp; itemMap.put("fieldD", getFieldDValueForFieldAValue(itemMap.get("fieldA")));&nbsp; &nbsp; return itemMap;}fieldD然后您可以为每个具有正确值的对象生成一个映射列表List<Map<String, String>> list = items.stream()&nbsp; &nbsp; .map(Item::toMap) // Converts all Items to Maps&nbsp; &nbsp; .map(m -> addFieldD(m)) // Adds the correct fieldD value to each Map&nbsp; &nbsp; .collect(Collectors.toList()); // make it a List最后用它制作一个 JSON 字符串:Type listType = new TypeToken<List<Map<String, String>>>() {}.getType();Gson gson = new Gson();String json = gson.toJson(list, listType);System.out.println(json);这将输出以下 JSON:[&nbsp; {"fieldA":"lookupval1","fieldC":"val3","fieldB":"val2","fieldD":"xxx"},&nbsp; {"fieldA":"lookupval1","fieldC":"val6","fieldB":"val5","fieldD":"xxx"},&nbsp; {"fieldA":"lookupval2","fieldC":"val9","fieldB":"val8","fieldD":"yyy"}]
随时随地看视频慕课网APP

相关分类

Java
我要回答