当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Collections Search in List

问题描述:

i have two list

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

output is

One:[MENU_CREATION, ROLE_CREATION]

Two:[CREATE_BRANCH, CREATE_COUNTRY, MENU_CREATION, ROLE_CREATION]

Similar:[ROLE_CREATION, MENU_CREATION]

Different:[CREATE_BRANCH, CREATE_COUNTRY]

the above code is perfectly working i got different collection

but i want to check the listTwo and differentPermissions, if different and listTwo is equals doSomething if different and listTwo is not equals doSomthing

i have below code, when different has one value its working, if different has more values the loop is break up

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

}

}

any ideas, is great to me...

Thanks Pradeep

[EDIT]

Small change in my code see below full code

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

}

}

网友答案:

After a quick look at your code, try to use diffString.equals(selectedString) instead of diffString == selectedString.

Then, for your problem, I don't really understand what you want to compare. Do you want the lists to match perfectly, or you want to check if the items of your list that have the same index match?

网友答案:

== compares the references of the objects and not their content. Use diffString.equals(selectedString) in order to compare Strings.

网友答案:

To find out same/different items in a collections I would recommend using Set interface with standard set operations like union, intersection etc. You can use google Guava library to help you with that: 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();

You won't have to iterate over your collections items to find out what you need. Hope that helps in the future implementations

分享给朋友:
您可能感兴趣的文章:
随机阅读: