Java 8: Aggregate Operations: No more loops.

Aggregate Operations
Aggregate Operations

Java 8 introduced some cool solutions for traditional ways. Aggregate Operations is one of them. By the introduction of streams in Java 8, we got some great functional operations to use on collections and we can transform traditional loops to more concise and readable code.
Lets see one example.
Suppose that members of this social networking application are represented by the following Person class:

package com.finalhints;

import java.time.LocalDate;

public class Person {

	public enum Sex {
		MALE, FEMALE
	}

	String name;
	LocalDate birthday;
	Sex gender;
	String emailAddress;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public LocalDate getBirthday() {
		return birthday;
	}
	public void setBirthday(LocalDate birthday) {
		this.birthday = birthday;
	}
	public Sex getGender() {
		return gender;
	}
	public void setGender(Sex gender) {
		this.gender = gender;
	}
	public String getEmailAddress() {
		return emailAddress;
	}
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
}

Now if we want to find one Male from the list of person, traditional way of writing code is:

public Person getFirstMale()
{
	    for (Person p : person) {
                if (p.getGender().equals(Sex.MALE)) {
		            return p;
                }
            }
            return null;
}

Java 8 changed this traditional way to more efficient, small code with more readability. Solved the above loop in Java 8 by Stream API.

return person.stream()
             .filter(p -> p.getGender() == Person.Sex.MALE)
             .findFirst();

Its cool??? Right !!! Now what if we want to find the list? The following traditional way also can be converted to more readable code.

List<Person> malePerson = new ArrayList<Person>();
for (Person p : person){
if (p.getGender().equals(Sex.MALE)) {
	malePerson.add(p);
     }
}
return 	malePerson;

Solution using Java 8: stream and filter.

return person.stream()
	     .filter(p -> p.getGender() == Person.Sex.MALE)
	     .collect(Collectors.toList());

9 thoughts on “Java 8: Aggregate Operations: No more loops.

  • May 23, 2016 at 11:59 am
    Permalink

    hello there and thank you for your info ?I have definitely picked up anything new from right here. I did however expertise a few technical points using this web site, since I experienced to reload the site lots of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but slow loading instances times will very frequently affect your placement in google and could damage your high quality score if ads and marketing with Adwords. Well I adding this RSS to my email and can look out for much more of your respective interesting content. Make sure you update this again very soon..

    Reply
  • May 26, 2016 at 1:24 pm
    Permalink

    Iˇll right away grasp your rss as I can’t to find your e-mail subscription hyperlink or newsletter service. Do you have any? Kindly permit me recognize in order that I may just subscribe. Thanks.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *