3.9K Star 22.6K Fork 7.8K

GVPdromara / hutool

 / 详情

调用 Collection<T> filter(Collection<T> collection, Editor<T> editor) 异常 java.lang.UnsupportedOperationException

已完成
创建于  
2017-10-30 10:32

调用clear报错,传入对象是List类型.

	/**
	 * 过滤<br>
	 * 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
	 * 
	 * <pre>
	 * 1、过滤出需要的对象,如果返回null表示这个元素对象抛弃
	 * 2、修改元素对象,返回集合中为修改后的对象
	 * </pre>
	 * 
	 * @param <T> 集合元素类型
	 * @param collection 集合
	 * @param editor 编辑器接口
	 * @return 过滤后的数组
	 */
	public static <T> Collection<T> filter(Collection<T> collection, Editor<T> editor) {
		Collection<T> collection2 = ObjectUtil.clone(collection);
		collection2.clear();

		T modified;
		for (T t : collection) {
			modified = editor.edit(t);
			if (null != modified) {
				collection2.add(t);
			}
		}
		return collection2;
	}

jdk8 源码注释!

   /**
     * Removes all of the elements from this collection (optional operation).
     * The collection will be empty after this method returns.
     *
     * @throws UnsupportedOperationException if the <tt>clear</tt> operation
     *         is not supported by this collection
     */
    void clear();
java.util.Collection#clear

评论 (3)

skygongle 创建了任务
Looly 添加了标签invalid
Looly 删除了标签invalid
Looly 添加了标签enhancement

我这边测试没有问题:

ArrayList<String> list = CollUtil.newArrayList("a", "b", "c");

Collection<String> filtered = CollUtil.filter(list, new Editor<String>() {
	@Override
	public String edit(String t) {
		return t +1;
	}});

Console.log(filtered);

看样子你传入的List很有可能是个UnmodifiedList,这种不可变的List并不支持此方法。后续版本会改进这里。

Looly 关闭了任务
Looly 添加了标签bug

Java集合操作asList方法产生的List对象不可改变

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
1463 loolly admin 1578914022 126263 skygongle 1578918721
Java
1
https://gitee.com/dromara/hutool.git
git@gitee.com:dromara/hutool.git
dromara
hutool
hutool

搜索帮助