如何拆分我拥有的哈希图并获取信息?

问题描述 投票:0回答:1

我有这样的输入输出。此输出的 targetInfo 中最多可以包含三个元素。我想获取其中的前三个元素。所以我想将entertainment_interest_f、citizenship_s、tv_interest_f变量和值打印到屏幕上。我该怎么办?

CollectiveInterestsRes(
                            targetInfo=[{entertainment_interest_f=3.0}, {citizenship_s->America=3.0}, {tv_interest_f=3.0}],
                            msisdnCount=3,
                            msisdnList=[495012072622, 495012103468, 495012115405],
                            isSuccess=true,
                            ruleRequests=[RuleRequest(ruleId=tv, subruleId=null, locationRequest=null, operator=EQ, values=[1], msisdnRequest=null)]
                        )

课程内容如下:

public class CollectiveInterestsRes {
        private List<Map<String,String>> targetInfo;
    
        private Integer gsmNoCount;
        private List<String> gsmNoList;
        private boolean isSuccess;
        private List<RuleRequest> ruleRequests;
    }

我已经能把这个方法分解这么多了,你能帮我了解更多吗?

List<Map<String, String>> targetInfo = collectiveInterestsRes.get(0).getTargetInfo();
java hashmap
1个回答
0
投票

除了重构这个有点疯狂的结构之外,您可以做的就是迭代列表并将所有元素放入一个公共映射中。然后通过key访问元素

final List<Map<String, String>> targetInfo = collectiveInterestsRes.get(0).getTargetInfo();
final Map<String, String> accumulatedMap = new HashMap<>();

targetInfo.forEach(accumulatedMap::putAll);

System.out.println(accumulatedMap.get("tv_interest_f"));
System.out.println(accumulatedMap.get("entertainment_interest_f"));
© www.soinside.com 2019 - 2024. All rights reserved.