# SKP's Algorithms and Data Structures #7: Functional Programming and Java Lambdas

### This Article Series Focuses on Algorithms, Data Structures, or Applying them to Problem Solving. In this Article, We Discuss the Solution to [Functional Programming] Problem to Demonstrate Usage of Java Lambdas.

· Java Zone · Code Snippet
Save
2.11K Views

[Question/Problem Statement is the Property of Techgig]

Java Advanced — Lambda Expressions [www.techgig.com]
Write the Following Methods that Return a Lambda Expression Performing a Specified Action: Perform Operation isOdd(): The Lambda Expression must return if a Number is Odd or  If it is Even. Perform Operation isPrime(): The lambda expression must return if a number is prime or if it is composite. PerformOperation isPalindrome(): The Lambda Expression must return if a number is a Palindrome or if it is not.

Input Format
Input is as Show in the Format Below
Input
3
1 3
2 7
3 7777

Constraints
NA

Output Format
Output is as Show in the Format Below
Output
ODD
PRIME
PALINDROME

[Explanation of the Solution]
This is a Good Question to Refresh Java 8 Lambdas. In my Solution, I Implemented the Functional Interfaces within my `main()` Method and assigned it to Local Reference Variables.

[Source Code, Sumith Puri (c) 2021 — Free to Use and Distribute]
Java

`xxxxxxxxxx`
1
76

1
` import java.util.Scanner;`
2

3
`/*    `
4
` * Techgig Core Java Basics Problem - Knock Off Java Lambdas!   `
5
` * Author: Sumith Puri [I Bleed Java!] // GitHub: @sumithpuri   `
6
` */`
7
`interface LambdaYogi {`
8
`    public boolean opYog(int x);`
9
`}`
10

11
`public class CandidateCode {`
12
`    public static void main(String args[]) throws Exception {`
13

14
`                // you may choose to refactor, as this method`
15
`                // becomes really long and unmanageable #TODO`
16
`        LambdaYogi isOdd = a -> {`
17
`            boolean retFlag = false;`
18
`            if (a % 2 != 0)`
19
`                retFlag = true;`
20
`            return retFlag;`
21
`        };`
22
`        LambdaYogi isPrime = a -> {`
23
`            boolean retFlag = true;`
24
`            for (int i = 2; i < a - 1; i++) {`
25
`                if (a % i == 0) {`
26
`                    retFlag = false;`
27
`                    break;`
28
`                }`
29
`            }`
30
`            return retFlag;`
31
`        };`
32
`        LambdaYogi isPalindrome = a -> {`
33
`            boolean retFlag = false;`
34
`            String actStr = String.valueOf(a).trim();`
35
`            String revStr = new StringBuffer(actStr).reverse().toString();`
36

37
`            // using string basis, not mathematical`
38
`            if (actStr.equals(revStr))`
39
`                retFlag = true;`
40
`            return retFlag;`
41
`        };`
42

43
`        Scanner scanner = new Scanner(System.in);`
44
`        int val = scanner.nextInt();`
45

46
`        for (int i = 0; i < val; i++) {`
47
`            int op = scanner.nextInt();`
48
`            int no = scanner.nextInt();`
49
`            switch (op) {`
50
`            case 1: {`
51
`                if (isOdd.opYog(no))`
52
`                    System.out.println("ODD");`
53
`                else`
54
`                    System.out.println("EVEN");`
55
`                break;`
56
`            }`
57
`            case 2: {`
58
`                if (isPrime.opYog(no))`
59
`                    System.out.println("PRIME");`
60
`                else`
61
`                    System.out.println("COMPOSITE");`
62
`                break;`
63
`            }`
64
`            case 3: {`
65
`                if (isPalindrome.opYog(no))`
66
`                    System.out.println("PALINDROME");`
67
`                else`
68
`                    System.out.println("NONPALIN");`
69
`            }`
70
`            }`
71
`        }`
72
`    }`
73
`}`

Happy Problem Solving using Java

Topics:
techgig, core java, algorithms, data structures, problem solving, mathematical programming, complexity metrics, competitive, iq, java program

Published at DZone with permission of Sumith Puri, DZone MVB.

Opinions expressed by DZone contributors are their own.