Created
February 21, 2014 15:21
-
-
Save spencerwi/9136092 to your computer and use it in GitHub Desktop.
Quick demo playing with Java 8 streams, lambdas, and aggregate operations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.stream.*; | |
import java.util.Comparator; | |
import java.util.Collections; | |
import java.util.Optional; | |
import java.util.Random; | |
class Java8Demo { | |
private static final String[] poolOfFirstNames = { "Joe", "Paul", "John", "Samantha", "Amanda", "Karen" }; | |
private static final String[] poolOfLastNames = {"Smith", "Lee", "Patel", "Hernandez"}; | |
public static void main(String[] args){ | |
List<Person> people = Stream.of(1, 2, 3, 4, 5) | |
.parallel() | |
.map(i -> new Person(getRandomFirstName(), getRandomLastName(), new Random().nextInt(100))) | |
.collect(Collectors.toList()); | |
System.out.println("People: "); | |
people.forEach(p -> System.out.println("\t" + p)); | |
System.out.println(); | |
System.out.println("People between the age of 20 and 40, sorted by Lastname, then by Firstname, then by age: "); | |
people.stream() | |
.parallel() | |
.filter(person -> (person.getAge() >= 20 && person.getAge() <= 40)) | |
.sequential() | |
.sorted( | |
Comparator.comparing(Person::getLastName) | |
.thenComparing(Person::getFirstName) | |
.thenComparing(Person::getAge) | |
) | |
.forEach(person -> System.out.println("\t" + person)); | |
System.out.println(); | |
Person oldestPerson = Collections.max(people, Comparator.comparing(Person::getAge)); | |
System.out.println("The oldest person is " + oldestPerson); | |
String longestName = people.stream() | |
.map(person -> person.getFirstName() + " " + person.getLastName()) | |
.collect(Collectors.maxBy(Comparator.comparing(String::length))) | |
.orElse("Unknown"); | |
System.out.println("The longest full name in this list is '" + longestName + "'"); | |
System.out.println(); | |
boolean everyoneIsAlive = people.stream().allMatch(Person::isAlive); | |
if (everyoneIsAlive) { | |
System.out.println("Yay, everyone is alive!"); | |
} else { | |
System.out.println("Oh no, some of these people are dead! :("); | |
people.stream() | |
.filter(person -> !(person.isAlive())) | |
.forEach(person -> System.out.println("\t" + person + " is dead. RIP " + person.getFirstName() + ". :(")); | |
} | |
} | |
public static class Person { | |
private String firstName, | |
lastName; | |
private Integer age; | |
private boolean alive; | |
public Person(String firstName, String lastName, int age){ | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.age = age; | |
this.alive = (Math.random() < 0.5); // 50% chance of being either alive or dead | |
} | |
public String getFirstName(){ return this.firstName; } | |
public String getLastName() { return this.lastName; } | |
public Integer getAge() { return this.age; } | |
public Boolean isAlive() { return this.alive; } | |
public String toString() { return this.getFirstName() + " " + this.getLastName() + ", age " + this.getAge(); } | |
} | |
private static String getRandomFirstName() { return poolOfFirstNames[new Random().nextInt(poolOfFirstNames.length)]; } | |
private static String getRandomLastName() { return poolOfLastNames[new Random().nextInt(poolOfLastNames.length)]; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment