How a Thread Can Interrupt Another Thread in Java

AmitDiwan
Updated on 13-Jul-2020 11:57:50

957 Views

The ‘interrupt’ function can be used in Java to interrupt the execution of a thread with the help of the exception InterruptedException.The below example shows how the currently executing thread stops execution (because of a new exception raised in the catch block) once it is interrupted −Example Live Demopublic class Demo extends Thread {    public void run()    {       try       {          Thread.sleep(150);          System.out.println("In the 'run' function inside try block");       }       catch (InterruptedException e)       {       ... Read More

Why Kotlin Will Replace Java for Android App Development

AmitDiwan
Updated on 13-Jul-2020 11:54:59

254 Views

While working with Kotlin, Java libraries and frameworks can be used. This can be done with the help of advanced frameworks, without having to change the whole project. Java and Kotlin co-exist and can be present in the same project.Kotlin code can be placed inside Android Studio, without making the whole project in Kotlin.It is an open source platform that also helps in quick development of projects. It is easy, and readable, and considerably requires lesser coding in comparison to Java.Kotlin was developed by JetBrains, and IntelliJ is the IDE which Android Studio also uses.Kotlin plugin can be installed and ... Read More

Implement JavaFX Event Handling Using Lambda in Java

raja
Updated on 13-Jul-2020 11:54:58

4K+ Views

JavaFX Button class provides the setOnAction() method that can be used to set an action for the button click event. An EventHandler is a functional interface and holds only one method is the handle() method.Syntax@FunctionalInterface public interface EventHandler extends EventListenerIn the below example, we can able to implement event handling of JavaFX by using a lambda expression.Exampleimport javafx.application.*; import javafx.beans.property.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class LambdaWithJavaFxTest extends Application {    public static void main(String[] args) {       Application.launch(args);    }    @Override    public void start(Stage stage) throws Exception {     ... Read More

Print Lower and Upper Triangular Matrix in C

suresh kumar
Updated on 13-Jul-2020 11:53:43

13K+ Views

Program DescriptionWrite a program to print lower triangular matrix and upper triangular matrix of an Array.Triangular MatrixA Triangular matrix is one that is either lower triangular or upper triangular.Lower Triangular MatrixA square matrix is called lower triangular if all the entries above the main diagonal are zero.Upper Triangular MatrixA square matrix is called upper triangular if all the entries below the main diagonal are zero.A matrix of the form$${\displaystyle L={\begin{bmatrix}\ell _{1, 1}&&&&0\\ell _{2, 1}&\ell _{2, 2}&&&\\ell _{3, 1}&\ell _{3, 2}&\ddots &&\\vdots &\vdots &\ddots &\ddots &\\ell _{n, 1}&\ell _{n, 2}&\ldots &\ell _{n, n-1}&\ell _{n, n}\end{bmatrix}}}$$is called a lower triangular matrix or left triangular matrix, and analogously a matrix of the form$${\displaystyle U={\begin{bmatrix}u_{1, 1}&u_{1, 2}&u_{1, ... Read More

Sum of List with Stream Filter in Java

AmitDiwan
Updated on 13-Jul-2020 11:53:38

1K+ Views

To get sum of list with stream filter in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args)    {       List my_list = new ArrayList();       my_list.add(11);       my_list.add(35);       my_list.add(56);       my_list.add(78);       my_list.add(91);       System.out.println(sum(my_list));    }    public static int sum(List my_list)    {       System.out.println("In the main function, the sum of list with filter is ");       return my_list.stream().filter(i -> i > 5).mapToInt(i -> i).sum();    } ... Read More

Structure and Members of the Java Program

AmitDiwan
Updated on 13-Jul-2020 11:52:46

316 Views

While writing any piece of code in Java, there is a certain set of rules and regulations that need to be followed, that is considered as a standard. For example − A class contains variables, and functions. The functions can be used to work with the variables. Classes can be extended, and improvised too.Basic structureList of packages that are imported; public class {    Constructor (can be user defined or implicitly created)    {       Operations that the constructor should perform;    }    Data elements/class data members;    User-defined functions/methods;    public static void main (String ... Read More

Implement DoubleFunction Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 11:44:48

417 Views

The DoubleFunction is a built-in functional interface defined in java.util.function package. This interface accepts one double-valued parameter as input and returns a value of type R. As this is a functional interface, it can be used as an assignment target for a lambda expression or method reference. DoubleFunction interface having only one abstract method, apply().Syntax@FunctionalInterface public interface DoubleFunction {  R apply(double value); }Exampleimport java.util.function.DoubleFunction; public class DoubleFunctionTest {    public static void main(String[] args) {       DoubleFunction getGrade = marks -> { // lambda expression          if(marks > 90 && marks 70 && marks 50 && marks

Generate Prime Numbers Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 11:32:07

2K+ Views

A prime number is a number that is greater than 1 and divided by 1 or itself only. It other words, it can't be divided by other numbers than itself or 1. The generation of prime numbers is 2, 3, 5, 7, 11, 13, 17 and etc.In the below example, we can generate the prime numbers with the help of Stream API and lambda expressions.Exampleimport java.util.*; import java.util.stream.*; public class PrimeNumberLambdaTest {    public static void main(String[] args) {       List generate = PrimeNumberLambdaTest.generate(10);       System.out.println(generate);    }    public static List generate(int series) {       Set set = new TreeSet(); ... Read More

Implement IntFunction with Lambda Expression in Java

raja
Updated on 13-Jul-2020 09:25:13

472 Views

IntFunction interface is a functional interface in Java 8 and defined in java.util.function package. This interface accepts an int-valued parameter as input and returns a value of type R. IntFunction interface can be used as an assignment target for a lambda expression or method reference and holds only one abstract method, apply().Syntax@FunctionalInterface public interface IntFunction {    R apply(int value); }Exampleimport java.util.HashMap; import java.util.Map; import java.util.function.IntFunction; public class IntFunctionTest {    public static void main(String[] args) {       IntFunction getMonthName = monthNo -> {  // lambda expression          Map months = new HashMap();          months.put(1, "January");       ... Read More

Implement Fibonacci Series Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 09:24:29

3K+ Views

The Fibonacci is a sequence of numbers in which every number after the first two numbers is the sum of the two preceding numbers like 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on. The sequence of Fibonacci numbers defined by using "F(n)=F(n-1)+F(n-2)".In the below example, we can implement the Fibonacci series with the help of Stream API and lambda expression. The Stream.iterate() method returns an infinite sequential ordered stream produced by iterative application of a function to an initial element seed, producing a stream consisting of seed, f(seed), f(f(seed)), etc.Exampleimport java.util.List; import java.util.stream.*; public class FibonacciTest {    public static void ... Read More

Advertisements