0

I'm trying to add list object to hashmap

I tried with the following code didn't worked

public static Map<Long, List<Long>>  getCellAttributes(List<Vo> voList){

    LOG.info("VO: {}", Arrays.toString(VO.toArray()));

    Map<Long, List<Long>> atributesMap = new HashMap();

    List<Long> cellList = new ArrayList<>();

    for(: VOList){
        Long cID = VO.getID();
        Long cellUserNumber = VO.getCellNumber();
        if(cellMap.containsKey(ID)){
            cellList.add(cellNumber);
            cellmap.put(ID, list);
        }
        else {
            Map.put(ID, cellAtributesMap.get(ID).add(cellNumber));
        }
    }
    return cellMao;
}

Found below error for else block part.

Wrong 2nd argument type. Found: 'boolean', required:
1
  • can anyone please help me, the else block is making me crazy Commented Jul 14, 2019 at 2:38

3 Answers 3

2

Ok, first off, what is Map.put in the else block? To me it sounds wrong logically, probably you meant the case where the campaignId is not in the map yet. In this case, you can just:

else {
   List<Long> cellList = new ArrayList<>();
   cellList.add(cellUserNumber);
   cellAtributesMap.put(campaignId, cellList);
} 

Now the if block also looks logically wrong, there is no need to maintain a global list (what if the targetedOffersCampaignVOList is not ordered) and there is no need to put every time into the map.

Since it doesn't seem to be a homework for me, here is a better version:

Map<Long, List<Long>> cellAtributesMap = new HashMap();

// note, the following line is not required and should be removed
//List<Long> cellList = new ArrayList<>();

for(TargetedOffersCampaignVO targetedOffersCampaignVO: targetedOffersCampaignVOList){
    Long campaignId = targetedOffersCampaignVO.getCampaignID();
    Long cellUserNumber = targetedOffersCampaignVO.getCellUserNumber();
    if(cellAtributesMap.containsKey(campaignId)){
        // the list in the value already exists anyway, just add a new cell user number to it 
        cellAttributesMap.get(campaignId).add(cellUserNumber);
    }
    else {
        // create a new key-value pair in the result map
        // and add one element which is a current cellUserNumber to it
        List<Long> cellList = new ArrayList<>();
        cellList.add(cellUserNumber);
        cellAtributesMap.put(campaignId, cellList);
    }
}
return cellAtributesMap;
Sign up to request clarification or add additional context in comments.

2 Comments

@Bramik it worked, really thanks much for your response
You're welcome. Feel free accept the answer if you believe it helped to resolve the issue. Its a common practice here in Stack Overflow. Welcome to the community :)
0

I think you have problem in your else block:

else {
         Map.put(campaignId, cellAtributesMap.get(campaignId).add(cellUserNumber));
     }

Map call here is in inappropirate. If my guess is correct, you want to initialize a new array if that particular campaignId does not exist in previously, in that case you want to execute this else block. Then you just change your else block like this:

else
{
  List<Long> newList= new ArrayList<>();
  newList.add(cellUserNumber);
  cellAtributesMap.put("campaignId", newList);
}

Comments

-1
cellAtributesMap.get(campaignId).add(cellUserNumber)

Above method call returns a boolean value. Your else block should be like below

List<Long> list=new ArrayList<>();//any list implementation
list.add(cellUserNumber);
cellAtributesMap.put(campaignId,list);

3 Comments

@Thanks Girish for the response. Unfortunately you're response will not work for me because If I create new List in the else block the first value will save in the list it will only returns cellList value to hashmap for corresponding hashmap. below example 1. targetedOffersCampaignVO1.setCampaignID(2l); (this comes to else block for the first time) 2. then later it falls in if block for the same campaignID for different cellusernumber it will get looped and saves with only cellList not with arraylist object.
simple terms else block list object will get override by the if block cellList object
from your code what it looks is, you are adding the same value to all the key. is it what you are trying to achieve?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.