Home > Net >  How can I split the hashmap I have and get the information?
How can I split the hashmap I have and get the information?

Time:02-01

I have an input output like this. A maximum of three elements can come in the targetInfo in this output. I want to get the first three elements in it. So I want to print the entertainment_interest_f, citizenship_s, tv_interest_f variables and values ​​to the screen. How can I do it?

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 content like this :

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

I was able to break the method down so much, can you help me for more?

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

CodePudding user response:

Apart of refactoring this little bit crazy structure you have, the thing you can do is to iterate over the list and put all elements in one common map. Then access elements by 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"));
  • Related