distinct()
Java中IntStream类中的方法返回一个由该流的不同元素组成的流。
语法如下
IntStream distinct()
假设我们在流中包含以下元素。其中一些重复
IntStream intStream = IntStream.of(10, 20, 30, 20, 10, 50, 80, 90, 100, 80);
若要获取不同的元素,请使用IntStreamdistinct()
方法。
以下是distinct()
在Java中实现IntStream方法的示例
import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { IntStream intStream = IntStream.of(10, 20, 30, 20, 10, 50, 80, 90, 100, 80); System.out.println("显示不同的元素:"); intStream.distinct().forEach(System.out::println); } }
输出结果
显示不同的元素: 10 20 30 50 80 90 100