Created
March 25, 2015 20:19
-
-
Save jamestyack/a6a9abbf8dec271fc6c5 to your computer and use it in GitHub Desktop.
Java8 Streams stuff
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
public class StreamsTest { | |
@Test | |
public void chapter2Stream1() { | |
String contents = "catch,catching,cat,cats,dog,eating"; | |
String[] splitted = contents.split("[\\ P{ L}] +"); | |
System.out.println(splitted); | |
String[] wordsArray = { "catch", "catching", "cat", "cats", "dog", "eating"}; | |
Stream<String> stream = Arrays.stream(wordsArray, 1, wordsArray.length); | |
stream.forEach(System.out::println); | |
System.out.println(wordsArray); | |
Stream<String> words = Stream.of(wordsArray); | |
long c = words.filter(s -> s.contains("at")).count(); | |
System.out.println(c); | |
} | |
/* | |
* Skip the first element of array when creating stream | |
*/ | |
@Test | |
public void Chapter2StreamFromPartOfList() { | |
String[] wordsArray = { "catch", "catching", "cat", "cats", "dog", "eating"}; | |
Stream<String> stream = Arrays.stream(wordsArray, 1, wordsArray.length); | |
stream.forEach(System.out::println); | |
} | |
@Test | |
public void infiniteStream() { | |
Stream <Double> echos = Stream.generate(Math::random); | |
//echos.forEach(System.out::println); | |
} | |
@Test | |
public void infiniteSequence() { | |
Stream <BigInteger> integers = Stream.iterate(BigInteger.ZERO, n -> n.add( BigInteger.valueOf(2))); | |
// just get first 500 values | |
integers.limit(5).forEach(System.out::println); | |
} | |
@Test | |
public void mapAStream() { | |
CsvRow[] rows = { new CsvRow("huzi", "chipin", "4"), new CsvRow("tigger", "jack russell", "12")}; | |
Stream<CsvRow> stream = Arrays.stream(rows); | |
Stream<Dog> dogs = stream.map(row -> new Dog(row.get(0), row.get(1))); | |
Map<String, String> collectedDogs = dogs.collect(Collectors.toMap((Dog d) -> d.getName(), (Dog d) -> d.getType())); | |
//Stream<Pet> petStream = stream.map(row -> new Pet(row.get(0), row.get(1))); | |
//Map<String, String> collectedPets = petStream.collect(Collectors.toMap((Pet p) -> p.getName(), (Pet p) -> p.getType())); | |
System.out.println(collectedDogs); | |
//sSystem.out.println(collectedPets); | |
} | |
@Test | |
public void streamOfStreamsWithMap() { | |
List<String> words = new ArrayList<>(); | |
words.add("james"); | |
words.add("john"); | |
Stream<Stream<Character>> result = words.stream().map(w -> characterStream(w)); | |
List<Stream<Character>> collect = result.collect(Collectors.toList()); | |
collect.forEach(str -> System.out.println(str.collect(Collectors.toList()))); | |
} | |
@Test | |
public void streamOfStreamsWithFlatMap() { | |
List<String> words = new ArrayList<>(); | |
words.add("james"); | |
words.add("john"); | |
Stream<Character> flatMap = words.stream().flatMap(w -> characterStream(w)); | |
List<Character> collectedChars = flatMap.collect(Collectors.toList()); | |
collectedChars.forEach(System.out::print); | |
Stream<String> sIter = Stream.iterate("str", s -> s + "s"); | |
List<String> collect = sIter.limit(5).collect(Collectors.toList()); | |
System.out.println(collect); | |
} | |
public static Stream<Character> characterStream(String s) { | |
List<Character> result = new ArrayList<>(); | |
for (char c : s.toCharArray()) | |
result.add(c); | |
return result.stream(); | |
} | |
/* | |
* Peeking to see elements as they go though the pipeline | |
*/ | |
@Test | |
public void peeking() { | |
Stream.of("one", "two", "three", "four").filter(e -> e.length() > 3) | |
.peek(e -> System.out.println("Filtered value: " + e)) | |
.map(String::toUpperCase) | |
.peek(e -> System.out.println("Mapped value: " + e)) | |
.collect(Collectors.toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment