In this article, we will discuss Stream’s distinct() method in details with examples
1. Stream distinct() method :
- This Stream method is an intermediate operation which reads given stream and returns new stream of distinct elements thereby removing repeated/duplicate elements
- distinct() method takes no-arguments
- Stream’s distinct() method is stateful which means it is interfering with other elements in the stream to eliminate duplicate elements
- Another intermediate operation like map(), sorted(), filter(), etc. can be applied to returned stream
- Note :- Number of elements returned in the new stream after applying distinct method is either equal or less than the original stream elements
- Method signature :- Stream<T> distinct()
2. Stream distinct() method examples :
2.1 To remove repeated/duplicate elements present
- First list contains integer numbers with duplicates
- Second list contains String elements with duplicates
- We are going to use distinct method of Stream API to remove duplicates and finally printing to console
package net.bench.resources.stream.distinct.example;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamDistinctMethod {
public static void main(String[] args) {
// 1. list of integers
List<Integer> numbers = Arrays.asList(
1,
2,2,
3,3,3,
4,4,4,4,
5,5,5,5,5
);
// collect distinct numbers
List<Integer> distinctNumbers = numbers
.stream()
.distinct()
.collect(Collectors.toList());
// print to console
System.out.println("1. Distinct numbers are :- \n");
distinctNumbers.forEach(number -> System.out.println(number));
// get count of distinct elements
long numberCount = numbers.stream().distinct().count();
// print to console
System.out.println("\ncount of distinct Numbers = "
+ numberCount);
// 2. list of Strings
List<String> sectors = Arrays.asList(
"Motor",
"Power",
"Steel",
"Chemicals",
"Chemicals",
"Power",
"Steel",
"Capital",
"TCS",
"Motor",
"Power",
"Communications"
);
// collect distinct sectors
Set<String> distinctSectors = sectors
.stream()
.distinct()
.collect(Collectors.toSet());
// print to console
System.out.println("\n\n2. Distinct sectors are :- \n");
distinctSectors.forEach(System.out::println);
// get count of distinct sectors
long sectorCount = sectors.stream().distinct().count();
// print to console
System.out.println("\ncount of distinct Sectors = "
+ sectorCount);
}
}
Output:
1. Distinct numbers are :-
1
2
3
4
5
count of distinct Numbers = 5
2. Distinct sectors are :-
Motor
TCS
Communications
Capital
Steel
Power
Chemicals
count of distinct Sectors = 7
2.2 Filter distinct elements
- First list contains integer numbers with duplicates, in this list we are removing duplicates using distinct() method and applying filter condition to get only distinct odd numbers using filter() method i.e.; Lambda expression
- Second list contains String elements with duplicates, in this list we are removing duplicates using distinct() method and applying filter condition to get distinct Strings which starts with alphabet capital ‘C’ using filter() method i.e.; Lambda expression
- Finally, printing to console using Stream’s forEach() method
package net.bench.resources.stream.distinct.example;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamDistinctFilterMethod {
public static void main(String[] args) {
// 1. list of integers
List<Integer> numbers = Arrays.asList(
1,
2,2,
3,3,3,
4,4,4,4,
5,5,5,5,5
);
// collect distinct elements and filter ODD numbers
List<Integer> distinctNumbers = numbers
.stream()
.distinct()
.filter(num -> num%2 != 0)
.collect(Collectors.toList());
// print to console
System.out.println("1. Distinct ODD numbers are :- \n");
distinctNumbers.forEach(number -> System.out.println(number));
// 2. list of Strings
List<String> sectors = Arrays.asList(
"Motor",
"Power",
"Steel",
"Chemicals",
"Chemicals",
"Power",
"Steel",
"Capital",
"TCS",
"Motor",
"Power",
"Communications"
);
// collect distinct sectors
Set<String> distinctSectors = sectors
.stream()
.distinct()
.filter(str -> str.startsWith("C"))
.collect(Collectors.toSet());
// print to console
System.out.println("\n\n2. Distinct sectors "
+ "which starts with alphabet 'C' :- \n");
distinctSectors.forEach(System.out::println);
}
}
Output:
1. Distinct ODD numbers are :-
1
3
5
2. Distinct sectors which starts with alphabet 'C' :-
Communications
Capital
Chemicals
2.3 Sort distinct elements
- First list contains integer numbers in random order with duplicates, in this list we are removing duplicates using distinct() method and sorting numbers in reverse order using sorted() method i.e.; Lambda expression
- Second list contains String elements in random order with duplicates, in this list we are removing duplicates using distinct() method and sorting in natural alphabetical order using sorted() method i.e.; Lambda expression
- Finally, printing to console using Stream’s forEach() method
package net.bench.resources.stream.distinct.example;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class StreamDistinctSortedMethod {
public static void main(String[] args) {
// 1. list of integers
List<Integer> numbers = Arrays.asList(
3,3,3,
5,5,5,5,5,
2,2,
1,
4,4,4,4
);
// collect distinct elements and sort in REVERSE order
List<Integer> distinctNumbers = numbers
.stream()
.distinct()
.sorted(Comparator.reverseOrder()) // reverse order
.collect(Collectors.toList());
// print to console
System.out.println("1. Reverse sorted distinct numbers :- \n");
distinctNumbers.forEach(number -> System.out.println(number));
// 2. list of Strings
List<String> sectors = Arrays.asList(
"Motor",
"Power",
"Steel",
"Chemicals",
"Chemicals",
"Power",
"Steel",
"Capital",
"TCS",
"Motor",
"Power",
"Communications"
);
// collect distinct sectors and sort in NATURAL order
List<String> distinctSectors = sectors
.stream()
.distinct()
.sorted() // natural alphabetical order
.collect(Collectors.toList());
// print to console
System.out.println("\n\n2. Distinct sectors in "
+ "natural alphabetical order :- \n");
distinctSectors.forEach(System.out::println);
}
}
Output:
1. Reverse sorted distinct numbers :-
5
4
3
2
1
2. Distinct sectors in natural alphabetical order :-
Capital
Chemicals
Communications
Motor
Power
Steel
TCS
2.4 Map to UpperCase after getting distinct elements
- A list contains String elements with duplicates, in this list we are removing duplicate elements using distinct() method and converting to upper case using map(String::toUpperCase) method
- Finally, printing to console using Stream’s forEach() method
package net.bench.resources.stream.distinct.example;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamDistinctMapMethod {
public static void main(String[] args) {
// list of Strings
List<String> sectors = Arrays.asList(
"Motor",
"Power",
"Steel",
"Chemicals",
"Chemicals",
"Power",
"Steel",
"Capital",
"TCS",
"Motor",
"Power",
"Communications"
);
// collect distinct sectors and map to upper Case
List<String> distinctSectors = sectors
.stream()
.distinct() // removes duplicates
.map(String::toUpperCase) // converts to upper case
.collect(Collectors.toList());
// print to console
System.out.println("Distinct sectors in upper case :- \n");
distinctSectors.forEach(System.out::println);
}
}
Output:
Distinct sectors in upper case :-
MOTOR
POWER
STEEL
CHEMICALS
CAPITAL
TCS
COMMUNICATIONS
2.5 altogether – distinct(), filter(), sort() and map()
- A list contains String elements with duplicates in random order, in this list we are removing duplicate elements using distinct() method and filtering String starting with alphabet capital ‘C’ using filter() method and sorting in natural alphabetical order using sorted() method and converting to lower case using map(String::toLowerCase) method
- Finally, printing to console using Stream’s forEach() method
package net.bench.resources.stream.distinct.example;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamDistinctFilterSortMapMethod {
public static void main(String[] args) {
// list of Strings
List<String> sectors = Arrays.asList(
"Motor",
"Power",
"Steel",
"Chemicals",
"Chemicals",
"Power",
"Steel",
"Capital",
"TCS",
"Motor",
"Power",
"Communications"
);
// collect distinct sectors and filter, sort and map to lower Case
List<String> distinctSectors = sectors
.stream()
.distinct() // removes duplicates
.filter(str -> str.startsWith("C")) // filter
.sorted() // natural alphabetical order
.map(String::toLowerCase) // converts to lower case
.collect(Collectors.toList());
// print to console
System.out.println("Sectors with distinct, filtered, sorted and mapped :- \n");
distinctSectors.forEach(System.out::println);
}
}
Output:
Sectors with distinct, filtered, sorted and mapped :-
capital
chemicals
communications
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
Happy Coding !!
Happy Learning !!