如何检查ArrayList <string>是否包含字符串数组中的任何元素?

时间:2015-05-25 19:51:36

标签: java android arrays android-activity arraylist

在Android中,我想执行if语句来检查ArrayList是否包含字符串数组中的任何元素? e.g。

检查 singingGroup 中的任何元素是否也包含在 Winners []

String Winners[] = {"Jennifer", "Steven", "Peter", "Parker"};

ArrayList<String> singingGroup  = new ArrayList<String>();

singingGroup.add("Patrick");

singingGroup.add("Jane");

singingGroup.add("Joe");

singingGroup.add("Susan");

singingGroup.add("Amy");

我该怎么做? 因为我知道如何检查是否包含一个项目,如下面的另一个数组但如果有的话,则不存在于另一个

if (Arrays.asList(Winners).contains(singingGroup)) {

5 个答案:

答案 0 :(得分:5)

您可以使用

Collections.disjoint(singingGroup, Arrays.asList(Winners));

进行测试,是2个参数没有共同的共同元素。 (另见javadoc

结果的否定似乎是你正在寻找的。

答案 1 :(得分:1)

Collections.disjoint是存档的一种方法,但您也可以使用 retainAll()方法。

  

仅保留此列表中包含在指定集合中的元素(可选操作)。换句话说,从该列表中删除未包含在指定集合中的所有元素。

案例I:来自singingGroup的元素不包含在Winners []

String Winners[] = {"Jennifer", "Steven", "Peter", "Parker"};
ArrayList<String> singingGroup  = new ArrayList<String>();

singingGroup.add("Patrick");
singingGroup.add("Jane");
singingGroup.add("Joe");
singingGroup.add("Susan");
singingGroup.add("Amy");

List<String> WinnerList = new ArrayList<>(Arrays.asList(Winners));
WinnerList.retainAll(singingGroup);
System.out.println("retainList = " + WinnerList);

输出

list1 = []

案例II:来自singingGroup的元素也包含在Winners []

String Winners[] = {"Jennifer", "Steven", "Peter", "Parker"};
ArrayList<String> singingGroup  = new ArrayList<String>();

singingGroup.add("Steven");
singingGroup.add("Jane");
singingGroup.add("Joe");
singingGroup.add("Susan");
singingGroup.add("Jennifer");

List<String> WinnerList = new ArrayList<>(Arrays.asList(Winners));
WinnerList.retainAll(singingGroup);
System.out.println("retainList = " + WinnerList);

输出

retainList = [Jennifer, Steven]

答案 2 :(得分:0)

你也可以这样检查:

String Winners[] = {"Jennifer", "Patrick", "Peter", "Parker"};

        ArrayList<String> singingGroup  = new ArrayList<String>();

        singingGroup.add("Patrick");

        singingGroup.add("Jane");

        singingGroup.add("Joe");

        singingGroup.add("Susan");

        singingGroup.add("Amy");

        for(int i=0; i< Winners.length;i++)
        {
            if(singingGroup.contains(Winners[i]))
            {
                System.out.println("duplicate");
            }
        }

答案 3 :(得分:0)

您可以使用Apache Commons提供的CollectionUtils类。

使用交集方法(如果您想对公共元素执行某些操作,则非常有用):

Collection<String> intersection = CollectionUtils.intersection(singingGroup, Arrays.asList(Winners));

if (intersection.size() > 0){

    // At least one element contained in the intersection
}

或者,使用 containsAny 方法:

if (CollectionUtils.containsAny(singingGroup, Arrays.asList(Winners))){

    // True if at least one common element exists in both lists  
}

答案 4 :(得分:0)

//for loop would be perfect to check if element i = element i

int i =0;
int loopCount = 0;
while(loopCount < Winners.lenght)
{
  for(int i =0; i < singingGroup.length; i++)
  {
    if(Winners[loopCount] == singingGroup[i])
    {
    System.out.println(Winners[loopCount] + "is apart of the winners");
    }//end of comparing if

    if(i == singing.Group.length)
    { 
    loopCount ++;
    } //end of i == singingGroup

  }//end of for loop

}//end of while  loop

这不是最优化的代码,但如果您急需它,这将有效