任何与保留所有功能相反的功能的方法?

时间:2015-08-18 06:42:34

标签: java collections

我有两个列表,比如

List<String> list1 = new ArrayList<String>(Arrays.asList("A", "B", "C"));
List<String> list2 = new ArrayList<String>(Arrays.asList("A"));

List<String> result = list1.(SomeMethod)(list2)必须返回result = {"B","C"}

这种方法是否可用?

1 个答案:

答案 0 :(得分:13)

list1.removeAll(list2)

/**
 * Removes from this list all of its elements that are contained in the
 * specified collection (optional operation).
 *
 * @param c collection containing elements to be removed from this list
 * @return <tt>true</tt> if this list changed as a result of the call
 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
 *         is not supported by this list
 * @throws ClassCastException if the class of an element of this list
 *         is incompatible with the specified collection (optional)
 * @throws NullPointerException if this list contains a null element and the
 *         specified collection does not permit null elements (optional),
 *         or if the specified collection is null
 * @see #remove(Object)
 * @see #contains(Object)
 */
boolean removeAll(Collection<?> c);