Skip to content

Instantly share code, notes, and snippets.

@PaulSandoz PaulSandoz/
Last active Dec 12, 2015

What would you like to do?
This is an example of Conway's Game of Life using the OpenJDK lambda Stream API. This code works with build from the tip of the lambda repository as of the 1st February 2013. Since the API is a moving this code may not compile with future builds.
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit if you need additional information or have any
* questions.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import static*;
* Inspired from Christophe Grand:
public class Life {
static final class Cell {
final int x;
final int y;
Cell(int x, int y) {
this.x = x;
this.y = y;
Cell add(Cell that) {
return new Cell(that.x + x, that.y + y);
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cell cell = (Cell) o;
if (x != cell.x) return false;
if (y != cell.y) return false;
return true;
public int hashCode() {
return 31 * x + y;
public String toString() {
return String.format("[%d, %d]", x, y);
static Cell of(int x, int y) {
return new Cell(x, y);
static List<Cell> neighbourDeltas() {
int[] values = {1, 0, -1};
List<Cell> cellDiffs = new ArrayList<>();
for (int x : values) {
for (int y : values) {
if (y != 0 || x != 0) {
cellDiffs.add(new Cell(x, y));
return Collections.unmodifiableList(cellDiffs);
static List<Cell> neighbourDeltas2() {
List<Integer> values = Arrays.asList(1, 0, -1);
Function<Integer, Stream<? extends Cell>> f = x -> -> y != 0 || x != 0).map(y -> new Cell(x, y));
// List of neighbouring cells around the origin
// x x x
// x o x
// x x x
static final List<Cell> NEIGHBOUR_DELTAS = neighbourDeltas();
static Stream<Cell> neighbours(Stream<Cell> world) {
return world.flatMap(cell ->;
static <T> Collector<T, Map<T, Integer>> toFrequencies() {
return toMap(c -> 1, HashMap::new, Integer::sum);
static Set<Cell> step(Set<Cell> world) {
// Calculate the frequencies of neighbouring cells
Map<Cell, Integer> fs = neighbours(
return fs.entrySet().stream().
// Any live cell with fewer than two live neighbours dies, as if caused by under-population.
// [*] Any live cell with two or three live neighbours lives on to the next generation.
// Any live cell with more than three live neighbours dies, as if by overcrowding.
// [*] Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
filter(e -> e.getValue() == 3 || (e.getValue() == 2 && world.contains(e.getKey()))).
// Map from Stream<Entry<Cell, Integer>> to Stream<Cell>
map(e -> e.getKey()).
// Collect to a set
public static void main(String[] args) {
// Initial state, a blinker!
Set<Cell> world = new HashSet<>(Arrays.asList(Cell.of(1, 2),
Cell.of(1, 1), Cell.of(1, 0)));
// Iterate for 5 generations printing out each generation
Streams.iterate(world, Life::step).limit(5).
// x x
// x -> x x x -> x -> x x x
// x x

This comment has been minimized.

Copy link

tobyweston commented Feb 11, 2014

Is there a problem with the generics on line 120?
Line 146 should be Stream, not Streams?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.