Implement toDoubleFunction Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 12:10:26

781 Views

ToDoubleFunction is a functional interface defined in java.util.function package. This functional interface expects a parameter as input and produces a double-valued result. It is used as an assignment target for a lambda expression or method reference. ToDoubleFunction interface contains only one abstract method, applyAsDouble().Syntax@FunctionalInterface public interface ToDoubleFunction {    double applyAsDouble(T value); }Example-1import java.util.function.ToDoubleFunction; public class ToDoubleFunctionTest1 {    public static void main(String args[]) {       ToDoubleFunction strLength = s -> s.length();    // lambda expression       System.out.println("The length of a given String using lambda expression is: " + strLength.applyAsDouble("TutorialsPoint"));       ToDoubleFunction innerClassImplementation = new ToDoubleFunction() ... Read More

Search a String in Matrix Using Split Function in Java

AmitDiwan
Updated on 13-Jul-2020 12:08:20

127 Views

To search a string in Matrix using split function, the code is as follows −Example Live Demoimport java.util.*; public class Demo {    public static int search_string(String[] my_matrix, String search_string){       for (String input : my_matrix){          String[] my_value = input.split(search_string);          if (my_value.length >= 2 || my_value.length == 0){             return 1;          }          else if (my_value.length == 1 && input.length() != my_value[0].length()){             return 1;          }       } ... Read More

Perl vs Java

AmitDiwan
Updated on 13-Jul-2020 12:04:56

363 Views

JavaJava is an object oriented programming language as well as a computing platform.It is safe, quick and reliable.The code in Java is first converted into bytecode and then executed using a JVM (Java Virtual Machine).The java program that is converted into bytecode is stored with the help of the extension ‘.class’.Java doesn’t give any specific way in which associative arrays could be stored, instead there are implementations of various hash functions.Java programs that need to be run are stored with the extension ‘.java’.Java is a statically typed language, i.e type checking is performed during compile-time (not run-time).Single line comments in ... Read More

Print Solid and Hollow Square Patterns in C

suresh kumar
Updated on 13-Jul-2020 12:04:35

845 Views

Program DescriptionIn geometry, a square is a regular quadrilateral, which means that it has four equal sides and four equal angles.Solid and Hollow Square will appear as shown belowAlgorithmFor Solid Square −Accept the Number of Rows from the user to draw the Solid Square For each Row, Print * for each Column to draw the Solid SquareFor Hollow Square −Accept the Number of Rows from the user to draw the Hollow Square For the First and Last Row, Print * for each Column For the Remaining Rows, Print * for the first and Last Column.Example/* Program to print hollow and ... Read More

Package Imports in JShell of Java 9

AmitDiwan
Updated on 13-Jul-2020 12:03:22

306 Views

In general, 10 packages are imported using the JShell.The following command shows the packages that were imported by default.jshell> /importOutputimport java.io.* import java.math.* import java.net.* import java.nio.file* import java.sql.* import java.util.* import java.util.regex* import java.util.function* import java.util.prefs* import java.util.stream.*Importing a specific package using JShelljshell> import java.util.*;

Implement Runnable Interface Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 12:03:19

18K+ Views

The Runnable interface is a functional interface defined in java.lang package. This interface contains a single abstract method, run() with no arguments. When an object of a class implementing this interface used to create a thread, then run() method has invoked in a thread that executes separately.Syntax@FunctionalInterface public interface Runnable {  void run(); }In the below example, we can implement a Runnable interface by using an anonymous class and lambda expression.Examplepublic class RunnableLambdaTest {    public static void main(String[] args) {       Runnable r1 = new Runnable() {          @Override          public void run() { // anonymous class   ... Read More

Method Overloading and Ambiguity in Varargs in Java

AmitDiwan
Updated on 13-Jul-2020 12:02:11

1K+ Views

There are ambiguities while using variable arguments in Java. This happens because two methods can definitely be valid enough to be called by data values. Due to this, the compiler doesn’t have the knowledge as to which method to call.Example Live Demopublic class Demo {    static void my_fun(double ... my_Val){       System.out.print("fun(double ...): " + "Number of args: " + my_Val.length );       for(double x : my_Val)       System.out.print(x + " ");       System.out.println();    }    static void my_fun(boolean ... my_Val){       System.out.print("fun(boolean ...) " + "The number of ... Read More

Print Reverse Floyd's Triangle in C

suresh kumar
Updated on 13-Jul-2020 12:01:16

524 Views

Program DescriptionFloyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner1                               15 14 13 12 11 2 3                             10 9 8 7 4 5 6                         6 ... Read More

Input Multiple Values from User in One Line in Java

AmitDiwan
Updated on 13-Jul-2020 12:00:31

13K+ Views

To input multiple values from user in one line, the code is as follows −Example Live Demoimport java.util.Scanner; public class Demo {    public static void main(String[] args) {       System.out.print("Enter two floating point values : ");       Scanner my_scan = new Scanner(System.in);       double double_val = my_scan.nextFloat();       int int_val = my_scan.nextInt();       System.out.println("The floating point value is : " + double_val + " and the integer value is : "       + int_val);    } }Input56.789 99OutputEnter two floating point values : The floating point value is ... Read More

Print Solid and Hollow Rhombus Patterns in C

suresh kumar
Updated on 13-Jul-2020 11:59:46

618 Views

Program DescriptionPrint the Solid and Hollow Rhombus Patterns as show belowAlgorithmFor Hollow Rhombus −Accept the Number of Rows for Hollow Rhombus from the User Create a Hollow Rhombus containing the same number of Rows specified by the User. Print the first row containing the number of stars same as the number of rows. Print the second row containing the first and last star as show in the output and leave the spaces between first and the last star. Do the same till you reach the last Row. Print the last row containing the number of stars same as the number ... Read More

Advertisements