Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Arraylist which i would be using to retrieve data but also there are multiple fields which are duplicates and after adding to hashmap it replaces the values.

Below is the arraylist and also output is mentioned below.

What would be the code which will provide the same output as given below.

Input:

[{AttributeValue=[{AttributeValue=2000.000000, AttributeName=accountBalance}, {AttributeValue=[{AttributeValue=51.200000, AttributeName=accountBalance}, {AttributeValue=146, AttributeName=dedicatedAccountID}, {AttributeValue=200.000000, AttributeName=accountBalance}, {AttributeValue=121, AttributeName=dedicatedAccountID}]{ AttributeName=dedicatedAccounts}, {AttributeValue=Offer HashMap, AttributeName=offers}, {AttributeValue=30, AttributeName=permanentServiceClass}, {AttributeValue=0, AttributeName=serviceOffering}], AttributeName=accountInformationAfterRefill}, {AttributeValue=[{AttributeValue=0.000000, AttributeName=accountBalance}, {AttributeValue=[{AttributeValue=10.000000, AttributeName=accountBalance}, {AttributeValue=121, AttributeName=dedicatedAccountID}], AttributeName=dedicatedAccounts}], AttributeName=accountInformationBeforeRefill}, {AttributeValue=30, AttributeName=currentServiceClass}, {AttributeValue=nftvtm5, AttributeName=originHostName}, {AttributeValue=EXT, AttributeName=originNodeType}, {AttributeValue=520541945, AttributeName=originTransactionID}, {AttributeValue=2384142050058, AttributeName=subscriberNumber}, {AttributeValue=2000.000000, AttributeName=transactionAmount}]

Output:

In Array list,

accountBalance = 2000.000000,permanentServiceClass = 30,accountBalance = 0.000000,currentServiceClass = 30,transactionAmount = 2000.000000

I had tried with hashmap but after that it replaces the accountBalance value. Then made other hashmap which will have only values but if i would do like this then have to create multiple hashmaps as the fields are not fixed. Is there anyway to get the output.


What I have tried:

if (tempMap.get("AttributeValue").getClass().isInstance(new String())) {
    String attribval = (String) tempMap.get("AttributeValue");
    String attribName = (String) tempMap.get("AttributeName");
    if (attribName.equalsIgnoreCase("accountBalance")) {
        newarray.add(attribval);
    } else {
        smVariable.put(tempMap.get("AttributeName"),
                tempMap.get("Att‌​ributeValue"));
    }
}
Posted
Updated 15-Feb-17 17:23pm
v2
Comments
Richard MacCutchan 16-Feb-17 3:51am    
You need to use a different list type to filter out the duplicates as you read them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900