- Practice
- Java
- Advanced
- Java Lambda Expressions
- Discussions

# Java Lambda Expressions

# Java Lambda Expressions

manni_reies + 7 comments Trying to keep everything to one line. Might not be the most efficient, but it's clean.

public static PerformOperation is_odd(){ return (int a) -> a % 2 != 0; } public static PerformOperation is_prime(){ return (int a) -> java.math.BigInteger.valueOf(a).isProbablePrime(1); } public static PerformOperation is_palindrome(){ return (int a) -> Integer.toString(a).equals( new StringBuilder(Integer.toString(a)).reverse().toString() ); }

davipc + 0 comments Beautiful code!

Ailixia + 4 comments Hey, why do you use one more toString() there (last one)?

StringBuilder(Integer.toString(a)).reverse().toString() );

davipc + 0 comments I believe that's for the function to return an immutable String, and not a StringBuilder.

somebody294 + 0 comments because if you want String content of the StringBuilder object, you need to call the toString, otherwise you will get the object hash.

tsherisherpa + 0 comments last .toString() converts the string builder obj to string which makes it available to use the string method .equals() which can be used only to compare two string .

Kronodeus + 0 comments Even shorter palindrome check for the OCD amongst us:

(int a) -> (""+a).equals(""+new StringBuilder((""+a)).reverse());

masonite + 0 comments Beautiful solution. One comment, though: they don't need to be static functions.

rodney_barbati + 0 comments you don't need to declare the parameter type as int in your lamdbas - it is implied by the return type (PerformOperation).

mithunkumar + 0 comments You have given the interface as a return type. so what will be the return value of the methods isOdd, isPrime, isPalindrome and the interface method is not declared with the body. so where is the body and what is the body of the check method? Can you please clarify these doubts?

thanks in advance

voyenavion + 1 comment Something I don't understand...presumably these methods are entered as the PerformOperation parameter the checker() method calls for. My question is how do the methods you wrote (e.g. is_Odd() ) get the int parameter? They don't include any params in their method signature...

peter_jones + 0 comments By defining the lambda, you are defining behaviour but not executing it at that point, and you are returning this to the PerformOperation Functional interface return type. Then whoever uses this, ie who called odd(), will pass this lambda (retrieved via the PerformOperation reference returned from odd()) to a method that takes a PerformOperation Functional interface as a parameter. It is in THIS called method that the lamda will be executed, and therefore the int parameter will be supplied.

Highlander_Monk + 0 comments Beautiful peice :)

ozdemirburak + 4 comments public PerformOperation is_odd(){ return n -> (n%2 != 0); } public PerformOperation is_prime(){ return n -> IntStream.range(2, (int) Math.sqrt(n+1)) .noneMatch(i -> n%i == 0); } public PerformOperation is_palindrome(){ return n -> IntStream.iterate(n, e -> e/10 ) .limit(11) .filter(f -> f>0) .map(e -> e%10) .reduce(0, (c,e)->c*10+e) == n; }

DigitalOnion + 0 comments Excelent solution!

aKaSoze + 0 comments good one sir

preethavp + 0 comments Hi, How did you add the IntStream import? I am unable to do so.

rss95 + 1 comment is_palindrome() has a flaw will ignore any 0 ditgits at the end eg:100, it will ignore last 2 zeros and will compare 1 == 100

but dosent matter bcoz number leading with zero will be considered as octal number and it will fck up the algo anyway

ozdemirburak + 1 comment It is not flaw, it is supposed to work like that. Because leading zeros have no meaning in math. this code is_palindrome() returns false for 100 and it is correct. Or are you claiming that 100 is palindrome? Have look at basic math here; https://en.wikipedia.org/wiki/Leading_zero

rss95 + 0 comments Well it is true i do not know maths really well And saying that function for palindrome has a flaw was also wrong considering the question requires to check palindrome for integers. I was just working with your algo and like a idiot i passed 010 to It and got a wrong answer . Then after debugging it for a while i found out that java considers leading zero as octal. So i thought for palindrome , working on it as a string is better than working on it as integer

morfanos + 1 comment I have so many issues with this exercise...

namespace clashing, e.g. class Math { }.

(a) Returning 0/1 instead of true/false.

(b) Returning 0 to denote a

*truthy*value, which goes against common coding sense.This:

op = ob.checkEvenOdd(); ret = ob.checker(op, num);

The whole thing was confusing from begining to end!

tao_zhang + 1 comment You are absolutely right! 'check

`er`

' is a method, '`p`

erformOperation' is an interface, etc. Why Hackerrank doesn't let someone, who follows a standard naming convention, proofread and refactor the code?Shafaet + 2 comments We have refactored the code and fixed some of the issues mentioned above. If there are more issues, please let us know. Thanks for your feedback.

andronix + 0 comments Method MyMath.checker(PerformOperation p, int num) is static and should be accessed in a static way.

augten95 + 1 comment I don't see a closing bracket for the MyMath class in the code. It is giving me this error when I run my code. Error: reached end of file while parsing. Is there a way to add the closing bracket to the MyMath class?

GrandAdmiral + 0 comments You can add the closing bracket yourself. I spent a lot of time trying to figure out that same issue. That was frustrating because I assumed it was a problem with my syntax.

niteshkrsingh51 + 4 comments Its a bit long but easy to understand...

public static PerformOperation isOdd() { return num -> { if(num%2==0) return false; else return true; } ; } public static PerformOperation isPrime() { return num -> { int flag = 0; for(int i=2;i<=num/2;i++) { if(num%i==0) { flag = 1; break; } else { flag = 0; break; } } if(flag == 0) return true; else return false; }; } public static PerformOperation isPalindrome() { return num -> { int r,sum=0,temp; temp = num; while(num>0) { r = num % 10; sum = (sum*10)+r; num = num/10; } if(temp == sum) return true; else return false; }; } }

acwest + 0 comments For completeness, I implemented a Sieve of Erathosthenes for the prime check. I also treated all negative numbers as not prime, although that can apparently be argued...

arnav_banerji + 0 comments you code is nice and simple and easy for beginners to understand those who are about to learn lambdas....thanks!! next step if you can migrate this code into something called as stream pipelining

codertoaster + 0 comments I never understood how lambda expressions worked until now, thanks a lot.

john_canessa + 0 comments 13 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 PRIME PRIME PRIME COMPOSITE PRIME COMPOSITE PRIME COMPOSITE PRIME COMPOSITE PRIME COMPOSITE PRIME

Not sure if the isPrime() method works. The loop seems to exit during the first iteration.

ajitpandit881 + 1 comment Solution.java:76: error: reached end of file while parsing } ^ 1 error

Getting this error because of missing closing bracket. Please fix this issue ASAP

slawomirkorbas + 1 comment There is import java.util.* but I was not able tyo use 'IntStream' in my solution. Eg:

public PerformOperation isPrime() { return num -> num > 1 ? IntStream.range(2, num - 1).filter( n -> num%n == 0 ).count() == 0 : false; };

Why IntStream cannot be resolved ? It is compiling in my IDE.

allxi + 0 comments try using java.util.stream.IntStream

mduran + 1 comment I'm getting

`Symbol not found`

for`IntStream`

:public PerformOperation is_prime() { return x -> IntStream.range(2, x).noneMatch(i -> x % i == 0); }

Crownless + 0 comments as imported only java.util.* When you can't import inner packages(java.util.stream) you have to use full path java.util.stream.IntStream instead of IntStream.

petes40 + 2 comments `public static PerformOperation is_odd() { return (int a) -> { return ((a & 1) == 1); }; } public static PerformOperation is_prime() { return (int a) -> { if (a < 2) return false; for (int i = 2; i * i <= a; i++) if (a % i == 0) return false; return true; }; } public static PerformOperation is_palindrome() { return (int a) -> { String str = Integer.toString(a); String str2 = ""; int i = 0; int j = str.length() - 1; for ( ; i <= j; ++i, --j) { if (str.charAt(i) != str.charAt(j)) return false; } return true; }; }`

wlsc1 + 1 comment Fixed code.

public static PerformOperation is_odd() { return (int a) -> { return ((a & 1) == 1); }; } public static PerformOperation is_prime() { return (int a) -> { if (a < 2) return false; for (int i = 2; i * i <= a; i++) if (a % i == 0) return false; return true; }; } public static PerformOperation is_palindrome() { return (int a) -> { String str = Integer.toString(a); String str2 = ""; int i = 0; int j = str.length() - 1; for ( ; i <= j; ++i, --j) { if (str.charAt(i) != str.charAt(j)) return false; } return true; }; } }

lahouari + 0 comments [deleted]

abishake1396 + 1 comment return (int a) -> {

} what does it mean?

rlniranjan + 0 comments Solution.java:59: error: cannot find symbol op = ob.isOdd(); ^ symbol: method isOdd() location: variable ob of type MyMath Solution.java:63: error: cannot find symbol op = ob.isPrime(); ^ symbol: method isPrime() location: variable ob of type MyMath Solution.java:67: error: cannot find symbol op = ob.isPalindrome(); ^ symbol: method isPalindrome() location: variable ob of type MyMath 3 errors

melih_burak_mert + 0 comments Awful exercise. Very bad explanation. These Java exercises are terrible. Isn't there any audit mechanism for the tests?

charles_qld + 1 comment My solution: public static PerformOperation is_odd() { return n -> n % 2 != 0; }

`public PerformOperation is_prime() { return n -> !java.util.stream.IntStream.rangeClosed(2, (int)java.lang.Math.sqrt(n)).anyMatch(x -> n % x == 0); } public PerformOperation is_palindrome() { return n -> { String s = String.valueOf(n); int len = s.length(); return java.util.stream.IntStream.rangeClosed(1, (int)len/2).anyMatch(p -> s.charAt(p-1) == s.charAt(len - p)); } ; }`

}

diptenduster + 0 comments public static PerformOperation isOdd(){ return (int a) -> a % 2 != 0; }

`public static PerformOperation isPrime(){ return (int a) -> java.math.BigInteger.valueOf(a).isProbablePrime(1); } public static PerformOperation isPalindrome(){ return (int a) -> Integer.toString(a).equals( new StringBuilder(Integer.toString(a)).reverse().toString() ); }`

Sort 59 Discussions, By:

Please Login in order to post a comment