Java Articles

Page 282 of 450

Java program to List all files in a directory and nested sub-directory - Recursive approach

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 585 Views

To list all files in a directory and nested sub-directory, the Java program is as follows −Exampleimport java.io.File; public class Demo{    static void print_recursively(File[] my_arr, int my_index, int sub_level){       if(my_index == my_arr.length)       return;       for (int i = 0; i < sub_level; i++)       System.out.print("\t");       if(my_arr[my_index].isFile())       System.out.println(my_arr[my_index].getName());       else if(my_arr[my_index].isDirectory()){          System.out.println("[" + my_arr[my_index].getName() + "]");          print_recursively(my_arr[my_index].listFiles(), 0, sub_level + 1);       }       ...

Read More

How to implement DoubleConsumer using lambda expression in Java?

raja
raja
Updated on 14-Jul-2020 337 Views

DoubleConsumer is a functional interface from java.util.function package. This functional interface accepts a single double-valued argument as input and produces no output. This interface can be used as an assignment target for a lambda expression or method reference. DoubleConsumer contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface DoubleConsumer { void accept(double value); }Example-1import java.util.function.DoubleConsumer; public class DoubleConsumerLambdaTest1 { public static void main(String args[]) { DoubleConsumer increment = doubleVal -> { // lambda expression ...

Read More

How to implement LongConsumer using lambda in Java?

raja
raja
Updated on 13-Jul-2020 223 Views

LongConsumer is a in-built functional interface from java.util.function package. This interface can accept a single long-valued argument as input and doesn't produce any output. It can also be used as the assignment target for a lambda expression or method reference and contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface LongConsumerExample-1import java.util.function.LongConsumer; public class LongConsumerLambdaTest { public static void main(String[] args) { LongConsumer displayNextVal = l-> { // lambda expression System.out.println("Display the next value ...

Read More

How to implement IntUnaryOperator using lambda in Java?

raja
raja
Updated on 13-Jul-2020 998 Views

IntUnaryOperator represents a functional interface that takes an int value and returns another int value. It is defined in java.util.function package and used as an assignment target for a lambda expression or method reference. It contains one abstract method: applyAsInt(), two default methods: compose(), andThen() and one static method: identity().Syntax@FunctionalInterface public interface IntUnaryOperatorExampleimport java.util.function.IntUnaryOperator; public class IntUnaryOperatorLambdaTest1 { public static void main(String[] args) { IntUnaryOperator getSquare = intValue -> { // lambda expression int result = intValue * ...

Read More

How to write lambda expression code for SwingUtilities.invokeLater in Java?

raja
raja
Updated on 13-Jul-2020 1K+ Views

An event handling code that runs on a special thread called event dispatch thread(EDT). Most of the code that invokes swing methods also runs on this EDT thread. It is necessary because most of swing object methods do not thread-safe. SwingUtilities is a utility class and has one important static method, invokeLater(). This method can be used to perform a task asynchronously in the AWT event dispatcher thread.Syntaxpublic static void invokeLater(Runnable doRun)Exampleimport javax.swing.*; public class InvokeLAterLambdaTest { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { // ...

Read More

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

raja
raja
Updated on 13-Jul-2020 447 Views

DoubleBinaryOperator is a functional interface defined in java.util.function package. It accepts two parameters of type double as input and produces another double value as a result. DoubleBinaryOperator interface can be used as an assignment target for a lambda expression or method reference and has only one abstract method applyAsDouble().Syntax@FunctionalInterface public interface DoubleBinaryOperator {  double applyAsDouble(double left, double right); }Example-1import java.util.function.DoubleBinaryOperator; public class DoubleBinaryOperatorTest {    public static void main(String args[]) {       // using lambda expression       DoubleBinaryOperator sum = (d1, d2) -> d1 + d2;       DoubleBinaryOperator mul = (d1, d2) -> d1 * d2;       DoubleBinaryOperator div = ...

Read More

How to implement the IntPredicate interface using lambda and method reference in Java?

raja
raja
Updated on 13-Jul-2020 517 Views

IntPredicate interface is a built-in functional interface defined in java.util.function package. This functional interface accepts one int-valued argument as input and produces a boolean value as an output. This interface is a specialization of the Predicate interface and used as an assignment target for a lambda expression or method reference. It provides only one abstract method, test ().Syntax@FunctionalInterface public interface IntPredicate { boolean test(int value); }Example for Lambda Expressionimport java.util.function.IntPredicate; public class IntPredicateLambdaTest { public static void main(String[] args) { IntPredicate intPredicate = (int input) -> { ...

Read More

How to implement ActionEvent using method reference in JavaFX?

raja
raja
Updated on 13-Jul-2020 2K+ Views

The javafx.event package provides a framework for Java FX events. The Event class serves as the base class for JavaFX events and associated with each event is an event source, an event target, and an event type. An ActionEvent widely used when a button is pressed.In the below program, we can implement an ActionEvent for a button by using method reference.Exampleimport javafx.application.*; import javafx.beans.property.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.scene.effect.*; public class MethodReferenceJavaFXTest extends Application {    private Label label;    public static void main(String[] args) {       launch(args);    }    @Override    public void start(Stage primaryStage) {   ...

Read More

How to implement ToDoubleFunction using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 849 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 ...

Read More

Perl vs Java

AmitDiwan
AmitDiwan
Updated on 13-Jul-2020 427 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
Showing 2811–2820 of 4,498 articles
« Prev 1 280 281 282 283 284 450 Next »
Advertisements