Exploring Java 8 - lambda expressions

First, defining a "Person" class, that implements the "Comparable" interface:

/**
 *
 */
package com.obinna.tests.exploringjava8;

/**
 * @author obinnakalu
 *
 */
class Person implements Comparable<Person>
{
private String firstName;
private String lastName;
private int age;

/**
*
*/
public Person() {
super();
}

/**
* @param firstName
* @param lastName
* @param age
*/
public Person(String firstName, String lastName, int age)
{
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

/**
* @return the firstName
*/
public String getFirstName()
{
return firstName;
}

/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName)
{
this.firstName = firstName;
}

/**
* @return the lastName
*/
public String getLastName()
{
return lastName;
}

/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName)
{
this.lastName = lastName;
}

/**
* @return the age
*/
public int getAge()
{
return age;
}

/**
* @param age the age to set
*/
public void setAge(int age)
{
this.age = age;
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
return result;
}

/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Person other = (Person) obj;
if (age != other.age)
{
return false;
}
if (firstName == null)
{
if (other.firstName != null)
{
return false;
}
} else if (!firstName.equals(other.firstName))
{
return false;
}
if (lastName == null)
{
if (other.lastName != null)
{
return false;
}
} else if (!lastName.equals(other.lastName))
{
return false;
}
return true;
}
/*
@Override
public int compareTo(Object o)
{
int i;
if(this.getClass() == o.getClass())
{
Person p = (Person)o;
if(this.getAge() < p.getAge()) { i = -1; }
if(this.getAge() > p.getAge()) { i = 1; }
if(this.getAge() == p.getAge()) { i = 0; }
}
return i;
}
*/
@Override
public int compareTo(Person p)
{
/*
int i = 0;
if(this.getClass() == p.getClass())
{
if(this.getLastName().compareTo(p.getLastName()) == 1) { i = 1; }
if(this.getLastName().compareTo(p.getLastName()) == -1) { i = -1; }
if(this.getLastName().compareTo(p.getLastName()) == 0) { i = 0; }
//if(this.getAge() > p.getAge()) { i = -1; }
//if(this.getAge() == p.getAge()) { i = 0; }
}
*/
return (this.getLastName().compareTo(p.getLastName()));
}
}

Next, make a simple console App that uses the "Person" class in a List collection, and applying a few operations on it with lambda expressions:

/**
 *
 */
package com.obinna.tests.exploringjava8;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author obinnakalu
 *
 */
public class ExploringJava8App
{
private List<Person> people;

private void populateListOfPeople()
{
people.add(new Person("Obinna", "Kalu", 42));
people.add(new Person("James", "Dean", 71));
people.add(new Person("Mary", "Jane", 20));
people.add(new Person("Thomas", "Jefferson", 90));
people.add(new Person("Cynthia", "Bailey", 45));
}

private void printPeople()
{
Iterator<Person> it = people.iterator();
while(it.hasNext())
{
Person p = it.next();
System.out.println(p.getFirstName() + " " + p.getLastName() + " is " + p.getAge());
}
}

private void printPeopleWithLambda()
{
this.people.forEach(p -> System.out.println(p.getFirstName()));
}

private List<Person> filterYoungerThan50()
{
List l = people.stream().filter(x -> x.getAge() < 50).collect(Collectors.toList());
return l;
}

private List<Person> sort() // Sort
{
List l = people.stream().sorted().collect(Collectors.toList());
return l;
}

/**
* @param args
*/
public static void main(String[] args)
{
ExploringJava8App app = new ExploringJava8App();
app.people = new ArrayList<Person>();
app.populateListOfPeople();
app.printPeople();
System.out.println("\n======== List of under 50s: =========");
app.filterYoungerThan50().forEach(p -> System.out.println(p.getFirstName() + " " + p.getLastName() + " is " + p.getAge()));
System.out.println("\nPrint with lambda");
app.printPeopleWithLambda();
System.out.println("\nPrint sorted");
app.sort().forEach(p -> System.out.println(p.getFirstName() + " " + p.getLastName() + " is " + p.getAge()));;
}
}

Voila! A bit of toying with the new lambda expressions support in Java 8. Thank you.

Questions? Comments? Thoughts? Welcome!

Comments