比较,并在列表和数组的java删除元素不是present [英] Comparing and removing elements not present in a List and array java

查看:114
本文介绍了比较,并在列表和数组的java删除元素不是present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串阵列和一个列表<串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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆