Topic: 1z0-819 topic 1 question 170

Given:



Which two code fragments remove the elements from the original list? (Choose two.)

A.
B.
C.
D.

Re: 1z0-819 topic 1 question 170

Tested A C

Re: 1z0-819 topic 1 question 170

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.

Re: 1z0-819 topic 1 question 170

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

Re: 1z0-819 topic 1 question 170

I mean say B sorry big_smile

Re: 1z0-819 topic 1 question 170

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.

Re: 1z0-819 topic 1 question 170

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.

Re: 1z0-819 topic 1 question 170

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);
}
}

Re: 1z0-819 topic 1 question 170

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

Re: 1z0-819 topic 1 question 170

ArrayList & synchronizedList would throw a ConcurrentModificationException when removing elements while iterating over it.

Re: 1z0-819 topic 1 question 170

ArrayList not throw error, because is created a copy just for not throw ConcurrentModificationException, So is valid and remove elements for the original list

Re: 1z0-819 topic 1 question 170

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.

Re: 1z0-819 topic 1 question 170

ConcurrentLinkedQueue has a method remove that takes an object as argument!