时间复杂度:O(m+n)
package leetcode.arrayAndList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class CommentStr {
public static void main(String[] args) {
ArrayList ans = new ArrayList();
// 两个字符串数组
String[] array1 = {"apple", "banana", "orange", "grape"};
String[] array2 = {"orange", "grape", "watermelon", "kiwi"};
Set<String> mySet = new HashSet<>();
for (String str:array1){
mySet.add(str);
}
for (String str:array2){
if(mySet.contains(str)){
ans.add(str);
}
}
System.out.println(ans);
}
}
标签:java,String,hashset,str,ans,字符串,import,存到
From: https://www.cnblogs.com/chenyi502/p/17564172.html