Found 7442 Articles for Java

How to find a maximum value in a collection using method reference in Java?

raja
Updated on 11-Jul-2020 12:29:58

225 Views

A method reference provides a way in the lambda expression to refer a method without executing it. It requires a target type context that consists of a compatible functional interface.Syntax :: In the below example, we can find out the maximum value of an ArrayList using method reference.Exampleimport java.util.*; class MyClass {    private int val;    MyClass(int v) {       val = v;    }    int getVal() {       return val;    } } public class MethodReferenceMaxValueTest {    static int compareMaxValue(MyClass a, MyClass b) {       return a.getVal() - b.getVal();    }    public ... Read More

How to use method references with Generics in Java?

raja
Updated on 11-Jul-2020 12:25:52

956 Views

The method references are introduced in Java 8 similar to lambda expression. It can allow us to reference methods or constructors without executing them. The method references and lambda expressions require a target type that consists of a compatible functional interface. We can also use a method reference with generic classes and generic methods in java.Exampleinterface MyFunc {    int func(T[] vals, T v); } class MyArrayOps {    static int countMatching(T[] vals, T v) {       int count = 0;       for(int i=0; i < vals.length; i++)          if(vals[i] == v)             count++;       return count;   ... Read More

What is the generic functional interface in Java?

raja
Updated on 11-Jul-2020 12:23:09

4K+ Views

A lambda expression can't specify type parameters, so it's not generic. However, a functional interface associated with lambda expression is generic. In this case, the target type of lambda expression has determined by the type of argument(s) specified when a functional interface reference is declared.Syntaxinterface SomeFunc {  T func(T t); }Exampleinterface MyGeneric {    T compute(T t); } public class LambdaGenericFuncInterfaceTest {    public static void main(String args[]) {       MyGeneric reverse = (str) -> {   // Lambda Expression          String result = "";          for(int i = str.length()-1; i >= 0; i--)           ... Read More

What are the rules for a local variable in lambda expression in Java?

raja
Updated on 11-Jul-2020 11:57:48

4K+ Views

A lambda expression can capture variables like local and anonymous classes. In other words, they have the same access to local variables of the enclosing scope. We need to follow some rules in case of local variables in lambda expressions.A lambda expression can't define any new scope as an anonymous inner class does, so we can't declare a local variable with the same which is already declared in the enclosing scope of a lambda expression.Inside lambda expression, we can't assign any value to some local variable declared outside the lambda expression. Because the local variables declared outside the lambda expression can be final or effectively final.The rule of ... Read More

How to use an ArrayList in lambda expression in Java?

raja
Updated on 11-Jul-2020 11:40:27

2K+ Views

The lambda expression is an inline code that can implement a functional interface without creating an anonymous class. An ArrayList can be used to store a dynamically sized collection of elements.In the below program, We have removed the elements of an ArrayList whose age is less than or equal to 20 using the removeIf() method. This method introduced in Java 8 version to remove all elements from a collection that satisfy a condition.Syntaxpublic boolean removeIf(Predicate filter) The parameter filter is a Predicate. If the given predicate satisfies the condition, the element can be removed. This method returns the boolean value true if elements are removed, false otherwise.Exampleimport java.util.*; ... Read More

How can we use this and super keywords in method reference in Java?

raja
Updated on 11-Jul-2020 08:55:49

1K+ Views

The method reference is similar to a lambda expression that refers to a method without executing it and "::" operator can be used to separate a method name from the name of an object or class in a method reference.The methods can be referenced with the help of this and super keywords in Java. The super keyword can be used as a qualifier to invoke the overridden method in a class or an interface.syntaxthis::instanceMethod TypeName.super::instanceMethodExampleimport java.util.function.Function; interface Defaults {    default int doMath(int a) {       return 2 * a;    } } public class Calculator implements Defaults { ... Read More

Java Program for credit card number validation

Sunidhi Bansal
Updated on 20-Dec-2019 11:42:07

6K+ Views

Given a long number containing digits of a credit card number; the task is to find whether the credit card number is valid or not with a program.For checking a credit card is valid or not, the following are the validations we have to be sure for declaring the result.A credit card’s number must have 13 to 16 digits, it must start with the following digits.All the visa cards start from 4 All the master cards start from 537 is the starting for American express cardsAll the discover cards start from 6Steps to check whether the credit card is valid or ... Read More

What is a target type of lambda expression in Java?

raja
Updated on 11-Jul-2020 08:30:22

3K+ Views

A functional Interface for which a lambda expression has invoked is called target type of lambda expression. It means that if a lambda expression has invoked for some "X" interface then "X" is the target type of that lambda expression. Hence, we conclude that lambda expressions can be used only in those situations where java compiler can determine the Target Type.In the below example, the target type of lambda expression is BiFunction. An instance of a class is automatically created that implements the functional interface and lambda expression provides an implementation of the abstract method declared by the functional interface.Exampleinterface BiFunction { ... Read More

What are the rules to follow while using exceptions in java lambda expressions?

raja
Updated on 20-Dec-2019 08:36:16

3K+ Views

The lambda expressions can't be executed on their own. It is used to implement methods that have declared in a functional interface. We need to follow a few rules in order to use the exception handling mechanism in a lambda expression.Rules for Lambda ExpressionA lambda expression cannot throw any checked exception until its corresponding functional interface declares a throws clause.An exception thrown by any lambda expression can be of the same type or sub-type of the exception declared in the throws clause of its functional interface.Example-1interface ThrowException { void throwing(String message); } public class LambdaExceptionTest1 { ... Read More

How to pass a lambda expression as a method parameter in Java?

raja
Updated on 19-Dec-2019 13:09:20

7K+ Views

A lambda expression is an anonymous or unnamed method in Java. It doesn't execute on its own and used to implement methods that are declared in a functional interface. If we want to pass a lambda expression as a method parameter in java, the type of method parameter that receives must be of functional interface type.Exampleinterface Algebra { int operate(int a, int b); } enum Operation { ADD, SUB, MUL, DIV } public class LambdaMethodArgTest { public static void main(String[] args) { print((a, b) -> ... Read More

Advertisements