Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
添加 CollectionsUtil.remove(Collection<O>, O...) 支持动态数组 fix #600
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed Dec 29, 2017
1 parent 87c2009 commit 7590c94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/feilong/core/util/CollectionsUtil.java
Expand Up @@ -586,14 +586,14 @@ public static <O> List<O> removeAll(Collection<O> objectCollection,Collection<O>

//---------------------------------------------------------------
/**
* 从 <code>objectCollection</code>中 删除<code>removeElement</code> <span style="color:red">(原集合对象不变)</span>.
* 从 <code>objectCollection</code>中 删除<code>removeElements</code> <span style="color:red">(原集合对象不变)</span>.
*
* <h3>说明:</h3>
* <blockquote>
* <ol>
* <li>返回剩余的集合 <span style="color:red">(原集合对象不变)</span>,这个方法非常有用,如果你不想修改 <code>collection</code>的话,不能调用
* <code>collection.remove(removeElement);</code>.</li>
* <li>底层实现是调用的 {@link ListUtils#removeAll(Collection, Collection)},将不是<code>removeElement</code> 的元素加入到新的list返回.</li>
* <code>collection.remove(removeElements);</code>.</li>
* <li>底层实现是调用的 {@link ListUtils#removeAll(Collection, Collection)},将不是<code>removeElements</code> 的元素加入到新的list返回.</li>
* </ol>
* </blockquote>
*
Expand Down Expand Up @@ -634,7 +634,7 @@ public static <O> List<O> removeAll(Collection<O> objectCollection,Collection<O>
* the generic type
* @param objectCollection
* the object collection
* @param removeElement
* @param removeElements
* 需要被删除的元素
* @return a <code>List</code> containing all the elements of <code>c</code> except any elements that also occur in <code>remove</code>.
* @throws NullPointerException
Expand All @@ -643,9 +643,11 @@ public static <O> List<O> removeAll(Collection<O> objectCollection,Collection<O>
* @see ListUtils#removeAll(Collection, Collection)
* @since Commons Collections 4
* @since 1.0.8
* @since 1.10.7 change param to Varargs
*/
public static <O> List<O> remove(Collection<O> objectCollection,O removeElement){
return removeAll(objectCollection, toList(removeElement));
@SafeVarargs
public static <O> List<O> remove(Collection<O> objectCollection,O...removeElements){
return removeAll(objectCollection, toList(removeElements));
}

//---------------------------------------------------------------
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package com.feilong.core.util.collectionsutiltest;

import static com.feilong.core.bean.ConvertUtil.toList;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
Expand All @@ -26,19 +27,21 @@

import com.feilong.core.util.CollectionsUtil;

import static com.feilong.core.bean.ConvertUtil.toList;

/**
* The Class CollectionsUtilRemoveElementTest.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
*/
public class RemoveElementTest{

//**************CollectionsUtil.remove(Collection<String>, String)*************************
/**
* Test remove.
*/
@Test
public void testRemove1(){
List<String> list = toList("xinge", "feilong1", "feilong2", "feilong3", "feilong2");

List<String> removeList = CollectionsUtil.remove(list, "feilong2", "feilong3");

assertThat(removeList, hasItems("xinge", "feilong1"));
assertThat(list, hasItems("xinge", "feilong1", "feilong2", "feilong3"));
}

@Test
public void testRemove(){

Expand Down

0 comments on commit 7590c94

Please sign in to comment.