简体   繁体   中英

In java, how can i search String array in another String array?

I have two different String array.

String[] str1={(ABC),(CDE),(DEF),(FGE),(ERT)};

String[] str2={(ABC),(FGE)};

I wanna know is str1 have str2's all members?How can i search str2 in str1?

Arrays.asList(str1).containsAll(Arrays.asList(str2));

Create one Set object for each of your arrays containing the elements of the array. Then use the Set class's containsAll method to check that the one Set contains all of the elements from the other Set.

http://download.oracle.com/javase/6/docs/api/java/util/Set.html

You could search each and every element of str2 in str1. Or, more efficiently: sort str1, and use binary search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM