Welcome at microwebservices.eu, my interests: microservices.com.pl, Java, cloud on AWS, J2EE, containerization/Dockerization, Kubernetes, JEE, EJB, JSP, Maven, Web Services, SOAP, REST, High Availability Systems, Genetic Algorithms, Neural Networks etc. See linkedin.com/in/grathor33/, bitbucket.org and https://github.com/grathor33/
Thursday, April 22, 2021
REMINDER: substring - ending index is exclusive! => "S".equals("String".substring(0,1))
Parameters
startIndex : starting index is inclusive, and starts at 0
endIndex : ending index is exclusive
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#substring(int,int)
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
https://www.javatpoint.com/java-string-substring
"north".substring(0,1) => "n"
"north".substring(1,2) => "o"
"north".substring(1,3) => "or"
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
Signature:
public String substring(int startIndex)
and
public String substring(int startIndex, int endIndex)
If you don't specify endIndex, java substring() method will return all the characters from startIndex.
compose() and andThen() methods in java.util.function.Function (Java Platform SE 8 )
apply
R apply(T t)
Applies this function to the given argument.- Parameters:
t
- the function argument- Returns:
- the function result
compose
default <V> Function<V,R> compose(Function<? super V,? extends T> before)
Returns a composed function that first applies thebefore
function to its input, and then applies this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.- Type Parameters:
V
- the type of input to thebefore
function, and to the composed function- Parameters:
before
- the function to apply before this function is applied- Returns:
- a composed function that first applies the
before
function and then applies this function - Throws:
NullPointerException
- if before is null- See Also:
andThen(Function)
andThen
default <V> Function<T,V> andThen(Function<? super R,? extends V> after)
Returns a composed function that first applies this function to its input, and then applies theafter
function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.- Type Parameters:
V
- the type of output of theafter
function, and of the composed function- Parameters:
after
- the function to apply after this function is applied- Returns:
- a composed function that first applies this function and then applies the
after
function - Throws:
NullPointerException
- if after is null- See Also:
compose(Function)
Wednesday, April 21, 2021
Modern JAVA: What is the difference between intermediate and terminal operations?
intermediate
and terminal
operations.The distinction between this operations is that an intermediate operation is lazy while a terminal operation is not. When you invoke an intermediate operation on a stream, the operation is not executed immediately. It is executed only when a terminal operation is invoked on that stream. In a way, an intermediate operation is memorized and is recalled as soon as a terminal operation is invoked. You can chain multiple intermediate operations and none of them will do anything until you invoke a terminal operation. At that time, all of the intermediate operations that you invoked earlier will be invoked along with the terminal operation.
All intermediate operations return Stream (can be chained), while terminal operations don't.
Intermediate Operations are:
filter(Predicate<T>) map(Function<T>) flatMap(Function<T>) sorted(Comparator<T>) peek(Consumer<T>) distinct() limit(long n) skip(long n)
Terminal operations produces a non-stream (cannot be chained) result such as primitive value, a collection or no value at all.
Terminal Operations are:
forEach forEachOrdered toArray reduce collect min max count anyMatch allMatch noneMatch findFirst findAny
Last 5 are short-circuiting terminal operations"
Java: static final methods and their usage
LAMBA and Interfaces in modern Java - References to different methods...
NazwaKlasy::nazwaMetody
Tuesday, April 20, 2021
Functional interface in Java SE 8+ = an interface with 1 abstract method (not derived from Object class)
Java SE 8+ STREAM: peek() vs. forEach() etc.
<R,A> R | collect(Collector<? super T,A,R> collector) Performs a mutable reduction operation on the elements of this stream using a Collector . |
<R> R | collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner) Performs a mutable reduction operation on the elements of this stream. |
Stream<T> | filter(Predicate<? super T> predicate) Returns a stream consisting of the elements of this stream that match the given predicate. |
static <T> Stream<T> | generate(Supplier<T> s) Returns an infinite sequential unordered stream where each element is generated by the provided Supplier . |
static <T> Stream<T> | iterate(T seed, UnaryOperator<T> f) Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed , producing a Stream consisting of seed , f(seed) , f(f(seed)) , etc. |
Stream<T> | limit(long maxSize) Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length. |
<R> Stream<R> | map(Function<? super T,? extends R> mapper) Returns a stream consisting of the results of applying the given function to the elements of this stream. |
static <T> Stream<T> | of(T... values) Returns a sequential ordered stream whose elements are the specified values. |
static <T> Stream<T> | of(T t) Returns a sequential Stream containing a single element. |
Optional<T> | reduce(BinaryOperator<T> accumulator) Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any. |
T | reduce(T identity, BinaryOperator<T> accumulator) Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value. |
<U> U | reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner) Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions. |
<R> Stream<R> | flatMap(Function<? super T,? extends Stream<? extends R>> mapper) Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. |
Monday, April 19, 2021
ThreadLocal (Java Platform SE 7 ) and AtomicInteger...
import java.util.concurrent.atomic.AtomicInteger;import java.lang.ThreadLocal<T>;
public class ThreadId { // Atomic integer containing the next thread ID to be assigned private static final AtomicInteger nextId = new AtomicInteger(0); // Thread local variable containing each thread's ID private static final ThreadLocal<Integer> threadId = new ThreadLocal<Integer>() { @Override protected Integer initialValue() { return nextId.getAndIncrement(); } }; // Returns the current thread's unique ID, assigning it if necessary public static int get() { return threadId.get(); } }
Sunday, April 18, 2021
Java Stream flatMap(). HOW-TO
Before flattening : [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 ]] After flattening : [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] |
Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8); Stream<String> words = lines.flatMap(line -> Stream.of(line.split( " +" ))); |
IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper) LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper) DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper) |