简体   繁体   English

CollectionUtils.retainAll方法不起作用(Java)

[英]CollectionUtils.retainAll method doesn't work (Java)

I have two ArrayLists. 我有两个ArrayLists。 I want to compare them, to get the number of common elements. 我想将它们进行比较,以获取常见元素的数量。 Or just a list with the common elements. 或只是带有常见元素的列表。

When I try to do this with CollectionUtils.retainAll, it doesn't know this method I think? 当我尝试使用CollectionUtils.retainAll进行此操作时,我不知道这种方法吗?

It says it cannot find the symbol 'Variable CollectionUtils', so it also doesn't know the method? 它说找不到符号'Variable CollectionUtils',所以也不知道该方法吗? Or what do I wrong? 还是我错了?

My code: 我的代码:

ArrayList<String> lijst1 = new ArrayList<String>(Arrays.asList(inhoud1.split("AT")));
ArrayList<String> lijst2 = new ArrayList<String>(Arrays.asList(inhoud2.split("AT")));

ArrayList matches = CollectionUtils.retainAll(lijst1,lijst2);

Sorry if it's something easy. 抱歉,这很简单。 It probably will be. 可能会的。

The CollectionUtils class is a part of the Apache Commons Collections library, and is not part of the Java API, hence unavailable by default. CollectionUtils类是Apache Commons Collections库的一部分,并且不是Java API的一部分,因此默认情况下不可用。 Include the library as a JAR on your classpath, or use Maven to add it, then import org.apache.commons.collections4.CollectionUtils . 将库作为JAR包含在类路径中,或使用Maven添加它,然后导入org.apache.commons.collections4.CollectionUtils

You can also use lijst1.retainAll(lijst2) , but you'll have to use it slightly differently, since ArrayList#retainAll acts by side-effects (that is, it will update lijst1 instead of returning a new collection. 您还可以使用lijst1.retainAll(lijst2) ,但是您必须使用稍有不同的方法,因为ArrayList#retainAll具有副作用(也就是说,它将更新lijst1而不是返回新集合)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM