博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java列表removeAll(Collection)示例
阅读量:2531 次
发布时间:2019-05-11

本文共 5280 字,大约阅读时间需要 17 分钟。

Java List removeAll() method removes all of its elements that are also present in the given list. The method throws UnsupportedOperationException if the operation is not supported by the list.

Java List removeAll()方法将删除其所有在给定列表中也存在的元素。 如果列表不支持该操作,则该方法将引发UnsupportedOperationException。

If the given collection is null, is thrown.

如果给定的集合为null,则抛出 。

This method returns true if the list is changed, otherwise false.

如果更改列表,则此方法返回true ,否则返回false

Java列表removeAll()示例 (Java List removeAll() Examples)

Let’s look at some examples of removeAll() method with different types of list implementations.

让我们看一下带有不同类型的列表实现的removeAll()方法的一些示例。

1. ArrayList removeAll()示例 (1. ArrayList removeAll() Example)

List
list = new ArrayList<>();list.add("A");list.add("B");list.add("C");list.add("C");list.add("B");list.add("A");System.out.println(list);List
removeList = List.of("A", "B");boolean isRemoved = list.removeAll(removeList);System.out.println(list);System.out.println(isRemoved);

Output:

输出:

[A, B, C, C, B, A][C, C]true

2. LinkedList removeAll()示例 (2. LinkedList removeAll() Example)

List
linkedList = new LinkedList<>();linkedList.add(1);linkedList.add(2);linkedList.add(3);System.out.println(linkedList);boolean flag = linkedList.removeAll(List.of(1, 2));System.out.println(linkedList);System.out.println(flag);

Output:

输出:

[1, 2, 3][3]true

3.列出removeAll()UnsupportedOperationException (3. List removeAll() UnsupportedOperationException)

If we invoke removeAll() method on an Unmodifiable list, we will get UnsupportedOperationException.

如果我们在U​​nmodifiable列表上调用removeAll()方法,则将获得UnsupportedOperationException。

List.of() method returns an Unmodifiable list.

List.of()方法返回一个不可修改的列表。

jshell> List
list = List.of(1, 2);list ==> [1, 2]jshell> list.removeAll(List.of(1));| Exception java.lang.UnsupportedOperationException| at ImmutableCollections.uoe (ImmutableCollections.java:72)| at ImmutableCollections$AbstractImmutableCollection.removeAll (ImmutableCollections.java:80)| at (#67:1)jshell>
Java List RemoveAll UnsupportedOperationException

Java List removeAll() UnsupportedOperationException

Java列表removeAll()UnsupportedOperationException

4.列出removeAll()NullPointerException (4. List removeAll() NullPointerException)

jshell> List
list = new ArrayList<>();list ==> []jshell> list.removeAll(null);| Exception java.lang.NullPointerException| at Objects.requireNonNull (Objects.java:221)| at ArrayList.batchRemove (ArrayList.java:847)| at ArrayList.removeAll (ArrayList.java:822)| at (#71:1)jshell>
Java List RemoveAll NullPointerException

Java List removeAll() NullPointerException

Java列表removeAll()NullPointerException

5. Java列表removeAll()不起作用 (5. Java List removeAll() Not Working)

If you look at the implementation of removeAll() method in ArrayList/LinkedList, it uses following methods internally.

如果查看ArrayList / LinkedList中的removeAll()方法的实现,则它在内部使用以下方法。

removeAll() -> contains() -> indexOf() -> indexOfRange() -> equals()

So, it’s necessary that the list elements have proper implementation of equals() and hashCode() methods. Otherwise, you will get unwanted results.

因此,列表元素必须具有equals()和hashCode()方法的正确实现。 否则,您将得到不想要的结果。

Here is an example where the equals() and hashCode() method is not implemented for the List elements and the removeAll() operation is not working as expected.

这是一个示例,其中未对List元素实现equals()和hashCode()方法,并且removeAll()操作未按预期工作。

package com.journaldev.java;import java.util.ArrayList;import java.util.List;public class ArrayListRemoveAll {	public static void main(String[] args) {		List
list = new ArrayList<>(); list.add(new Record(1, "Hi")); list.add(new Record(2, "Hello")); list.add(new Record(3, "Howdy")); System.out.println("Original List =" + list); list.removeAll(List.of(new Record(1, "Hi"), new Record(2, "Hello"))); System.out.println("Updated List =" + list); }}class Record { private int id; private String data; Record(int i, String d) { this.id = i; this.data = d; } @Override public String toString() { return String.format("R{%d, %s}", this.id, this.data); }}

Output:

输出:

Original List =[R{1, Hi}, R{2, Hello}, R{3, Howdy}]Updated List =[R{1, Hi}, R{2, Hello}, R{3, Howdy}]

It seems that the removeAll() method is not working. Now let’s add method implementations to the Record class.

看来removeAll()方法不起作用。 现在让我们将方法实现添加到Record类。

@Overridepublic int hashCode() {	final int prime = 31;	int result = 1;	result = prime * result + ((data == null) ? 0 : data.hashCode());	result = prime * result + id;	return result;}@Overridepublic boolean equals(Object obj) {	if (this == obj)		return true;	if (obj == null)		return false;	if (getClass() != obj.getClass())		return false;	Record other = (Record) obj;	if (data == null) {		if (other.data != null)			return false;	} else if (!data.equals(other.data))		return false;	if (id != other.id)		return false;	return true;}

Updated Output:

更新的输出:

Original List =[R{1, Hi}, R{2, Hello}, R{3, Howdy}]Updated List =[R{3, Howdy}]

Now the removeAll() method is working as we expected. So whenever you feel that the removeAll() operation is not working as expected, check the equals() and hashCode() implementations in the list elements class.

现在removeAll()方法正在按预期工作。 因此,每当您感到removeAll()操作未按预期工作时,请检查list elements类中的equals()和hashCode()实现。

参考资料 (References)

翻译自:

转载地址:http://pymzd.baihongyu.com/

你可能感兴趣的文章
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>