Note: question ask which code fragments remove the elements from the "original list".
answer is None.
if question would has been which code compiles, then answer is A and C.
because
"for-each" loop use Iterator, and with regular list, we get : ConcurrentModificationException
A : ConcurrentLinkedQueue is ok because it will never throw any ConcurrentModificationException, even if you remove or add item. Note that those are all in java.util.concurrent package.
Only correct is A,
ConcurrentLinkedQueue and CopyOnWriteArrayList=> WORK A COPY not reference to list so elemments in original list are not removed
Collections.synchronizedList => thows excpetion
answer: A and maybe D
A : ConcurrentLinkedQueue is ok because it will never throw any ConcurrentModificationException, even if you remove or add item. Note that those are all in java.util.concurrent package.
B: For-each loops are not appropriate when you want to modify the ArrayList. Iterator should be used.
C: is not good.
cwa - new collection with new copy of the elements. So, it doesn't remove anything from the original.
D: looks like it can work if put synchronized(al) before loop. Should be checked.
A is correct, can modified original list if you create other arraylist from the original list.
D: only work if create a copy of syncronizedliss as follow:
List<Integer> sl = Collections.synchronizedList(list);
synchronized (sl) {
List<Integer> copyList = new ArrayList<>(sl);
for (Integer i : copyList) {
sl.remove(i);
}
}
or else thown exception.
Tested AC. With a lot of modification...:
import java.util.*;
import java.util.concurrent.*;
public class q170 {
public static void main(String[] args) {
List<Integer> original = new ArrayList<>(Arrays.asList(1,2,3,4,5));
System.out.println(original);
Queue<Integer> clq = new ConcurrentLinkedQueue<>(original);
for (Integer w : clq) {clq.remove(w);}
System.out.println(clq);
//List<Integer> al = new ArrayList<>(original);
//for (Integer w : al) {al.remove(w);}
//System.out.println(al);
List<Integer> cwa = new CopyOnWriteArrayList<>(original);
for (Integer w : cwa) {cwa.remove(w);}
System.out.println(cwa);
//List<Integer> sl = Collections.synchronizedList(original);
//for (Integer w : sl) {sl.remove(w);}
//System.out.println(sl);
}
}
THat is incorect you print cwa in line CopyOnWriteArrayList ...
must be print original for know if list original is empty.
if you print original
System.out.println(cwa); =>[]
System.out.println(original);=> [1, 2, 3, 4, 5]
then option With concurrent-queue-arrayslist work in a copy not in the original list.
SO THAT OPTIONS ARE INVALID for remove items from original list
ArrayList not throw error, because is created a copy just for not throw ConcurrentModificationException, So is valid and remove elements for the original list
C creates a new CopyOnWriteArrayList from the original list and then iterates over it, removing each element. The CopyOnWriteArrayList class is thread-safe and allows for concurrent modification, so it is safe to remove elements while iterating over it.
Option A is incorrect because ConcurrentLinkedQueue does not have a remove method that takes an object as an argument.
ConcurrentLinkedQueue has a method remove that takes an object as argument!
upvoted 1 times
...
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
aruni_mishra
7 months agocathDev
9 months, 4 weeks agoASPushkin
1 year agod7bb0b2
1 year agod7bb0b2
1 year agoASPushkin
1 year, 2 months agod7bb0b2
1 year agoOmnisumem
1 year, 3 months agod7bb0b2
1 year ago[Removed]
1 year, 5 months agod7bb0b2
1 year agoStavok
1 year, 6 months agod7bb0b2
1 year ago