This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ConcurrentModificationProblem implements ConcurrentModificationSolvable { | |
public final ArrayList<String> experimentData() { | |
ArrayList<String> roundList = new ArrayList<>(); | |
roundList.add("A"); | |
roundList.add("B"); | |
roundList.add("C"); | |
roundList.add("D"); | |
return roundList; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ConcurrentModificationSolution extends ConcurrentModificationProblem { | |
final void solve() { | |
ArrayList<String> roundList = experimentData(); | |
String newItem = "C"; | |
System.out.println("************************** Solve by " + solutionName()); | |
System.out.println("Before : " + roundList); | |
try { | |
long start = System.currentTimeMillis(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SolveByCopyOnWriteArrayList extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundItem, String newItem) { | |
List<String> copyRoundItem = new CopyOnWriteArrayList<>(roundItem); | |
for(String item: copyRoundItem) { | |
if(item.equals(newItem)) { | |
roundItem.remove(item); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SolveByCopyToRemove extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundList, String newItem) { | |
ArrayList<String> copy = new ArrayList<>(); | |
for (String item : roundList) { | |
if (item.equals(newItem)) { | |
copy.add(item); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SolveByInnosense extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundList, String newItem) { | |
for (String item : roundList) { | |
if (item.equals(newItem)) { | |
roundList.remove(item); | |
roundList.add(0, newItem); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SolveBySomeSense extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundList, String newItem) { | |
for (String item : roundList) { | |
if (item.equals(newItem)) { | |
roundList.remove(item); | |
roundList.add(0, newItem); | |
break; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SolveByIterator extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundItem, String newItem) { | |
Iterator iterator = roundItem.iterator(); | |
while(iterator.hasNext()) { | |
String item = (String) iterator.next(); | |
if(item.equals(newItem)) { | |
iterator.remove(); | |
break; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SolveByJava8RemoveIf extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundList, String newItem) { | |
roundList.removeIf(item -> item.equals(newItem)); | |
roundList.add(0, newItem); | |
} | |
@Override | |
public String solutionName() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.util.stream.Collectors.toList; | |
public class SolveByJava8Stream extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundList, String newItem) { | |
roundList = roundList | |
.stream() | |
.filter(item -> !item.equals(newItem)) | |
.map(Object::toString) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.util.stream.Collectors.toList; | |
public class SolveByJava8Stream extends ConcurrentModificationSolution { | |
@Override | |
public void addItemAndReIndex(List<String> roundList, String newItem) { | |
System.out.println("Before roundListIdentity=" + System.identityHashCode(roundList)); | |
roundList = roundList | |
.stream() | |
.filter(item -> !item.equals(newItem)) |
OlderNewer