有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有哪种方法的功能与retain all相反?

我有两个清单,比如

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) 个答案

  1. # 1 楼答案

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