public static void main(String[] args) {
String a = "1,2";
String b = "3";
String c = "1,2";
List<String> aList = Arrays.asList(a.split(","));
List<String> bList = Arrays.asList(b.split(","));
List<String> cList = Arrays.asList(c.split(","));
List<String> result = findCommonInArrays(Arrays.asList(aList,bList,cList));
System.out.print(result);
}
/**
* 获取在所有集合出现过的元素
* @param lists 多个集合
* @return List<String>
*/
public static List<String> getListIntersection (List<List<String>> lists) {
if (lists.size() == 1) {
return lists.get(0);
}
//调用Arrays.asList()生产的List,是由Arrays.asList()返回的Arrays的内部类ArrayList,而不是java.util.ArrayList
List list = new ArrayList(lists.get(0));
for (int i = 1; i < lists.size(); i++) {
List<String> temp = lists.get(i);
list.retainAll(temp);
}
return list;
}
获取在所有集合中出现过的元素(交集)
最新推荐文章于 2023-03-04 11:03:40 发布