Java Articles

Page 169 of 450

How to implement Function<T, R> interface with lambda expression in Java?\\n

raja
raja
Updated on 14-Jul-2020 3K+ Views

Function interface is a functional interface from java.util.function package. This interface expects one argument as input and produces a result. Function interface can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: apply(), two default methods: andThen() and compose() and one static method: identity().Syntax@FunctionalInterface public interface Function {  R apply(T t); }Exampleimport java.util.function.Function; public class FunctionTest {    public static void main(String[] args) {       Function f1 = i -> i*4;   // lambda       System.out.println(f1.apply(3));       Function f2 = i -> i+4; // lambda   ...

Read More

How to implement DoubleSupplier using lambda and method reference in Java?

raja
raja
Updated on 14-Jul-2020 567 Views

DoubleSupplier interface is a built-in functional interface defined in java.util.function package. This functional interface doesn’t expect any input but produces a double-valued output. DoubleSupplier interface can be used as an assignment target for lambda expression and method reference. This interface contains only one abstract method: getAsDouble().Syntax@FunctionalInterface public interface DoubleSupplier {    double getAsDouble(); }Example of Lambda Expressionimport java.util.concurrent.ThreadLocalRandom; import java.util.function.DoubleSupplier; public class DoubleSupplierLambdaTest {    public static void main(String args[]) {       DoubleSupplier getRandomDouble = () -> {    // lambda expression          double doubleVal = ThreadLocalRandom.current().nextDouble(0000, 9999);          return Math.round(doubleVal);       };       ...

Read More

How to create our own/custom functional interface in Java?\\n

raja
raja
Updated on 14-Jul-2020 15K+ Views

The functional interface is a simple interface with only one abstract method. A lambda expression can be used through a functional interface in Java 8. We can declare our own/custom functional interface by defining the Single Abstract Method (SAM) in an interface.Syntaxinterface CustomInterface {    // abtstact method }Example@FunctionalInterface interface CustomFunctionalInterface {    void display(); } public class FunctionInterfaceLambdaTest {    public static void main(String args[]) {       // Using Anonymous inner class       CustomFunctionalInterface test1 = new CustomFunctionalInterface() {          public void display() {             System.out.println("Display using Anonymous inner class");         ...

Read More

How to implement ObjLongConsumer<T> interface using lambda expression in Java?

raja
raja
Updated on 14-Jul-2020 344 Views

ObjLongConsumer is a functional interface from java.util.function package. This interface accepts an object-valued and long-valued argument as input but doesn't produce any output. ObjLongConsumer can be used as an assignment target for lambda expression and method reference and contains only one abstract method: accept().Syntax@FunctionalInterface public interface ObjLongConsumer {  void accept(T t, long value) }Exampleimport java.util.function.ObjLongConsumer; public class ObjLongConsumerTest {    public static void main(String[] args) {       ObjLongConsumer olc = (employee, number) -> {     // lambda expression          if(employee != null) {             System.out.println("Employee Name: " + employee.getEmpName());         ...

Read More

How to implement LongFunction<R> using lambda and method reference in Java?

raja
raja
Updated on 14-Jul-2020 344 Views

LongFunction is an in-built functional interface defined in java.util.function package. This functional interface expects a long-valued parameter as input and produces a result. LongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: apply().Syntax@FunctionalInterface public interface LongFunction {  R apply(long value) }Exampleimport java.util.function.LongFunction; public class LongFunctionTest {    public static void main(String[] args) {       LongFunction function1 = (long i) -> { // lambda expression          return i + i;       };       System.out.println("Using Lambda Expression: " + function1.apply(10));       LongFunction ...

Read More

How to implement LongBinaryOperator using lambda and method reference in Java?

raja
raja
Updated on 14-Jul-2020 345 Views

LongBinaryOperator is a part of the functional interface from java.util.function package. This functional interface expects two parameters of type long as the input and produces a long type result. LongBinaryOperator interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method, applyAsLong().Syntax@FunctionalInterface public interface LongBinaryOperator {  long applyAsLong(long left, long right) }Example of Lambda Expressionimport java.util.function.LongBinaryOperator; public class LongBinaryOperatorTest1 {    public static void main(String[] args) {       LongBinaryOperator multiply = (a, b) -> { // lambda expression          return a*b;       };       long a = ...

Read More

How to implement IntConsumer using lambda and method reference in Java?\\n

raja
raja
Updated on 14-Jul-2020 390 Views

IntConsumer interface is a functional interface from java.util.function package in Java 8. This interface accepts a single int-valued argument as input but doesn't produce any output. Since it is a functional interface, it can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface IntConsumer {    void accept(int value); }Example of Lambda Expressionimport java.util.function.IntConsumer; public class IntConsumerTest1 {    public static void main(String[] args) {       IntConsumer displayNextInt = i -> System.out.println("Next Int Value: " + (i+1));  // lambda       IntConsumer displaySquare = ...

Read More

Print all permutation of a string using ArrayList in Java

sudhir sharma
sudhir sharma
Updated on 14-Jul-2020 816 Views

In this problem, we are given a string of size n and we have to print all permutations of the string. But this time we have to print this permutation using ArrayList.Let’s take an example to understand the problem -Input − string = ‘XYZ’Output − XYZ, XZY, YXZ, YZX, ZXY, ZYXTo solve this problem, we will be generating all permutations of the character of the string. We will use a recursive function and will return arrayList.ExampleThe following is ArrayList implementation of the algorithm − Live Demoimport java.util.ArrayList; public class Main{    static void printArrayList(ArrayList combo) {       combo.remove("");   ...

Read More

Preconditions - Java

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 312 Views

Precondition to check if the list passed as parameter is empty or not. Let us see an example −Examplepublic void my_fun(List myList){    if (myList == null){       throw new IllegalArgumentException("List is null");    }    if (myList.isEmpty()){       throw new IllegalArgumentException("List is empty");    }    my_fun(myList); }A void function named ‘my_fun’ is defined that takes a list of objects as its parameters. If the list is null, it prints the relevant message. If the list has no elements in it, a specific message is displayed. The function is called by passing the list as ...

Read More

Killing threads in Java

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 409 Views

Example Live Demopublic class Main{    static volatile boolean exit = false;    public static void main(String[] args){       System.out.println("Starting the main thread");       new Thread(){          public void run(){             System.out.println("Starting the inner thread");             while (!exit){             }             System.out.println("Exiting the inner thread");          }       }.start();       try{          Thread.sleep(100);       }       catch (InterruptedException e){   ...

Read More
Showing 1681–1690 of 4,496 articles
« Prev 1 167 168 169 170 171 450 Next »
Advertisements