1、取交集(取兩個集合中都存在的元素)
HashSet<String> setA = new HashSet<>();
HashSet<String> setB = new HashSet<>();
//用于存放結果
HashSet<String> resSet = new HashSet<>();
resSet.addAll(setA);
resSet.retainAll(setB);
return resSet;
2、取差集(取存在一個集合中,但不存在于另外一個集合中的元素)
HashSet<String> setA = new HashSet<>();
HashSet<String> setB = new HashSet<>();
//用于存放結果
HashSet<String> resSet = new HashSet<>();
resSet.addAll(setA);
resSet.removeAll(setB);
return resSet;
3、取交集(取兩個集合中全部的元素,這個很簡單,都把他們添加進去就行)
HashSet<String> setA = new HashSet<>();
HashSet<String> setB = new HashSet<>();
//用于存放結果
HashSet<String> resSet = new HashSet<>();
resSet.addAll(setA);
resSet.addAll(setB);
return resSet;
[ 版權聲明 ]:
本文所有權歸作者本人,文中參考的部分已經做了标記!
商業用途轉載請聯系作者授權!
非商業用途轉載,請标明本文連結及出處!