比较,并在列表和数组的java删除元素不是present [英] Comparing and removing elements not present in a List and array java
问题描述
我有一个字符串
阵列和一个列表<串GT;
。我想要做的是使用该变量具有较大规模和使用,作为值去除较小的变量的基础。我也想获得更大尺寸的变量未在其他present的值。需要注意的是,为什么两个变量的数据类型不同的原因是因为的String []组
变量是从JSP页面和列表℃的复选框组;字符串> existingGroup
是从数据库中一个结果。例如:
I have one String
array and one List<String>
. What I want to do is to use the variable with a larger size and use that as the basis of the values removal of the smaller variable. I also want to get the values of the larger sized variable not present in the other. Note that the reason why the two variables differ on datatype is because the String[] group
variable is a checkbox group from a jsp page and the List<String> existingGroup
is a ResultSet from the database. For example:
的String []组
包含:
Apple
Banana
Juice
Beef
列表&LT;串GT; existingGroup
包含:
Apple
Beef
Lasagna
Flower
Lychee
此外,由于这两个变量的大小而变化,它仍然应该正确删除值。
And since the size of the two variables vary, it should still correctly remove the values.
我至今是
if(groupId.length >= existingGroup.size()) {
for(int i = 0; i < groupId.length; i++) {
if(! existingGroup.contains(groupId[i])) {
if(existingGroup.get(existingGroup.indexOf(groupId[i])) != null) {
// I'm unsure if I'm doing this right
}
}
}
} else {
for(int i = 0; i < existingGroup.size(); i++) {
// ??
}
}
感谢。
推荐答案
您可以使用列表
接口提供的方法。
You can use the methods the List
interface provides.
list.removeAll(Arrays.asList(array)); // Differences removed
或
list.retainAll(Arrays.asList(array)); // Same elements retained
根据您的需求
这篇关于比较,并在列表和数组的java删除元素不是present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!