簡體   English   中英

我如何拆分我擁有的hashmap並獲取信息?

[英]How can I split the hashmap I have and get the information?

我有這樣的輸入 output。 這個output中的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)]
                        )

Class 內容如下:

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();

除了重構這個有點瘋狂的結構之外,您可以做的是遍歷列表並將所有元素放在一個公共的 map 中。然后按鍵訪問元素

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"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM