檢查2個 Set 是否為子集合, 程式碼範例:
Set<Integer> a_set= new HashSet<>();
a_set.add(1);
a_set.add(2);
Set<Integer> b_set= new HashSet<>();
b_set.add(2);
boolean result = a_set.containsAll(b_set);
System.out.println("subset of Numbers? " + result);
輸出為 True.
類似程式碼:
Set<Integer> a_set= new HashSet<>();
a_set.add(1);
a_set.add(2);
Set<Integer> b_set= new HashSet<>();
b_set.add(2);
Set<Object> intersection = a_set.stream()
.distinct()
.filter(b_set::contains)
.collect(Collectors.toSet());
System.out.println("subset:"+ !intersection.isEmpty());