### Functional Interfaces and Lambda Expressions
------------------------------------------------
Use the below class
public class LambdaSample {
private static List<String> names=new ArrayList<>();
private static Map<String,String> studentDetails;
static {
names=Arrays.asList( "Anju","Raju","Arun","Vikram");
studentDetails=new HashMap<String,String>();
studentDetails.put("Anju","Delhi");
studentDetails.put("Raju","Chennai");
studentDetails.put("Arun","Delhi");
}
}
Implement below static methods and access them from main method . Use Java8 functions
1. displayNames()
--should displayall the names
2. findByName(String name)
-- check whether the given name exists in the list
3. getAllcount()
-- count the number of person whose name starts with "A"
4. getStduentData()
-- use biconsumer and print the student details
5. showMaxlength()
-- display the list of names which is length to be 4 characters
import java.util.*;
public class LambdaSample {
private static List<String> names = new ArrayList<>();
private static Map<String, String> studentDetails;
static {
names = Arrays.asList("Anju", "Raju", "Arun", "Vikram");
studentDetails = new HashMap<String, String>();
studentDetails.put("Anju", "Delhi");
studentDetails.put("Raju", "Chennai");
studentDetails.put("Arun", "Delhi");
}
// --should displayall the names
public static void displayNames() {
names.forEach(System.out::println);
}
//-- check whether the given name exists in the list
public static boolean findByName(String name) {
return names.stream().anyMatch(nameS -> nameS.equals(name));
}
//-- count the number of person whose name starts with "A"
public static long getAllCount() {
return studentDetails.entrySet().stream()
.filter(entry -> entry.getKey().startsWith("A"))
.count();
}
//-- use biconsumer and print the student details
public static void getStudentData() {
studentDetails.forEach((s, s2) -> System.out.println(s + " " + s2));
}
// -- display the list of names which is length to be 4 characters
public static void showMaxLength() {
names.stream()
.filter(name -> name.length() == 4)
.forEach(System.out::println);
}
}
public class Main {
public static void main(String[] args) {
LambdaSample.displayNames();
System.out.println(LambdaSample.findByName("Arun"));
System.out.println(LambdaSample.getAllCount());
LambdaSample.getStudentData();
LambdaSample.showMaxLength();
}
}
Comments
Leave a comment