Example of distinct in Java 8
UPDATED: 09 October 2015
Tags:
Collection
,
List
,
Stream
,
stream-api
Stream<T> distinct()
Source code (Employee.java)
Source code
Output
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.
For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.
Source code (Employee.java)
public class Employee { private String Email; public String getEmail() { return Email; } public void setEmail(String Email) { this.Email = Email; } /** * You've to Override `equals` and `hashCode` methods in order to work `distinct` method of `stream`. */ @Override public boolean equals(Object obj) { Employee compareEmployee = (Employee) obj; if(this.Email != null && compareEmployee != null){ return this.Email.equalsIgnoreCase(compareEmployee.getEmail()); }else{ return false; } } @Override public int hashCode() { if(this.Email != null && !this.Email.trim().isEmpty()){ return this.Email.hashCode(); }else{ return -1; } } }
Source code
import java.util.ArrayList; import java.util.List; public class DistinctExample { public static void main(String[] args) { /* Create `List` of `String` and add duplicate records */ List<String> listStrings = new ArrayList<>(0); listStrings.add("Chirag"); listStrings.add("Vicky"); listStrings.add("Vicky"); System.out.println("Distinct Strings in `listStrings`: "); listStrings.stream() .distinct() .forEach(strName -> { System.out.println("> " + strName); }); /* Create `List` of `Long` and add duplicate records */ List<Long> listNumbers = new ArrayList<>(0); listNumbers.add(22L); listNumbers.add(22L); listNumbers.add(11L); System.out.println("Distinct Numbers in `listNumbers`: "); listNumbers.stream() .distinct() .forEach(number -> { System.out.println("> " + number); }); /** * Create `List` of `Employee` and add duplicate records * Note: In case of Bean or Pojo you've to Override `equals` and `hashCode` methods in Bean or Pojo */ List<Employee> listEmployee = new ArrayList<>(0); Employee employeeChirag = new Employee(); employeeChirag.setEmail("chirag.thakor@javaquery.com"); listEmployee.add(employeeChirag); Employee employeeVicky = new Employee(); employeeVicky.setEmail("vicky.thakor@javaquery.com"); listEmployee.add(employeeVicky); Employee employeeDuplicate = new Employee(); employeeDuplicate.setEmail("vicky.thakor@javaquery.com"); listEmployee.add(employeeDuplicate); System.out.println("Distinct Employee in `listEmployee`: "); listEmployee.stream() .distinct() .forEach(employee -> { System.out.println("> " + employee.getEmail()); }); } }
Output
Distinct Strings in `listStrings`: > Chirag > Vicky Distinct Numbers in `listNumbers`: > 22 > 11 Distinct Employee in `listEmployee`: > chirag.thakor@javaquery.com > vicky.thakor@javaquery.com
Tags:
Collection
,
List
,
Stream
,
stream-api
0 comments :