public Welcome(String na, Courses cs) { name = na; course = cs; }
public String getName() { return name; }
public Courses getCourses() { return course; }
public static void main(String[] args) { List couList = Arrays.asList ( new Welcome ("Jayesh", Welcome.Courses.HADOOP), new Welcome ("Imtiaz", Welcome.Courses.HADOOP), new Welcome ("Gautam", Welcome.Courses.SPARK)); Map> userMap = couList.stream () .collect(Collectors.groupingBy (Welcome ::getCourses,Collectors.mapping(Welcome::getName, Collectors.toList()))); System.out.println(userMap); } }
1. Compilation error
2. It will print {SPARK=[Gautam], HADOOP=[Jayesh, Imtiaz]}
3. It will print {Gautam =[ SPARK], Jayesh=[ HADOOP, HADOOP]}
4. It will print {Gautam =[ SPARK], Jayesh=[ HADOOP], Imtiaz=[HADOOP]}
Correct Answer : 2 Explanation: The arrow operator ("->") for defining lambda functions, the double colon operator ("::") used for method references. Method references use the "::" operator. Here is a simplified expression using method references: strings.forEach(System.out::println);
Method references route the given parameters. In this case, System.out::println is equivalent to using the lambda expression string -> System.out.println(string). How about simplifying the following statement to use method references? strings.forEach(string -> System.out.println(string.toUpperCase())); The lambda expression in this code calls toUpperCase() method on the given String object. Since method references just route the parameters, so you cannot use them directly for simplifying this lambda expression. An alternative is to put this code inside a method and use the reference of that method.
java.util.Collection.stream()
Returns a sequential Stream with this collection as its source. This method should be overridden when the spliterator() method cannot return a spliterator that is IMMUTABLE, CONCURRENT, or late-binding. (See spliterator() for details.)
The groupingBy() . Will do the grouping based on key, value. Key is the course name and values is the all the users who had subscribed to the course.
Question : You have been given below code, what will be the behavior of the code ? package com.hadoopexam;
public Welcome(CompanyName pType) { companyName = pType; }
public static void main(String[] args) { CompanyName cName = new CompanyName(); Welcome wel = new Welcome(CompanyName.HADOOPEXAM); } } 1. It will compile and run perfectly
2. It will compile but will run with Error
3. It will run perfectly and print the HADOOPEXAM
4. It will give compile time error.
Correct Answer : 4 Explanation: It can not compile, because you are trying to instantiate enum CompanyName using new operator. Which you can not do.
Question : You have been given below code, what behavior you are expecting.
package com.hadoopexam;
public enum Welcome {
private int totalProducts; // n1 HadoopExam(30), QuickTechie(5), Training4Exam(50); // n2
public int getTotalProducts() { return totalProducts; } } 1. It will compile successfully
2. It will compile, but can not run successfully.
3. This is enum definition is not correct, it will through compile time errors.'
4. It will compile and run successfully.
Correct Answer : 3 Explanation: When you define an enum class then , enum elements should be in first, before appearing any other enum. Hence, in given code n2 line should be above line n1.