stream.distinct() method to filter or collect all distinct elements from a collection,
distinct() uses hashCode() and equals() methods to get distinct elements.
Hence our class must implement hashCode() and equals() methods.
Stream.distinct() method declaration:
Stream distinct()
In bellow example we have list of string data type tat contains duplicate elements:
Sample Example.java
Used distinct() with List of Objects:
In bellow example,class will be overried the hashCode() and equals() methods.
import java.util.ArrayList;
import java.util.List;
public class DistinctWithUserObjects {
public static void main(String[] args) {
List list = new ArrayList<>();
{
list.add(new PersonInfo("Ramesh", 20));
list.add(new PersonInfo("Ramesh", 20));
list.add(new PersonInfo("Komal Kumar", 15));
list.add(new PersonInfo("Kiran Kumar", 30));
list.add(new PersonInfo("Kiran Kumar", 30));
}
long l = list.stream().distinct().count();
System.out.println("No. of distinct books:"+l);
list.stream().distinct().forEach(b ->
System.out.println(b.getPerson()+ "," + b.getAge()));
}
}
public class PersonInfo {
private String person;
private int age;
public PersonInfo(String person, int age) {
this.person = person;
this.age = age;
}
public String getPerson() {
return person;
}
public int getAge() {
return age;
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
final PersonInfo personInfo = (PersonInfo) obj;
if (this == personInfo) {
return true;
} else {
return (this.person.equals(personInfo.age) && this.age == personInfo.age);
}
}
@Override
public int hashCode() {
int hashno = 7;
hashno = 13 * hashno + (person == null ? 0 : person.hashCode());
return hashno;
}
}
Distinct by attribute and Property
distinct() does not provide distinct elements by property or key. It works on the basis of hashCode() and equals().
If we want distinct element by a property or key, we can achieve it by a work around code.
static Predicate distinctByKey(Function keyExtractor) {
Map
return t -> apple.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
The above code can be used with Stream filter() method as follows.
list.stream().filter(distinctByKey(b -> b.getName()));
distinct() uses hashCode() and equals() methods to get distinct elements.
Hence our class must implement hashCode() and equals() methods.
Stream.distinct() method declaration:
Stream
In bellow example we have list of string data type tat contains duplicate elements:
Sample Example.java
In bellow example,class will be overried the hashCode() and equals() methods.
import java.util.ArrayList;
import java.util.List;
public class DistinctWithUserObjects {
public static void main(String[] args) {
List
{
list.add(new PersonInfo("Ramesh", 20));
list.add(new PersonInfo("Ramesh", 20));
list.add(new PersonInfo("Komal Kumar", 15));
list.add(new PersonInfo("Kiran Kumar", 30));
list.add(new PersonInfo("Kiran Kumar", 30));
}
long l = list.stream().distinct().count();
System.out.println("No. of distinct books:"+l);
list.stream().distinct().forEach(b ->
System.out.println(b.getPerson()+ "," + b.getAge()));
}
}
public class PersonInfo {
private String person;
private int age;
public PersonInfo(String person, int age) {
this.person = person;
this.age = age;
}
public String getPerson() {
return person;
}
public int getAge() {
return age;
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
final PersonInfo personInfo = (PersonInfo) obj;
if (this == personInfo) {
return true;
} else {
return (this.person.equals(personInfo.age) && this.age == personInfo.age);
}
}
@Override
public int hashCode() {
int hashno = 7;
hashno = 13 * hashno + (person == null ? 0 : person.hashCode());
return hashno;
}
}
distinct() does not provide distinct elements by property or key. It works on the basis of hashCode() and equals().
If we want distinct element by a property or key, we can achieve it by a work around code.
static
Map
return t -> apple.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
The above code can be used with Stream filter() method as follows.
list.stream().filter(distinctByKey(b -> b.getName()));