Tuesday, April 26, 2022

Java -8 Interview Questions

 Java- 8 Interview Question

netstat -ano | findstr :8080 taskkill /PID {your process id} /F



  1.  What is Type Inference?

Type inference helps the compiler determine the argument types by looking at each method invocation and corresponding declaration.



2. Why are default methods needed in the interface?

Default methods let you add new functionality to your libraries’ interfaces and ensure binary compatibility with older code written for the interfaces.



3. What is Java 8 StringJoiner class used for?

Java 8 StringJoiner class constructs a sequence of characters separated by a delimiter so that users can create a string by passing delimiters such as hyphens and commas.


package com.example.demo.java_8.StringJoiner;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StringJoinerTest {

public static void main(String[] args) {

List<String> str = Arrays.asList("Hello", "World", "Come", "to", "the", "World");
String afterModifyStr = str.stream().collect(Collectors.joining("-", "{(", ")}"));
System.out.println(afterModifyStr);

}

}
OUTPUT:-

{(Hello-World-Come-to-the-World)}


4. Describe the more commonly found functional interfaces in the standard library.

Although many functional interfaces exist, these are the one's users most likely encounter:

  • Function. Takes one argument and returns a result....... Function<T,R> ~~ apply()
  • Consumer. Takes one argument and returns no result....Consumer<T>~~ accept()
  • Supplier. Takes a not argument and returns a result....Supplier<T>~~ get()
  • Predicate. Takes one argument and returns a boolean.....Predicate<T> ~~ test()
  • BiFunction. Takes two arguments and returns a result
  • BinaryOperator. It’s like a BiFunction, except it takes two arguments and returns a result, and they are all the same type
  • UnaryOperator. It’s like a Function, but it takes a single argument and returns a result of the same type

Function (Program Example)



Date API



ALL


BiFunctions



BiPredicate


BiConsumer