/** * 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 <E> the element type * @param list the list to decorate, must not be null * @return a new {@link SetUniqueList} * @throws NullPointerException if list is null * @since 4.0 */ public static <E> SetUniqueList<E> setUniqueList(final List<E> list) { if (list == null) { throw new NullPointerException("List must not be null"); } if (list.isEmpty()) { return new SetUniqueList<>(list, new HashSet<E>()); } final List<E> temp = new ArrayList<>(list); list.clear(); final SetUniqueList<E> sl = new SetUniqueList<>(list, new HashSet<E>()); sl.addAll(temp); return sl; }
/** * {@inheritDoc} * <p> * NOTE: from 4.0, an unmodifiable list will be returned, as changes to the * subList can invalidate the parent list. */ @Override public List<E> subList(final int fromIndex, final int toIndex) { final List<E> superSubList = super.subList(fromIndex, toIndex); final Set<E> subSet = createSetBasedOnList(set, superSubList); return ListUtils.unmodifiableList(new SetUniqueList<>(superSubList, subSet)); }