Tan*_*ant 8 java list set set-intersection java-8
虽然在for循环中很容易实现,但在Java-8中是否有办法查找列表L中的所有元素是否都存在于Set中s?
Jac*_* G. 12
Stream当你可以使用时,没有必要使用Set#containsAll:
var set = Set.of(1, 2, 3, 4, 5);
var list = List.of(2, 3, 4);
System.out.println(set.containsAll(list));
Run Code Online (Sandbox Code Playgroud)
输出:
true
Run Code Online (Sandbox Code Playgroud)
你可以使用allMatch:
boolean result = l.stream().allMatch(s::contains);
Run Code Online (Sandbox Code Playgroud)