Java 8

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Q #20) What is the difference between Intermediate and Terminal Operations in Stream?

All Stream operations are either Terminal or Intermediate. Intermediate Operations are the operations that return the Stream so that some other operations can be carried out on that Stream. Intermediate operations do not process the Stream at the call site, hence they are called lazy. These types of operations (Intermediate Operations) process data when there is a Terminal operation carried out. Examples of Intermediate operation are map and filter. Terminal Operations initiate Stream processing. During this call, the Stream undergoes all the Intermediate operations. Examples of Terminal Operation are sum, Collect, and forEach.

Q #12) What is a Predicate? State the difference between a Predicate and a Function?

Predicate is a pre-defined Functional Interface. It is under java.util.function.Predicate package. It accepts only a single argument which is in the form as shown below, Predicate<T> - Predicate returns Boolean, Function returns Object - Both accept one argument, both can be used as a target for a method reference

Q #4) What are the default methods?

Default methods are the methods of the Interface which has a body. These methods, as the name suggests, use the default keywords. The use of these default methods is "Backward Compatibility" which means if JDK modifies any Interface (without default method) then the classes which implement this Interface will break. On the other hand, if you add the default method in an Interface then you will be able to provide the default implementation. This won't affect the implementing classes.

Q #18) Write a program to print 5 random numbers using forEach in Java 8?

Random random = new Random(); random.ints().limit(5).forEach(System.out::println); /* limit is set to 5 which means only 5 numbers will be printed with the help of terminal operation forEach */

Q #19) Write a program to print 5 random numbers in sorted order using forEach in Java 8?

Random random = new Random(); random.ints().limit(5).sorted().forEach(System.out::println);

Q #3) What is an optional class?

Optional class is a special wrapper class introduced in Java 8 which is used to avoid NullPointerExceptions. This final class is present under java.util package. NullPointerExceptions occurs when we fail to perform the Null checks.

Q #14) What is a Stream API? Why do we require the Stream API?

Stream API is a new feature added in Java 8. It is a special class that is used for processing objects from a source such as Collection. We require the Stream API because, - It supports aggregate operations which makes the processing simple. - It supports Functional-Style programming. - It does faster processing. Hence, it is apt for better performance. - It allows parallel operations.

Q #17) What is the purpose of the limit() method in Java 8?

The Stream.limit() method specifies the limit of the elements. The size that you specify in the limit(X), it will return the Stream of the size of 'X'. It is a method of java.util.stream.Stream

Q #13) Is there anything wrong with the following code? Will it compile or give any specific error? @FunctionalInterface public interface Test<A, B, C> { public C apply(A a, B b); default void printString() { System.out.println("softwaretestinghelp"); } }

The code will compile because it follows the functional interface specification of defining only a single abstract method. The second method, printString(), is a default method that does not count as an abstract method.

Q #16) How will you get the current date and time using Java 8 Date and Time API?

java.time.LocalDate.now()); java.time.LocalTime.now()); java.time.LocalDateTime.now());

Q #21) Write a Java 8 program to get the sum of all numbers present in a list?

list.stream().mapToInt(i -> i).sum();

Q #5) What are the main characteristics of the Lambda Function?

- A method that is defined as Lambda Expression can be passed as a parameter to another method. - A method can exist standalone without belonging to a class. - There is no need to declare the parameter type because the compiler can fetch the type from the parameter's value. - We can use parentheses when using multiple parameters but there is no need to have parenthesis when we use a single parameter. - If the body of expression has a single statement then there is no need to include curly braces.

Q #7) What is the difference between the Collection API and Stream API?

- A stream does not store data. - An operation on a stream does not modify its source, but simply produces a result. - Collections have a finite size, but streams do not. - Like an Iterator, a new stream must be generated to revisit the same elements of the source.

Q #6) What was wrong with the old date and time?

- Java.util.Date is mutable and is not thread-safe whereas the new Java 8 Date and Time API are thread-safe. - Java 8 Date and Time API meets the ISO standards whereas the old date and time were poorly designed. - It has introduced several API classes for a date like LocalDate, LocalTime, LocalDateTime, etc. - Talking about the performance between the two, Java 8 works faster than the old regime of date and time.

Q #10) What is Method Reference?

A special type of lambda expression. They're often used to create simple lambda expressions by referencing existing methods. There are four kinds of method references: - Static methods - Instance methods of particular objects - Instance methods of an arbitrary object of a particular type - Constructor strList.forEach(System.out::println); https://www.baeldung.com/java-method-references

Q #8) How can you create a Functional Interface?

Although Java can identify a Functional Interface, you can define one with the annotation @FunctionalInterface Once you have defined the functional interface, you can have only one abstract method. Since you have only one abstract method, you can write multiple static methods and default methods. @FunctionalInterface interface FuncInterface { public int multiply(int a, int b); } public class Java8 { public static void main(String args[]) { FuncInterface Total = (a, b) -> a * b; // simple operation of multiplication of 'a' and 'b' System.out.println("Result: "+Total.multiply(30, 60)); } }

Q #2) What are Functional Interfaces?

Functional Interface is an interface that has only one abstract method. The implementation of these interfaces is provided using a Lambda Expression which means that to use the Lambda Expression, you need to create a new functional interface or you can use the predefined functional interface of Java 8. The annotation used for creating a new Functional Interface is "@FunctionalInterface".

Q #11) Explain the following Syntax String:: Valueof Expression

It is a static method reference to the ValueOf method of the String class. System.out::println is a static method reference to println method of out object of System class. It returns the corresponding string representation of the argument that is passed. The argument can be Character, Integer, Boolean, and so on.

Q #9) What is a SAM Interface?

Java 8 has introduced the concept of FunctionalInterface that can have only one abstract method. Since these Interfaces specify only one abstract method, they are sometimes called SAM Interfaces. SAM stands for "Single Abstract Method".

Q #1) List down the new features introduced in Java 8?

Lambda Expressions Method References Optional Class Functional Interface Default methods Nashorn, JavaScript Engine Stream API Date API

Q #15) What is the difference between limit and skip?

The limit() method is used to return the Stream of the specified size. For Example, If you have mentioned limit(5), then the number of output elements would be 5. Stream.of(0,1,2,3,4,5,6,7,8) .limit(6) /*limit is set to 6, hence it will print the numbers starting from 0 to 5 */ .forEach(num->System.out.print("\n"+num)); Whereas, the skip() method is used to skip the element. Stream.of(0,1,2,3,4,5,6,7,8) .skip(6) /* It will skip till 6th index. Hence 7th, 8th and 9th index elements will be printed */ .forEach(num->System.out.print("\n"+num));


Ensembles d'études connexes

(Complete) Chapter 13: Local markets, poverty, and Income Distribution

View Set

Vocabulary Workshop Word Families #11

View Set

JavaScript operator,arithmetic,assignment,Data Type,functions, objects

View Set