Java Articles

Page 167 of 450

Java Program for Longest Palindromic Subsequence

AmitDiwan
AmitDiwan
Updated on 17-Aug-2020 310 Views

For longest Palindromic subsequence, the Java code is as follows −Example Live Demopublic class Demo{    static String longest_seq(String str_1, String str_2){       int str_1_len = str_1.length();       int str_2_len = str_2.length();       char str_1_arr[] = str_1.toCharArray();       char str_2_arr[] = str_2.toCharArray();       int L[][] = new int[str_1_len + 1][str_2_len + 1];       for (int i = 0; i 0){          if (str_1_arr[i - 1] == str_2_arr[j - 1]){             longest_seq[my_index - 1] = str_1_arr[i - 1];       ...

Read More

Find a pair with given sum in a Balanced BST in Java

Arnab Chakraborty
Arnab Chakraborty
Updated on 23-Jul-2020 276 Views

ConceptWith respect of a given Balanced Binary Search Tree and a target sum, we write a function that returns true if there is a pair with sum equals to target sum, otherwise return false. In this case, expected time complexity is O(n) and only O(Logn) extra space can beimplemented. Here, any modification to Binary Search Tree is not permitted.We have to note that height of a Balanced BST is always O(Logn).ExampleMethodAccording to the Brute Force Solution, we consider each pair in BST and verify whether the sum equals to X. The time complexity of this solution will be O(n^2).Now a ...

Read More

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

raja
raja
Updated on 15-Jul-2020 256 Views

LongToIntFunction is a functional interface from java.util.function package introduced in Java 8. This functional interface accepts a long-valued parameter as input and produces an int-valued result. LongToIntFunction interface can be used as an assignment target for a lambda expression or method reference. This interface contains only one abstract method: applyAsInt() and doesn't contain any default and abstract methods.Syntax@FunctionalInterface interface LongToIntFunction {    int applyAsInt(long value); }Example of Lambda Expressionimport java.util.function.LongToIntFunction; public class LongToIntLambdaTest {    public static void main(String args[]) {       LongToIntFunction getInt = longVal -> {     // lambda expression          int intVal = (int)longVal;       ...

Read More

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

raja
raja
Updated on 15-Jul-2020 176 Views

LongToDoubleFunction is a built-in functional interface and part of java.util.function package. This functional interface accepts a long-valued parameter as input and produces a double-valued result. LongToDoubleFunction can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsDouble().Syntax@FunctionalInterface interface LongToDoubleFunction {  double applyAsDouble(long value); }Example of Lambda Exampleimport java.util.function.LongToDoubleFunction; public class LongToDoubleLambdaTest {    public static void main(String args[]) {       LongToDoubleFunction getDouble = longVal -> { // lambda expression          double doubleVal = longVal;          return doubleVal;       };       long input = ...

Read More

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

raja
raja
Updated on 15-Jul-2020 197 Views

IntToLongFunction is a built-in functional interface from java.util.function package. This functional interface accepts an int-valued parameter and produces a long-valued result. IntToLongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsLong().Syntax@FunctionalInterface interface IntToLongFunction {  long applyAsLong(int value); }Example of Lambda Expressionimport java.util.function.IntToLongFunction; public class IntToLongFunctionLambdaTest {    public static void main(String args[]) {       IntToLongFunction getLong = intVal -> {      // lambda expression          long longVal = intVal;          return longVal;       };           int input = 40; ...

Read More

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

raja
raja
Updated on 14-Jul-2020 253 Views

IntToDoubleFunction is a functional interface from java.util.function package. This functional interface accepts an int-valued argument and produces a double-valued result. IntToDoubleFunction can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsDouble().Syntax@FunctionalInterface interface IntToDoubleFunction {    double applyAsDouble(int value); }Example of Lambda Expressionimport java.util.function.IntToDoubleFunction;; public class IntToDoubleLambdaTest {    public static void main(String[] args) {       IntToDoubleFunction getDouble = intVal -> {      // lambda expression          double doubleVal = intVal;          return doubleVal;       };       int input ...

Read More

How to implement DoubleToIntFunction using lambda expression in Java?

raja
raja
Updated on 14-Jul-2020 381 Views

DoubleToIntFunction is a functional interface defined in java.util.function package introduced in Java 8 version. This functional interface accepts a double-valued argument and produces an int-valued result. DoubleToIntFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt().Syntax@FunctionalInterface interface DoubleToIntFunction {  int applyAsInt(double value) }Exampleimport java.util.function.DoubleToIntFunction; public class DoubleToIntFunctionTest {    public static void main(String args[]) {       DoubleToIntFunction test = doubleVal -> {     // lambda expression          int intVal = (int) doubleVal;          return intVal;       };       ...

Read More

How to implement DoubleToLongFunction using lambda expression in Java?

raja
raja
Updated on 14-Jul-2020 309 Views

DoubleToLongFunction is a built-in functional interface from java.util.function package introduced in Java 8. This functional interface accepts a double-valued parameter and produces a long-valued result. DoubleToLongFunction 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 DoubleToLongFunction {  long applyAsLong(double value) }Exampleimport java.util.function.DoubleToLongFunction; public class DoubleToLongFunctionTest {    public static void main(String args[]) {       double dbl = 30.1212;       DoubleToLongFunction castToLong = (dblValue) -> (long) dblValue; // lambda expression       System.out.println(castToLong.applyAsLong(dbl));       dbl = 77.9212;       DoubleToLongFunction roundToLong = Math::round;     ...

Read More

How to declare a variable within lambda expression in Java?

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

A lambda expression is a function that expects and accepts input parameters and produces output results. It is an instance of a functional interface and also known as a single abstract method interface (SAM interface) like Runnable, Comparator, Callable and etc. We can declare a variable as a final string[] array and able to access that array index within a lambda expression.Exampleimport java.util.*; public class LambdaTest {    public static void main(String args[]) {       final String[] country = {null};       List cities = new ArrayList();       cities.add("Hyderabad");       cities.add("Ireland");       cities.add("Texas");       ...

Read More

How to implement ToLongBiFunction<T, U> using lambda expression in Java?

raja
raja
Updated on 14-Jul-2020 283 Views

ToLongBiFunction is a in-built functional interface from java.util.function package. This functional interface accepts two reference type parameters as input and produces a long-valued result. ToLongBiFunction interface can be used as an assignment target for a lambda expression or method reference and contains only one abstract method: applyAsLong().Syntax@FunctionalInterface interface ToLongBiFunction {  long applyAsLong(T t, U u); }Exampleimport java.util.*; import java.util.function.ToLongBiFunction; public class ToLongBiFunctionTest {    public static void main(String[] args) {       ToLongBiFunction getMobileNum = (map, value) -> {    // lambda          if(map != null && !map.isEmpty()) {             if(map.containsKey(value)) {       ...

Read More
Showing 1661–1670 of 4,496 articles
« Prev 1 165 166 167 168 169 450 Next »
Advertisements