periback2
periback2

Reputation: 1469

Map.containsValue() is returning false when value is is in the map

I'm trying to check when a value is in the map, but I don't know why containsValue() isn't checking it correctly.

Here's my code:

    Map<String, String> usersMap = userAPI.getBuyersFromBUUPToRootMap(getParentBusinessUnitId());
    //usersMap's [STDOUT] {super, root (rootSuper) - [EZMRootCmp]=3008, user, buyer A (buyer1) - [EZMBuyer]=1007, user, Obsvr (observer) - [EZMBuyer]=1006, user, Root (rootTest) - [EZMRootCmp]=3001, user, Test (test) - [EZMBuyer]=1001, zabuhab, Miguel (buyerAdmin1) - [EZMBuyer]=1008}

    System.out.println(usersMap.values()); 
    //[STDOUT] [3008, 1007, 1006, 3001, 1001, 1008]

    System.out.println(usersMap.containsValue(getUserProfile().getUserId())); //[STDOUT] false
    //getUserProfile().getUserId() [STDOUT] 1008

As you can see, getUserProfile().getUserId(), whose value is 1008 is in the Map, but it returns false.

Do you know why it's happening and how can I fix this situation? I tried to add a Long.valueOf(getUserProfile().getUserId()); but it keeps returning false

PS: I searched in SO in questions like this, but It didn't help me for this situation

Below, my full code:

    UserAPI userAPI = ServiceLocator.getService(UserAPI.class);
    Map<String, String> usersMap = userAPI.getBuyersFromBUUPToRootMap(getParentBusinessUnitId());
    System.out.println(usersMap);
    System.out.println("#######################");
    System.out.println(usersMap.values());
    System.out.println("##########valor para getAuctionHeaderDO().getPrimaryContact().getId() #############");
    System.out.println(usersMap.containsValue(getAuctionHeaderDO().getPrimaryContact().getId()));
    System.out.println("##########valor para getUserProfile().getUserId()#############");
    System.out.println(getUserProfile().getUserId());
    System.out.println(usersMap.containsValue(getUserProfile().getUserId()));

And the result:

[STDOUT] {super, root (rootSuper) - [EZMRootCmp]=3008, user, buyer A (buyer1) - [EZMBuyer]=1007, user, Obsvr (observer) - [EZMBuyer]=1006, user, Root (rootTest) - [EZMRootCmp]=3001, user, Test (test) - [EZMBuyer]=1001, zabuhab, Miguel (buyerAdmin1) - [EZMBuyer]=1008}

[STDOUT] #######################

[STDOUT] [3008, 1007, 1006, 3001, 1001, 1008]

[STDOUT] ##########valor para getAuctionHeaderDO().getPrimaryContact().getId() #############

[STDOUT] false

[STDOUT] ##########valor para getUserProfile().getUserId()#############

[STDOUT] 1008

[STDOUT] false

Upvotes: 2

Views: 6551

Answers (2)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41220

usersMap<String,String> is a map of String key and String value. And I think getAuctionHeaderDO().getPrimaryContact().getId() return integer or long. Map#containsValue(Object) accepts any object value but could not able to find it because values are not available in same type.


A small derivation -

Map<String,String> map= new HashMap<String, String>();
map.put("10", "1000");
map.put("11", "1001");
map.put("12", "1002");
map.put("13", "1003");
System.out.println(map.containsValue(1002));

It prints false.


You should find the value as String, then you will able to get the desired result.

System.out.println(map.containsValue(String.valueOf(1002)));

It will print true.

Upvotes: 10

ThanksForAllTheFish
ThanksForAllTheFish

Reputation: 7251

Your map is declared as

Map<String, String> usersMap = ...;

so the value must be a String. Then, I guess getUserProfile().getUserId() returns an Integer or an int. Try to build a String from this value and check the contains method again.

Upvotes: 4

Related Questions