So I managed to solve problems like this by making a copy which seems easier, but I wanted to know other ways without using a copy and not getting the ConcurrentModificationException.
I've already solved it using a copy, but It would be appreciated if I could get comments with hints, or dm a solution with an explanation of how to go about doing it without making a copy.
OnigiriByte
Level 16
Is there a solution without making a copy of the HashMap?
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
31 January 2019, 21:10
However, you can solve it using recursion. I gave this code to someone a month or so back:
+7
Darko Jakimovski
1 February 2019, 12:09
(!test.contains(pair.getValue())) always returns true, so it just adds the values from the map to the empty ArraList :/ I don't quite get how and why this works. Care to elaborate?
0
Guadalupe Gagnon
1 February 2019, 14:18
Actually the ' ! ' on that line before test.contains() will make whatever is returned (which will be false until a name repeats) to true. So when a name is not contained in the list, it adds the name. It continues doing this until a name repeats, then the else block is executed.
The else block first removes the repeat names, then it passes the map (which has been modified) to the same method removeFirstNameDuplicates(). When this occurs a completely new instance of the method is run with a blank ArrayList and the process starts over again. This will continue until no more names are repeat, in which case the method just ends naturally.
When the method ends naturally, which is called the base case, it passes back to the calling code, which is in the middle of the else block where the method was called. The very next line breaks that current method and it gets passed up again to its calling code. This repeats as many times as necessary all the way back to main. You can copy this code into your IDE and run it. I'll message you some code with sys.out statements so you can see output of what is going on, to get a better understanding.
+2
Don Boody
2 May 2019, 11:26
This example is so helpful even with the other tasks, thank you.
+1
Matt Duemler
26 July 2019, 15:30
Thank you. I've been hunting through the help section as I flounder through this particular set of problems. This explanation finally makes at least this problem make sense...
0
John
8 August 2019, 19:53
OK, I think I understand how removeFirstNameDuplicates() works, but please verify if my understanding is correct.
ArrayList test is created to hold the first names from map. It starts out empty.
The for loop start stepping through map, adding each first name to test until the first duplicate is found.
Then, the else statement calls removeItemFromMapByValue(), which removes every instance in map the has a value matching the first duplicated first name. A revised version of map is returned.
Now, removeFirstNameDuplicates() is called again with this new version of map, a new empty ArrayList test is created, and the process starts again, and is repeated until all duplicates are removed.
+1
Guadalupe Gagnon
15 August 2019, 20:58
That is exactly it. The only piece that you didn't mention is that when no first name duplicates are found the method will end and then pass back up, where the next line is break so that it doesn't continue iterating through a bad map.
--------------------------------------------------------------------------------------------------------------------------
level 1: RFNBD(map) {// finds a first name duplicate and calls method again}
level 2: RFNBD(map){// finds a first name duplicate and calls method again}
level 3: RFNBD(map) {// no duplicates are found, for loop goes to end and method ends}
level 2: // the line directly following the method call is executed (line 9 break)
level 1: //the line directly following the method call is executed (line 9 break)
--------------------------------------------------------------------------------------------------------------------------
So, along with exactly what you said, the sequence of events is like the diagram above.
0
Guadalupe Gagnon
29 January 2019, 14:16
Copy is the best way to solve this. Look at the method that is provided in the start code, it even makes a copy.
+3