集合在列表中搜索

时间:2012-06-15 09:22:13

标签: java collections core

我有两个列表

Collection<String> listOne = new ArrayList<String>();
Collection<String> listTwo = new ArrayList<String>();

for(PermissionDataObject permissionDataObject : selectedPermissions)
  listOne.add(permissionDataObject.getPermissionName());        

for(String selectedPermissions : selectedList)
  listTwo.add(selectedPermissions);

Collection<String> similarPermissions = new HashSet<String>(listOne);
Collection<String> differentPermissions = new HashSet<String>();

differentPermissions.addAll(listOne);
differentPermissions.addAll(listTwo);

similarPermissions.retainAll(listTwo);
differentPermissions.removeAll(similarPermissions);

System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similarPermissions, differentPermissions);

输出

One:[MENU_CREATION, ROLE_CREATION]
Two:[CREATE_BRANCH, CREATE_COUNTRY, MENU_CREATION, ROLE_CREATION]
Similar:[ROLE_CREATION, MENU_CREATION]
Different:[CREATE_BRANCH, CREATE_COUNTRY]

上面的代码完全正常,我有不同的收藏

但我想检查listTwo和differentPermissions,如果不同,listTwo等于doSomething如果不同而listTwo不等于doSomthing

我有下面的代码,当不同的一个值工作时,如果不同的值有更多的值,那么循环就会分解

for (Iterator<String> it = differentPermissions.iterator(); it.hasNext();){
    String diffString = it.next();
    String selectedString = selectedList.listIterator().next();

    log.info("---diffString---" + diffString);
    log.info("---selectedString---" + selectedString);

    if(diffString == selectedString){
         System.out.println("----permission added----" + diffString);
    }else{
         System.out.println("----permission deleted----" + diffString);
         }
    }

任何想法,对我都很好......

感谢Pradeep

[编辑] 我的代码中的小变化见下面的完整代码

Collection<String> listOne = Arrays.asList("MENU_CREATION", "ROLE_CREATION");
        Collection<String> listTwo = Arrays.asList("CREATE_BRANCH", "CREATE_COUNTRY", "MENU_CREATION", "ROLE_CREATION");

        Collection<String> similar = new HashSet<String>(listOne);
        Collection<String> different = new HashSet<String>();

        different.addAll(listOne);
        different.addAll(listTwo);

        similar.retainAll(listTwo);
        different.removeAll(similar);

         System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similar, different);

        for (Iterator<String> it = different.iterator(); it.hasNext();) {

            String diffString = it.next();
            String selectedString = listTwo.iterator().next();

            if (diffString.equals(selectedString)) {
                System.out.println("----permission added----" + diffString);
            } else {
                System.out.println("----permission deleted----" + diffString);
            }
        }

3 个答案:

答案 0 :(得分:0)

快速查看代码后,请尝试使用diffString.equals(selectedString)代替diffString == selectedString

然后,对于你的问题,我真的不明白你想要比较什么。您希望列表完全匹配,还是要检查列表中具有相同索引的项目是否匹配?

答案 1 :(得分:0)

==比较对象的引用而不是它们的内容。使用diffString.equals(selectedString)来比较String s。

答案 2 :(得分:0)

要在集合中找出相同/不同的项目,我建议使用Set接口和标准集合操作,如union,intersection等。 您可以使用Google Guava库来帮助您:http://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained

Set<String> wordsWithPrimeLength = ImmutableSet.of("one", "two", "three", "six", "seven", "eight");
Set<String> primes = ImmutableSet.of("two", "three", "five", "seven");

SetView<String> intersection = Sets.intersection(primes, wordsWithPrimeLength); // contains "two", "three", "seven"
// I can use intersection as a Set directly, but copying it can be more efficient if I use it a lot.
return intersection.immutableCopy();

您不必遍历集合项目以找出所需内容。希望有助于未来的实施