As you can see the hashmap is creating many instances of itself and I am not sure why, this is the code for the class which I am using,
private Map<String, Integer> userAttempts;
private static final int MAX_USER_ATTEMPTS = 3;
public UserAttempts()
{
userAttempts = new HashMap<>();
}
public boolean isUserAttemptsAvaliable(String username)
{
if (!userAttempts.containsKey(username))
{
userAttempts.put(username, 0);
return true;
}
int attempts = userAttempts.get(username);
if (attempts != MAX_USER_ATTEMPTS)
{
userAttempts.replace(username, attempts++);
return true;
}
return false;
}
I am testing this code with jUnit tests, which looks like this,
@Test
public void testIsUserAttemptsAvaliable2()
{
attempts.isUserAttemptsAvaliable("Name");
assertTrue(attempts.isUserAttemptsAvaliable("Name"));
}
I am not sure why this is happening, any one have any idea ?
You're just confusing yourself in the debugger, basically.
A HashMap
has an entry set - and that entry set refers to the containing map. All you're doing in the debugger is navigating from the map to the entry set, back to the map, back to the entry set etc.
You could see exactly the same effect with your own class, like this:
public class Outer
{
private Inner inner = new Inner();
// Inner class, with implicit reference to "containing" Outer
public class Inner
{
}
}
See more on this question at Stackoverflow