我有一个可选的整数列表。我希望看看列表是否实际存在,然后将其转换为流。这样做的一种方法是遍历可选的整数列表ifPresent
Optional<List<Integer>> listOfNumbers = ...
if (listOfNumbers.isPresent()) {
listOfNumbers.get().stream();
}
但是,我不希望有如果条件。我搜索,发现ifPresent()
做同样的事情,但是当我做listOfNumbers.ifPresent(this::get))
,我得到以下错误:
non-static variable this cannot be referenced from a static context
能否请你帮我有效的方式做到这一点?这对我来说仍然是新的,所以如果我的理解有任何不正确的地方,请告诉我。
'listOfNumbers.orElse( Collections.emptyList())。stream()...'? –