From 5bb02d6a72426a3f59ecb4b4149f3fc9883a72d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Paumard?= <Jose.Paumard@gmail.com> Date: Sun, 10 Sep 2017 14:38:13 +0200 Subject: [PATCH] Added an example for the Stream.takeWhile method. --- .../test/exercises/E_IntermediateStreams.java | 28 ++++++++++++++++- .../test/solutions/E_IntermediateStreams.java | 31 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/LambdaLab/test/exercises/E_IntermediateStreams.java b/LambdaLab/test/exercises/E_IntermediateStreams.java index 3857cc9..42b40ff 100644 --- a/LambdaLab/test/exercises/E_IntermediateStreams.java +++ b/LambdaLab/test/exercises/E_IntermediateStreams.java @@ -6,19 +6,23 @@ import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.AbstractCollection; +import java.util.AbstractList; +import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.LongStream; +import java.util.stream.Stream; import org.junit.After; +import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -208,6 +212,28 @@ public class E_IntermediateStreams { // Use Stream.reduce() and think about the order of the arguments. // </editor-fold> + /** + * Create a list containing all the super classes of ArrayList. + */ + @Test + public void e8_selectTheSuperClassesOfArrayList() { + Class<?> origin = ArrayList.class; + + List<String> result = null; // TODO + + assertEquals( + Arrays.asList(ArrayList.class, AbstractList.class, AbstractCollection.class, Object.class), + result); + } + // Hint: + // <editor-fold defaultstate="collapsed"> + // There is a getSuperClass() method on the Class class. + // Creating a stream of these classes can be made with Stream.iterate(). + // Then you need to close that stream when the current class is null. + // Java 9 added the takeWhile() method on the stream interface. + // </editor-fold> + + // ======================================================== // END OF EXERCISES diff --git a/LambdaLab/test/solutions/E_IntermediateStreams.java b/LambdaLab/test/solutions/E_IntermediateStreams.java index 2ee4bda..b4d53d1 100644 --- a/LambdaLab/test/solutions/E_IntermediateStreams.java +++ b/LambdaLab/test/solutions/E_IntermediateStreams.java @@ -6,19 +6,23 @@ import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.AbstractCollection; +import java.util.AbstractList; +import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.LongStream; +import java.util.stream.Stream; import org.junit.After; +import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -262,6 +266,31 @@ public class E_IntermediateStreams { // Use Stream.reduce() and think about the order of the arguments. // </editor-fold> + /** + * Create a list containing all the super classes of ArrayList. + */ + @Test + public void e8_selectTheSuperClassesOfArrayList() { + Class<?> origin = ArrayList.class; + + //TODO//List<String> result = null; + //BEGINREMOVE + Stream<Class<?>> stream = Stream.<Class<?>>iterate(origin, clazz -> clazz.getSuperclass()); + List<Class<?>> result = stream.takeWhile(clazz -> clazz != null).collect(Collectors.toList()); + //ENDREMOVE + + assertEquals( + Arrays.asList(ArrayList.class, AbstractList.class, AbstractCollection.class, Object.class), + result); + } + // Hint: + // <editor-fold defaultstate="collapsed"> + // There is a getSuperClass() method on the Class class. + // Creating a stream of these classes can be made with Stream.iterate(). + // Then you need to close that stream when the current class is null. + // Java 9 added the takeWhile() method on the stream interface. + // </editor-fold> + // ======================================================== // END OF EXERCISES -- GitLab