This is the second solution to find the length of a given long integer value with help of functional interface and lambda expression. The internal logic is same that is converting long to string, and apply length method.
import java.util.Scanner;
//Define functional interface lengthOflongInt
interface lengthOflongInt {
public int lenOfLong(long a);
}
public class LengthOfLongInteger {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner inpLongInt = new Scanner(System.in);
// Input a long integer
System.out.print("Enter long Integer=");
long longNum = inpLongInt.nextLong();
System.out.println("The long integer is=" + longNum);
// Define lambda expression lint which returns length
lengthOflongInt lint = ((longInt) -> {
String longIntToStr = String.valueOf(longInt);
return (longIntToStr.length());
});
// Call the lambda expression with parameter as long integer
System.out.println("The Length of the long integer " + longNum + " is=" + lint.lenOfLong(longNum));
System.out.println("The Length of other long integer is=" + lint.lenOfLong(8461288490317329L));
System.out.println("The Length of other long integer is=" + lint.lenOfLong(6536565846128717329L));
}
}
Output:
Enter long Integer=
523452323634
The long integer is=523452323634
The Length of the long integer 523452323634 is=12
The Length of other long integer is=16
The Length of other long integer is=19

Leave a comment