Sush Java 7,8
Sush Java 7,8
Ex no: 7
PROGRAM USING FUNCTIONAL INTERFACE
Date:
AIM:
To write a java program using functional interface.
7.1 QUESTION :
Write a lambda expression for the following tasks
• find the maximum value in the array,
• find the minimum value in an array,
• counts the occurrence of a given value occurs in an array
• find the sum of the values in the array, and
• find the average of the values in the array.
PROGRAM :
717822Y155-SUSHMA J 51
21YE04 – ADVANCED JAVA PROGRAMMING
OUTPUT:
717822Y155-SUSHMA J 52
21YE04 – ADVANCED JAVA PROGRAMMING
7.2 QUESTION :
Write a lambda expression for the following tasks
• Create 5 Employee object and store it in a list
• Print the Name and email of all the employee objects in the list
• Print the employee details those who are earning above 30,000.
• Print all the employee details with the skill set "Java"
• Print all the employee details with the designation "Developer"
PROGRAM :
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
class Employee {
int id;
String name;
double salary;
String designation;
List<String> skills;
public Employee(int id, String name, double salary, String designation, List<String> skills) {
this.id = id;
this.name = name;
this.salary = salary;
this.designation = designation;
this.skills = skills;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
", designation='" + designation + '\'' +
", skills=" + skills +
'}';
}
}
717822Y155-SUSHMA J 53
21YE04 – ADVANCED JAVA PROGRAMMING
717822Y155-SUSHMA J 54
21YE04 – ADVANCED JAVA PROGRAMMING
OUTPUT:
717822Y155-SUSHMA J 55
21YE04 – ADVANCED JAVA PROGRAMMING
7.3 QUESTION :
Write a lambda expression for the following tasks
• Create 5 Address object and store it in a list
• Print the address details which has the pin Code 641032.
• Print the address details which has the city Coimbatore.
• Print all the address details which has multiple phone numbers.
PROGRAM :
import java.util.*;
import java.util.function.Predicate;
class Address {
String doorNo;
String street;
String city;
long pinCode;
Set<Long> phoneNumbers;
public Address(String doorNo, String street, String city, long pinCode, Set<Long> phoneNumbers) {
this.doorNo = doorNo;
this.street = street;
this.city = city;
this.pinCode = pinCode;
this.phoneNumbers = phoneNumbers;
}
@Override
public String toString() {
return "Address{" +
"doorNo='" + doorNo + '\'' +
", street='" + street + '\'' +
", city='" + city + '\'' +
", pinCode=" + pinCode +
", phoneNumbers=" + phoneNumbers +
'}';
}
}
public class Main {
public static void main(String[] args) {
717822Y155-SUSHMA J 56
21YE04 – ADVANCED JAVA PROGRAMMING
// Print the address details which has the pin Code 641032
System.out.println("Addresses with pin Code 641032:");
Predicate<Address> pinCodePredicate = address -> address.pinCode == 641032;
addresses.stream()
.filter(pinCodePredicate)
.forEach(System.out::println);
// Print all the address details which has multiple phone numbers
System.out.println("\nAddresses with multiple phone numbers:");
Predicate<Address> multiplePhonePredicate = address -> address.phoneNumbers.size() > 1;
addresses.stream()
.filter(multiplePhonePredicate)
.forEach(System.out::println);
}
}
717822Y155-SUSHMA J 57
21YE04 – ADVANCED JAVA PROGRAMMING
OUTPUT:
717822Y155-SUSHMA J 58
21YE04 – ADVANCED JAVA PROGRAMMING
Ex no: 8
PROGRAM USING LAMBDA EXPRESSIONS
Date:
AIM:
To write a java program using lambda expressions.
8.1 QUESTION :
Write a Java program to print Fibonacci series for integer value n. Use Function<T> functional
interface and lambda expression that accepts integer value n and print Fibonacci series.
PROGRAM :
import java.util.function.Function;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("717822y155 SUSHMA ");
System.out.print("Enter a Number : ");
int n = sc.nextInt();
Function<Integer, Void> fibonacci = (num) -> {
int prev = 0;
int next = 1;
System.out.println("Fibonacci Series for n = " + num + ":");
for (int i = 0; i < num; i++) {
System.out.print(prev + " ");
int sum = prev + next;
prev = next;
next = sum;
}
return null;
};
fibonacci.apply(n);
}
}
717822Y155-SUSHMA J 59
21YE04 – ADVANCED JAVA PROGRAMMING
OUTPUT:
717822Y155-SUSHMA J 60
21YE04 – ADVANCED JAVA PROGRAMMING
8.2 QUESTION :
Write a Java program for the generation of OTP (One Time Password) as 6-digit number using
Supplier interface. Use random () method to generate the OTP.
PROGRAM :
import java.util.Random;
import java.util.function.Supplier;
OUTPUT:
717822Y155-SUSHMA J 61
21YE04 – ADVANCED JAVA PROGRAMMING
8.3 QUESTION :
Generate a secure random password in Java using Supplier interface. The criteria for a valid password:
• The length of the password should be eight characters.
• The first and last character should be capital letters.
PROGRAM :
import java.security.SecureRandom;
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
Supplier<String> passwordSupplier = () -> {
System.out.println("717822y155 SUSHMA ");
String capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
SecureRandom random = new SecureRandom();
StringBuilder password = new StringBuilder();
password.append(capitalLetters.charAt(random.nextInt(capitalLetters.length())));
for (int i = 0; i < 6; i++) {
char randomChar = (char) (random.nextInt(26) + 'a');
password.append(randomChar);
}
password.append(capitalLetters.charAt(random.nextInt(capitalLetters.length())));
return password.toString();
};
String password = passwordSupplier.get();
System.out.println("Generated Password: " + password);
}
}
OUTPUT:
717822Y155-SUSHMA J 62
21YE04 – ADVANCED JAVA PROGRAMMING
8.4 QUESTION :
Write a Java program to create a list of strings and convert lowercase letters to uppercase letters using
stream map () function.
PROGRAM :
import java.util.*;
import java.util.stream.Collectors;
OUTPUT:
RESULT:
Thus the program using lambda expressions for case convertion was executed successfully and the
output was verified.
717822Y155-SUSHMA J 63
21YE04 – ADVANCED JAVA PROGRAMMING
717822Y155-SUSHMA J 64