The distinct method is an intermediate operation that filters the stream and allows you to use only distict values ββ(by default using the Object :: equals method) to advance to the next operation.
I wrote an example below for your case,
// Create the list with duplicates. List<String> listAll = Arrays.asList("CO2", "CH4", "SO2", "CO2", "CH4", "SO2", "CO2", "CH4", "SO2"); // Create a list with the distinct elements using stream. List<String> listDistinct = listAll.stream().distinct().collect(Collectors.toList()); // Display them to terminal using stream::collect with a build in Collector. String collectAll = listAll.stream().collect(Collectors.joining(", ")); System.out.println(collectAll); //=> CO2, CH4, SO2, CO2, CH4 etc.. String collectDistinct = listDistinct.stream().collect(Collectors.joining(", ")); System.out.println(collectDistinct); //=> CO2, CH4, SO2
George Siggouroglou Nov 16 '15 at 12:36 2015-11-16 12:36
source share