Reputation: 4015
I am using a Map<Map, Boolean> in java and when I am trying to validate the Map with containsKey() method, it is returning always false, the inner Map always updates its keys & values. Here is the code which looks like similar to this
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ExampleMap
{
private static Map<Map<Long,Boolean>, Boolean> objectDetailsToSize= new HashMap<>();
public static void main(String[] args)
{
Random rn = new Random();
int size = 10;
Map<Long,Boolean> detailsMap = new HashMap<>();
for(int i=0; i< size; i++)
{
detailsMap.put(rn.nextLong(),rn.nextBoolean());
if(!objectDetailsToSize.containsKey(detailsMap))
{
objectDetailsToSize.put(detailsMap, rn.nextBoolean());
System.out.println("Why containsKey() method is failing here");
}
}
}
}
Why Map's containsKey() method is always failing here?
Upvotes: 0
Views: 64
Reputation: 198023
You must make a new detailsMap
-- or copy your existing one -- every time through the loop. Right now, you're modifying the map that you've already put in.
Upvotes: 0