Use the distinct ()

You can use stream.distinct () to filter identical elements, for example:

Collection<String> list = Arrays.asList("A", "B", "C", "D", "A", "B", "C");
 
// Get collection without duplicate i.e. distinct only
List<String> distinctElements = list.stream().distinct().collect(Collectors.toList());
 
//Let's verify distinct elements
System.out.println(distinctElements);Copy the code

Filtering through the distinct() function is simple and easy, but in most cases, the objects you deal with are more complex, such as a variety of attributes, and rarely just native types and strings. We can customize an interface for Predicate to handle complex objects, such as:

public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
{
    Map<Object, Boolean> map = new ConcurrentHashMap<>();
    return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}Copy the code

This method is very simple, using ConcurrentHashMap to determine whether an element is contained that accepts a reference to a functional object. Let’s use this method

import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; public class JavaStreamDistinctExamples { public static void main(String[] args) { Person lokesh = new Person(1, "Lokesh", "Gupta"); Person brian = new Person(2, "Brian", "Clooney"); Person alex = new Person(3, "Alex", "Kolen"); //Add some random persons Collection<Person> list = Arrays.asList(lokesh,brian,alex,lokesh,brian,lokesh); // Get distinct only List<Person> distinctElements = list.stream().filter(distinctByKey(p -> p.getId())).collect(Collectors.toList()); // Let's verify distinct elements System.out.println(distinctElements); } public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) { Map<Object, Boolean> map = new ConcurrentHashMap<>(); return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; } } class Person { public Person(Integer id, String fname, String lname) { super(); this.id = id; this.fname = fname; this.lname = lname; } private Integer id; private String fname; private String lname; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } @Override public String toString() { return "Person [id=" + id + ", fname=" + fname + ", lname=" + lname + "]"; }}Copy the code

For those of you new to Java 8 functional programming

 List<Person> distinctElements = list.stream().filter(distinctByKey(p -> p.getId())).collect(Collectors.toList());Copy the code

It might be hard to understand, but it’s just shorthand for this

 List<Person> distinctElements = list.stream().filter(distinctByKey(new Function<Person, Object>() {
            @Override
            public Object apply(Person person) {
                return person.getId();
            }
        })).collect(Collectors.toList());Copy the code
Use Max () and min()

For Max and min, simply pass them a Comparator to compare and get the maximum and minimum values.

Get the maximum and minimum date
LocalDate start = LocalDate.now();
LocalDate end = LocalDate.now().plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
List<LocalDate> dates = Stream.iterate(start, date -> date.plusDays(1))
                        .limit(ChronoUnit.DAYS.between(start, end))
                        .collect(Collectors.toList());
 
// Get Min or Max Date
LocalDate maxDate = dates.stream().max( Comparator.comparing( LocalDate::toEpochDay ) ).get();
LocalDate minDate = dates.stream().min( Comparator.comparing( LocalDate::toEpochDay ) ).get();
 
System.out.println("maxDate = " + maxDate);
System.out.println("minDate = " + minDate); Copy the code
Get the maximum and minimum number
Integer maxNumber = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
                    .max(Comparator.comparing(Integer::valueOf)).get();
Integer minNumber = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
                    .min(Comparator.comparing(Integer::valueOf)).get();
 
System.out.println("maxNumber = " + maxNumber);
System.out.println("minNumber = " + minNumber);Copy the code
Gets the maximum char or String
String maxChar = Stream.of("H", "T", "D", "I", "J").max(Comparator.comparing(String::valueOf)).get();
String minChar = Stream.of("H", "T", "D", "I", "J").min(Comparator.comparing(String::valueOf)).get();
 
System.out.println("maxChar = " + maxChar);
System.out.println("minChar = " + minChar);Copy the code
Gets the largest and smallest objects
List<Employee> emps = new ArrayList<Employee>(); emps.add(new Employee(1, "Lokesh", 36)); emps.add(new Employee(2, "Alex", 46)); emps.add(new Employee(3, "Brian", 52)); Comparator<Employee> comparator = Comparator.comparing(Employee::getAge); // Get Min or Max Object Employee minObject = emps.stream().min(comparator).get(); Employee maxObject = emps.stream().max(comparator).get(); System.out.println("minObject = " + minObject); System.out.println("maxObject = " + maxObject); class Employee { private int id; private String name; private int age; public Employee(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { StringBuilder str = null; str = new StringBuilder(); str.append("Id:- " + getId() + " Name:- " + getName() + " Age:- " + getAge()); return str.toString(); }}Copy the code

Related articles

  • Introduction to the
  • Lambda expressions
  • Method references
  • The default method
  • Functional interface
  • Optional
  • Predicate
  • Introduction of the Stream
  • Stream examples, distinct, Max, min