In this article, I am going to showcase you how to use Java 8 stream findAny() and findFirst(), first create a user class something like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.aem.toolkit.core.models; public class User { public User(String name, int age, String role) { this.name = name; this.age = age; this.role = role; } private String name; 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; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } private int age; private String role; } |
Step 2: create user collection
1 2 3 4 5 6 7 8 9 10 11 12 13 | static List<User> users = new ArrayList<User>(); public static void main(String[] args) { // TODO Auto-generated method stub users.add(new User("subscriber1", 25, "author")); users.add(new User("subscriber2", 25, "author")); users.add(new User("subscriber3", 25, "Contributor")); users.add(new User("subscriber4", 25, "Admin")); users.add(new User("subscriber5", 25, "author")); getFirstRecord(); findAnyRecord(); } |
Step 3: Java Stream findFirst()
The below code first applies filter which is user role equal to author, once it returns stream object then it gets the first object
1 2 3 4 5 6 7 8 9 10 11 | public static void getFirstRecord() { Optional<User> user = users.stream() .filter(u -> u.getRole().equals("author")) .findFirst(); if (user.isPresent()) { System.out.println(user.get().getName()); } } |
Step 3: Java Stream findAny()
The below code first applies filter which is user role equal to Contributor, once it returns stream object then it gets any matching object, this is something like contains method in the ArrayList
1 2 3 4 5 6 7 8 9 10 11 | public static void findAnyRecord() { Optional<User> user = users.stream() .filter(u -> u.getRole().equals("Contributor")) .findAny(); if (user.isPresent()) { System.out.println(user.get().getName()); } } |
Final code and output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.aem.toolkit.core.models; import java.util.ArrayList; import java.util.List; import java.util.Optional; public class UserHelper { static List<User> users = new ArrayList<User>(); public static void main(String[] args) { // TODO Auto-generated method stub users.add(new User("subscriber1", 25, "author")); users.add(new User("subscriber2", 25, "author")); users.add(new User("subscriber3", 25, "Contributor")); users.add(new User("subscriber4", 25, "Admin")); users.add(new User("subscriber5", 25, "author")); getFirstRecord(); findAnyRecord(); } public static void getFirstRecord() { Optional<User> user = users.stream() .filter(u -> u.getRole().equals("author")) .findFirst(); if (user.isPresent()) { System.out.println("First matching record is :" + user.get().getName()); } } public static void findAnyRecord() { Optional<User> user = users.stream() .filter(u -> u.getRole().equals("Contributor")) .findAny(); if (user.isPresent()) { System.out.println("Contributor record is :" + user.get().getName()); } } } |
Output
1 2 | First matching record is :subscriber1 Contributor record is :subscriber3 |