Found 7442 Articles for Java

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

raja
Updated on 14-Jul-2020 07:43:00

266 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?

raja
Updated on 14-Jul-2020 07:31:43

353 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

How to use this and super keywords with lambda expression in Java?

raja
Updated on 14-Jul-2020 07:32:30

2K+ Views

The "this" and "super" references within a lambda expression are the same as in the enclosing context. Since the lambda expression doesn't define a new scope, "this" keyword within a lambda expression signifies "this" parameter of a method where the lambda expression is residing.In the below example, this.toString() calls the toString() method of the LambdaTest object but not the toString() method of the Operate instance.Example@FunctionalInterface interface Operate {    int func(int num1, int num2);    public String toString(); } public class LambdaTest {    public static void main(String[] args) {       LambdaTest test = new LambdaTest();       test.getResult();    } ... Read More

Print all permutation of a string using ArrayList in Java

sudhir sharma
Updated on 14-Jul-2020 07:27:07

760 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

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

raja
Updated on 16-Jan-2020 10:04:48

303 Views

DoublePredicate is a built-in functional interface defined in java.util.function package. This interface can accept one double-valued parameter as input and produces a boolean value as output. DoublePredicate interface can be used as an assignment target for a lambda expression or method reference. This interface contains one abstract method: test() and three default methods: and(),  or() and negate().Syntax@FunctionalInterface public interface DoublePredicate {    boolean test(double value) }Example of lambda expressionimport java.util.function.DoublePredicate; public class DoublePredicateLambdaTest {    public static void main(String args[]) {       DoublePredicate doublePredicate = (double input) -> {    // lambda expression          if(input == 2.0) {     ... Read More

How to implement DoubleConsumer using lambda expression in Java?

raja
Updated on 14-Jul-2020 06:38:16

280 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          System.out.println("Incrementing " + doubleVal + " by one");          System.out.println("Current Value : ... Read More

Character class: union - Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:52:15

565 Views

The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression matches a single character from the specified or, set of possible characters. For example the regular expression [abc] matches a single character a or, b or, c.The union variant of the character class allows you to match a character from one of the specified ranges i.e. the expression [a-z[0-9]] matches a single character which is either a small alphabet (a-z) or a digit (0-9).Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public static void main(String[] args) {     ... Read More

Character class: range - Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:49:55

179 Views

The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression matches a single character from the specified or, set of possible characters. For example, the regular expression [abc] matches a single character a or, b or, c.The range variant of the character class allows you to use a range of characters i.e the expression [a-z] matches a single character from the alphabets a to z and the expression [^A-Z] matches a character which is not a capital letter.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public static void ... Read More

Character class: Negation - Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:47:11

3K+ Views

The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression matches a single character from the specified or, set of possible characters.For example the regular expression [abc] matches a single character a or, b or, c. Similarly, "[a-z]" matches a single character from a to z.Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters.For example the regular expression [^abc] matches a single character except a or, b ... Read More

Matching multiple lines in Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:44:13

936 Views

To match/search a input data with multiple lines −Get the input string.Split it into an array of tokens by passing "\r?" as parameter to the split method.Compile the required regular expression using the compile() method of the pattern class.Retrieve the matcher object using the matcher() method.In the for loop find matches in the each element (new line) of the array using the find() method.Reset the input of the matcher to the next element of the array using the reset() method.Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchingText{    public static void main(String[] args) {       String input = ... Read More

Advertisements