0
Why is the code not working
I was trying to add different friends for different persons from a HashSet. But the friends list is not changing, even when I am clearing the HashSet before reassigning it for different persons. The list is remaining the same for every person. Can anyone help? https://code.sololearn.com/c9M3erSOYsSn/?ref=app https://code.sololearn.com/c9M3erSOYsSn/?ref=app
5 Respostas
+ 1
It's clearing but instead clearing HashMap also and replacing last one, produced by while loop...
Instead of clearing statement, put this
HashSet<String> favPersons = new HashSet<>();
And remove previous declaration..
+ 1
Since it is a collection of elements it is not copying.. It pass refference.. So when you clear a HashMap, with all Refferences getting clear.. But with new refference, old one will not affect...
+ 1
You're welcome..
0
Jayakrishna Thanks its working. But how the hashmap was getting cleared and replaced when I was using the clear method on the hash set i.e. favPersons.clear()
0
Oh thanks I got it.