Java – Does the Java collection method retainAll() guarantee that the order of the list it modifies is preserved?

Does the Java collection method retainAll() guarantee that the order of the list it modifies is preserved?… here is a solution to the problem.

Does the Java collection method retainAll() guarantee that the order of the list it modifies is preserved?

Does it depend on which collection object is using retainAll? I’m using a list of strings.

I’ve done quite a bit of searching and found a bunch of tips here and elsewhere on the net that should keep the order of the list unchanged, but no clear direct answers. If already answered, point in the right direction.

I even found a code example that implements the retainAll function, which indicates that the order has not been modified. But as I understand it, these examples may not exactly be how the standard methods are coded in the downloaded java package. From my own tests using the method, it seems to indicate that it remains orderly as well. But I want to be more sure, as the tests I’m doing rely specifically on keeping an ordered list.

Most precisely, I want this function to be real and reliable.

 /**
 * This function takes the expected list and verifies it is contained within the actual list, in the expected order
 * @param expectedList {@link List} List ordered as it should be to pass criteria
 * @param actualList {@link List} List ordered as it exists in the product
 * @return {@link Boolean} returns true when the expectedList is ordered within the actualList correctly
 */
static boolean verifyOrder(List<String> expectedList, List<String> actualList)
{
    boolean ordered = false

if (expectedList == actualList)
        ordered = true
    else
    {
        actualList.retainAll(expectedList)
        if (actualList == expectedList)
            ordered = true
    }
    return ordered
}

Solution

As a reference, suppose the call is:

list.retainAll(coll)

Otherwise, the

problem is meaningless, because list is the collection that has been modified, and it is a collection in the specified order.

Is the Java collection method retainAll() guaranteed to leave the order of the list it modifies unaltered?

The specification explicitly states that it removes elements from the list that are not in the coll, so the order issue depends on the >remove method specifying the preservation order.

Since you say “keep the order of the list, then the answer is: yes

But you also say “yes Java collection method retainAll()”, so if list is not List but some other collection type, then the answer is no, because not all collection types guarantee order.

Can it depend on which collection object is using the retainAll?

Since list is the one that uses the method, then: No
Well, let’s say it’s List, but someone has already answered.

If you intend to refer to the coll as “the one that is using the method”, then: No
However, performance is affected by the coll collection type because the implementation uses coll.contains() to check for matches, so if the coll is a set.

Related Problems and Solutions