You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Xeno Amess <xe...@gmail.com> on 2020/03/06 15:32:25 UTC

question about org.apache.commons.collections.list.SetUniqueList

Lets see the codes.

/**
 * Factory method to create a SetList using the supplied list to retain order.
 * <p>
 * If the list contains duplicates, these are removed (first indexed one kept).
 * A <code>HashSet</code> is used for the set behaviour.
 *
 * @param list  the list to decorate, must not be null
 * @throws IllegalArgumentException if list is null
 */
public static SetUniqueList decorate(List list) {
    if (list == null) {
        throw new IllegalArgumentException("List must not be null");
    }
    if (list.isEmpty()) {
        return new SetUniqueList(list, new HashSet());
    } else {
        List temp = new ArrayList(list);
        list.clear();
        SetUniqueList sl = new SetUniqueList(list, new HashSet());
        sl.addAll(temp);
        return sl;
    }
}

What confused me is that why we do not create a new List for empty List?

if (list.isEmpty()) {
    return new SetUniqueList(list, new HashSet());
} else {

What is the reason for doing this?