Retriving a List of attribute (field) values from a Object List (Array List)
List users=new ArrayList<>(); List userIds=users .stream() .map(u->u.getId()) .collect(Collectors.toList());
Filter Objects by Attribute in a Array List
List users=new ArrayList<>(); List filteredUsers=users .stream() .filter(u->u.getId().equals(1l)) .collect(Collectors.toList());
Check if All Values of a Array List Matches a Cirtain Condition
checking if all the users in the list are having active statusList users=new ArrayList<>(); if(users.stream().allMatch(u->u.getStatus().equalsIgnoreCase("active"))){ //checking if all the users in the list are having active status }
Check if atleast one Value of a Array List Matches a Cirtain Condition
checking if atleast one user in the list has active statusList users=new ArrayList<>(); if(users.stream().anyMatch(u->u.getStatus().equalsIgnoreCase("active"))){ //checking if atleast one user in the list has active status }
Check if None of the Values of a Array List Matches a Cirtain Condition
checking none of the users in the list are having active statusList users=new ArrayList<>(); if(users.stream().noneMatch(u->u.getStatus().equalsIgnoreCase("active"))){ //checking none of the users in the list are having active status }
Filter Object Array List by Condition and Count Results
List users=new ArrayList<>(); Long count=users .stream() .filter(u->u.getStatus().equalsIgnoreCase("active")) .count();
Find the Firts matched result from Array List As a Optional Value
Listusers=new ArrayList<>(); Optional optionalUser=users .stream() .filter(u->u.getEmail().equalsIgnoreCase("abc@techbootstrap.com")) .findFirst(); optionalUser.ifPresent(u->{ //user presents with above condition, //do something with the value });
Most Used Java Stream Map Functions