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

19th Apr 2020, 3:39 PM
Ayan
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..
19th Apr 2020, 9:38 PM
Jayakrishna 🇼🇳
+ 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...
20th Apr 2020, 6:04 AM
Jayakrishna 🇼🇳
+ 1
You're welcome..
20th Apr 2020, 6:05 AM
Jayakrishna 🇼🇳
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()
20th Apr 2020, 3:14 AM
Ayan
0
Oh thanks I got it.
20th Apr 2020, 6:05 AM
Ayan