From 78f0dd183821ef471c430ddb6cb0444b28eb1367 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 21:04:28 +0100 Subject: Use more explicit stream filters --- src/ch/epfl/maze/physical/zoo/Hamster.java | 14 +++++++------- src/ch/epfl/maze/physical/zoo/Mouse.java | 15 +++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/maze/physical/zoo/Hamster.java b/src/ch/epfl/maze/physical/zoo/Hamster.java index 27706a5..3c903f5 100644 --- a/src/ch/epfl/maze/physical/zoo/Hamster.java +++ b/src/ch/epfl/maze/physical/zoo/Hamster.java @@ -6,7 +6,9 @@ import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; /** * Hamster A.I. that remembers the previous choice it has made and the dead ends @@ -38,13 +40,11 @@ public class Hamster extends ProbabilisticAnimal { */ private Direction[] excludeDeadPaths(Direction[] choices) { - List smartChoices = new ArrayList<>(); - - for (Direction dir : choices) - if (!this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) - smartChoices.add(dir); - - return smartChoices.toArray(new Direction[smartChoices.size()]); + return (new ArrayList<>(Arrays.asList(choices))) + .stream() + .filter(dir -> !this.deadPaths.contains(this.getPosition().addDirectionTo(dir))) + .collect(Collectors.toList()) + .toArray(new Direction[0]); } /** diff --git a/src/ch/epfl/maze/physical/zoo/Mouse.java b/src/ch/epfl/maze/physical/zoo/Mouse.java index 1b22c66..b5483c1 100644 --- a/src/ch/epfl/maze/physical/zoo/Mouse.java +++ b/src/ch/epfl/maze/physical/zoo/Mouse.java @@ -6,7 +6,8 @@ import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; +import java.util.stream.Collectors; /** * Mouse A.I. that remembers only the previous choice it has made. @@ -37,13 +38,11 @@ public class Mouse extends ProbabilisticAnimal { */ private Direction[] excludeOrigin(Direction[] choices) { - List smartChoices = new ArrayList<>(choices.length - 1); // max size, excluding origin - - for (Direction dir : choices) - if (!dir.isOpposite(this.previousChoice)) - smartChoices.add(dir); - - return smartChoices.toArray(new Direction[smartChoices.size()]); + return (new ArrayList<>(Arrays.asList(choices))) + .stream() + .filter(dir -> !dir.isOpposite(this.previousChoice)) + .collect(Collectors.toList()) + .toArray(new Direction[0]); } /** -- cgit v1.2.3