Search results for: Java

class Test { public static void main(String[] args) { System.out.println(“Normal Stream…”); IntStream integers = IntStream.rangeClosed(1, 10); integers.forEach(num -> System.out.print(num + ” “)); System.out.println(); System.out.println(“Parallel Stream…”); IntStream integers2 = IntStream.rangeClosed(1, 10); integers2.parallel().forEach(num -> System.out.print(num + ” “)); } } Output: Normal Stream… 1 2 3 4 5 6 7 8 9 10 Parallel Stream… 7 6…

Read More Introduction to Parallel Streams API in Java

class Test { public static void main(String[] args) { Optional optional = Optional.empty(); // creates an empty Optional if (optional.isEmpty()) { System.out.println(“It is an empty Optional.”); } else { System.out.println(“Optional is not empty.”); } } } Output: It is an empty Optional. 2. Optional.of(T value) method class Test { public static void main(String[] args) {…

Read More Introduction to Optional class in Java

@FunctionalInterface interface MyInterface { Message send(String message); } class Message { public Message(String message) { System.out.println(message); } } class Test { public static void main(String[] args) { MyInterface myInterface = Message::new; myInterface.send(“Hello!”); } } Output: Hello! class Message { public Message(String message) { System.out.println(message); } public Message(String message1, String message2) { System.out.println(message1 + message2); }…

Read More Constructor Reference in Java

@FunctionalInterface interface Drawable { void draw(); } class Test { public void drawCircle() { System.out.println(“Drawing circle…”); } public static void main(String[] args) { Test test = new Test(); Drawable drawable = () -> test.drawCircle(); drawable.draw(); } } Output: Drawing circle… @FunctionalInterface interface Drawable { void draw(); } class Test { public void drawCircle() { System.out.println(“Drawing…

Read More Method Reference in Java

@FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); } class Test { public static void main(String[] args) { Supplier<String> supplier = () -> “Java”; System.out.println(supplier.get()); } } Output: Java class User { private String name; private String username; private String membershipType; private String address; public…

Read More Supplier Functional Interface in Java

@FunctionalInterface public interface BinaryOperator<T> extends BiFunction<T,T,T> { /** * Returns a {@link BinaryOperator} which returns the lesser of two elements * according to the specified {@code Comparator}. * * @param <T> the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * @return a {@code…

Read More BinaryOperator Functional Interface in Java

@FunctionalInterface public interface UnaryOperator<T> extends Function<T, T> { /** * Returns a unary operator that always returns its input argument. * * @param <T> the type of the input and output of the operator * @return a unary operator that always returns its input argument */ static <T> UnaryOperator<T> identity() { return t -> t;…

Read More UnaryOperator Functional Interface in Java

@FunctionalInterface public interface BiPredicate<T, U> { /** * Evaluates this predicate on the given arguments. * * @param t the first input argument * @param u the second input argument * @return {@code true} if the input arguments match the predicate, * otherwise {@code false} */ boolean test(T t, U u); /** * Returns a…

Read More BiPredicate Functional Interface in Java

@FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); /** * Returns a composed predicate that represents a short-circuiting logical * AND of this…

Read More Predicate Functional Interface in Java

@FunctionalInterface public interface BiConsumer<T, U> { /** * Performs this operation on the given arguments. * * @param t the first input argument * @param u the second input argument */ void accept(T t, U u); /** * Returns a composed {@code BiConsumer} that performs, in sequence, this * operation followed by the {@code after}…

Read More BiConsumer Functional Interface in Java