0

我有两个清单

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);
         }
    }

任何想法,对我来说都很棒......

谢谢普拉迪普

[编辑] 我的代码中的小改动请参见下面的完整代码

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);
            }
        }
4

3 回答 3

0

要找出集合中相同/不同的项目,我建议使用 Set 接口和标准集合操作,如联合、交集等。您可以使用 google Guava 库来帮助您:http ://code.google.com/p/番石榴库/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();

您不必遍历您的集合项目来找出您需要的内容。希望对以后的实施有帮助

于 2012-06-15T09:39:45.077 回答
0

快速查看您的代码后,尝试使用diffString.equals(selectedString)而不是diffString == selectedString.

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

于 2012-06-15T09:28:43.550 回答
0

==比较对象的引用而不是它们的内容。用于diffString.equals(selectedString)比较Strings。

于 2012-06-15T09:31:59.413 回答