JavaOCP/MoreStreams/MoreStreams/src/main/java/ocp/practice/basics/MoreStreams/App.java

178 lines
4.9 KiB
Java

package ocp.practice.basics.MoreStreams;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Practicing streams and linkage
*
*/
public class App
{
public static void main( String[] args )
{
// Practice dirtying streams
// practiceDirtyLinkage();
// Practice parrallel streams (distinct)
// parrallelStreams();
// Reduction examples (with Optionals)
// reductionExamples();
// Stream operations (findFirst, anyMatch, allMatch)
matchingExamples();
// Flat mapping
flatMapping();
}
/**
* Takes one stream and let's you map T -> Stream<T>
* and then have that flattened into one stream
* exhausting each
*
* Best example would be splitting words across
* multiple lines. First we start by splitting
* it linefeed-wise and then word wise on each
* provided linefeed, producing a new stream.
*
* All these streams are exhausted, then moved
* on to the next until that too is exhausted
*/
private static void flatMapping()
{
String originalText = """
This is my sentence
and
it is
sexy
""";
Stream<String> lines = Stream.of(originalText.split("\n"));
lines.flatMap((line) -> Stream.of(line.split(" ")))
.forEach((word) -> System.out.println(word));
// This is my sentence and it is sexy
}
/**
* Playing around with {@link Stream#findFirst()},
* {@link Stream#anyMatch(java.util.function.Predicate)}
* and {@link Stream#allMatch(java.util.function.Predicate)}
*/
private static void matchingExamples()
{
Stream<Integer> stream = Stream.of(1, 2, 2, 3, 4, 5, 6, 7, 2, 9, 5);
/**
* Terminal operation returning the first item
* in the stream
*/
Optional<Integer> optional = stream.parallel().findFirst();
System.out.println(optional.get()); // Finds first element, so 1
/**
* Terminal operation which return whether
* anything in the stream matched it
*/
stream = Stream.of(1, 2, 2, 3, 4, 5, 6, 7, 2, 9, 5).parallel();
System.out.println(stream.anyMatch((i) -> i > 5)); // true
/**
* Terminal operation returning
* whether or not ALL elements
* of the stream matched the
* given predicate
*/
stream = Stream.of(1, 2, 2, 3, 4, 5, 6, 7, 2, 9, 5).parallel();
System.out.println(stream.allMatch((i) -> i>5)); // false
}
/**
* Practices using {@link Stream#distinct()}
*/
private static void parrallelStreams()
{
Stream<Integer> stream = Stream.of(1, 1, 2, 3, 4, 5, 6, 7, 1, 9, 5);
stream.parallel()
.distinct() // All parallel ones have a barrier here
.forEach(System.out::print); // 1 2 3 4 5 6 7 9 (order may differ based on who finishes first)
System.out.println();
}
/**
* Reduction examples
*/
private static void reductionExamples()
{
Stream<String> names = Stream.of("Abby", "Becky", "Thomas");
/**
* Here we take the first character of
* the left and right operands and add
* concatenate them together. Then
* resulting in the reduced value being
* that concatenation. This is used as
* the next iteration's left value
* and the new right value is then applied
* to it.
*
* I have a check for if we have the first
* iteration based on the first element
* as capturing variables into here is
* possible but not so much changing them.
* SO I hardcoded it.
*/
Optional<String> optional = names.reduce((left, right) -> {
if(left.equals("Abby"))
{
return ""+left.charAt(0)+right.charAt(0);
}
else
{
return ""+left+right.charAt(0);
}
});
if(optional.isPresent())
{
System.out.println("Optional value: '"+optional.get()+"'");
}
/**
* This will return the {@ink Optional}'s value
* IF it is present, ELSE the orElse's value
* is returned
*/
System.out.println("Optional value: "+optional.orElse("Niks"));
}
/**
* When you do a operation on a stream
* then it "dirties" the original stream
* meaning another thing cannot be linked.
*
* This is why you must always use to new
* stream created by the method call which
* dirtied the original one so as to have
* an "undirtied" one which can be linked.
*/
private static void practiceDirtyLinkage()
{
Stream<Integer> myStream = Stream.of(10, 20, 30);
// myStream.map((item) -> item+10);
myStream.forEach(System.out::print); // 10 20 30
var f = List.of(1,2,3,4);
f.forEach((d) -> System.out.print(d));
f.replaceAll(d->d+10);
Arrays.asList(1,2,3,4);
}
}