Javatpoint Logo
Javatpoint Logo

Java 8 Interview Questions

Java has been famously appreciated for appending several innovative characteristics from period to period. Java 8 SE was the latest deliverance, or we can say the latest version of the Java programming language development project. Java SE 8 was issued on 18th March 2014 with the goal of overcoming the deceptions and disadvantages of the previous editions of Java.

Java SE 8 has upgraded the Java programming language by introducing many new features and characteristics. Java 8 SE has been created to append a functional programming ability, build a modern JavaScript search engine, handle the date-time with the latest APIs, and extend a new streaming API.

Java 8 Interview Questions

Presently this has been six years after the deliverance of Java SE 8. The employment market is loaded with Java 8 interview problems. This report would step you within the Java 8 interview problems for newbies and experienced, scope, and possibilities. These are really well-known problems that will be summoned to you all in several Java interviews and discussions that require high-level abilities.

These concepts are a must-have if you are going to appear for any standard Java Certification Exams like Oracle Certified or going for an excellent job opportunity. With the help of these topics, you can acquire insight and in-depth knowledge regarding how to advance the questions and how to explain them effectively, thereby assisting you to ace the discussions and interviews.

The most essential Java 8 interview questions are given below, which will help you to ace the technical interviews.


1) Why is there a need for changes and updates in the Java programming language again and again?

Nowadays, as technology has advanced vigorously, there are many changes in computer hardware as well. In today's world, computer systems are running on Multi-core CPUs to deploy and control computer applications. So, to go on with the technology, it is necessary to release new updates and versions of Java from time to time.

We need to introduce new concepts and features in the Java programming language at regular intervals in order to:

  • To utilize the characteristics of Functional programming.
  • To use the modern technology of Multi-Core CPUs efficiently.

2) What do you mean by the given phrase?

Java SE 8 supports functional programming.

Before Java 8 everything was very focused on the object. Apart from the first things in Java they existed as objects. All call methods / functions should be made using sectional objects or indicators. Methods / Tasks were not stand alone.

By Java 8, the operating system has been installed. So, we can use anonymous functions. Java is a basic language. With the exception of classic data types, everything in Java is a thing. Even the same members are a thing. Each category creates realistic situations. There is no way to describe the work / way of living in Java alone. There is no way to pass a path like a counter or return the body of the path to that example.


3) What are the new features and characteristics that were introduced in the Java SE 8?

Java 8 Interview Questions

There are several important features and characteristics that are essential for the development using the Java programming language that was implemented in Java SE 8. These features are described as follows:

  • Lambda Expression: Lambda expressions are the functions and expressions that are shared and pointed to as an object.
  • Functional Interfaces: Functional Interfaces are the types of interfaces that can contain only a single abstract method. These interfaces are additionally recognized as Single abstract method interfaces.
  • Method References: Method references are the references that use a function as a parameter to request a method.
  • Default Method: The default method is a method that provides an implementation of methods within interfaces allowing the Interface development facilities.
  • Stream API: The Java Stream API is an abstract layer that implements the pipeline processing of the data.
  • Date Time API: In Java SE 8, the latest, updated Joda-time stimulated APIs are introduced to overcome the disadvantages of the previous versions.
  • Optional: Optional is a wrapper class included in Java SE 8 that is used to control the imaginary (null) values and assists in additional processing based upon the value.
  • Nashorn: A JavaScript Engine: Nashorn is a devised variant of JavaScript Engine introduced in Java SE 8 that provides JavaScript performances in Java to succeed Rhino.

4) What are the advantages of the Java SE 8?

The introduction of Java SE 8 has been determined to be extremely valuable for programmers in several ways. These are:

  1. It makes the code more concise and understandable. Functional Interfaces and Lambda Expressions in Java SE 8 made the code more reusable.
  2. In Java 8, code can be maintained and tested more quickly as compared to previous versions.
  3. It helps in writing extraordinarily interfaces and highly scalable code.
  4. It helps in writing parallel code.
  5. It made writing database-like operations easy.
  6. It provides better performance to different applications
  7. Code can be made more productive in Java SE 8.

5) What are the Java Lambda Expressions?

Java lambda expressions were Java's initial step towards functional programming. Java Lambda Expression or Lambda function is simply an unnamed expression or function that is rewritten as a parameter for any other function. Lambda Expressions in Java are the functions that are used to be shared as an object. It can also be used for an object to be referenced. Lambda Expressions need more concise coding, include and it also implements a method of executing the functional interfaces of Java 8. Lambda expressions in Java allow the users to express one functioning unit to carry throughout to different code.

Java 8 Interview Questions

Lambda Expressions Syntax:

or

Example of Lambda Expression in Java:

LambdaExpressionExample.java:

Output:

Thread is started: using the old method
Thread is started: using Lambda Expressions

6) Explain the syntax of lambda expressions in-brief?

Java 8 Interview Questions

The syntax of Lambda Expressions in Java consists of three main components. These components are shown below:

  • Argument List: The argument list is written inside the brackets in the syntax of Java lambda expressions. The argument list can be empty and can contain parameter or parameters as well.
  • Arrow Token: The arrow token is represented by an arrow (->) in Java lambda expressions. It is used to link the parameters in the argument list to the expression of the body.
  • Body: Java Lambda expression's body is written just after the arrow token. The body comprises the expressions and statements of the lambda expressions in Java.

Syntax:


7) What do you mean by Functional Interfaces in Java?

Functional interfaces are included in Java SE 8 with Lambda expressions and Method references in order to make code more readable, clean, and straightforward. These are the interfaces which ensure that they include precisely only one abstract method. It is used and executed by representing the interface with an annotation called @FunctionalInterface. As described earlier, functional interfaces can contain only one abstract method. However, they can include any quantity of default and static methods.

Functional Interface is additionally recognized as Single Abstract Method Interfaces. In short, they are also known as SAM interfaces.


8) Describe the guidelines that the users should follow while using the Java Functional Interfaces?

In the Java programming language, the user needs to follow many guidelines while using the Functional Interfaces of Java. These guidelines are:

  • The functional interface should contain only one abstract method. However, it can contain any number of static and default methods.
  • In Java functional interfaces, only one abstract method is allowed. We cannot use more than one abstract method in Functional interfaces.
  • The user should use the annotation @Functionalinterface while defining the functional interface in Java.
  • In functional interfaces, if we override the Java.lang.object class's method in the interface, it will not be counted as an abstract method.
  • We can use any method in order to define a number.

9) Name any four built-in Java Functional Interfaces?

Since Java SE 1.8 onwards, there are many interfaces that are converted into functional interfaces. All these interfaces are annotated with @FunctionalInterface. These interfaces are as follows:

  • Runnable: The interface only contains the run() method.
  • Comparable: This interface only contains the compareTo() method.
  • ActionListener: This interface only contains the actionPerformed() method.
  • Callable: This interface only contains the call() method.

10) Explain some of the functional interfaces which are used in Java?

There are many functional interfaces that are used in Java. The following are commonly used by the developers.

Function: Function is the most common functional interface out of all. It accepts only one argument and returns a specified result

Consumer: It is the same as Function interface, Consumer is also a functional interface that takes only one argument, and no result is returned by it.

Supplier: Supplier is a type of functional interface in Java that does not accept any argument and still returns the desired result.

Predicate: The type of functional interface in Java that accepts one argument and returns a boolean value is known as Predicate functional interface.

BiFunction: The Bi-Function is substantially related to a Function. Besides, it takes two arguments, whereas Function accepts one argument. Just like Function, it also returns a specified result.

Java 8 Interview Questions

Unary Operator and Binary Operator: There are also two other function interfaces that are named as Unary Operator and Binary Operator. They both extend the Function and Bi-Function, respectively. In simple words, Unary Operator extends Function, and Binary Operator extends Bi-Function.

The Unary Operator accepts only one argument and returns a single argument only. Still, in Unary Operator, both the input and output values must be identical and of the same type. On the other way, Binary Operator takes two values and returns one value comparable to Bi: Function, but similarly, like Unary Operator, the input and output value type must be identical and of the same type.


11) Describe in brief about the method references in Java?

Method references were included in the Java programming language in the most advanced variant of Java, i.e., Java SE 8. Method references in Java are an extra unquestionably admirable trait that is introduced to Java SE 8. Lambda Expressions are simply gained extra vanity and flexibility with the guidance of Method references. Whereas Method references in Java SE 8 are a specific brand of the lambda expression, which invoke (reference) the methods by applying a method name.

In plain words, Method references in Java have typically reduced Lambda Expressions that are used for invoking methods. Method references are used when the lambda expression is declaring a function and doing nothing more. Two Integer colons (::) means method reference in Java.

Example of Method References in Java:

Example1.java

Output:

JavaTpoint is a great website to learn. 

12) Explain in brief the syntax of method references in Java?

The syntax of a Java method reference is distributed in two parts.

These are:

  • Class / Object
  • Method / Constructor

Both the parts are parted by integer colons (::). There are no additional parameters transferred with the method reference.

Syntax:


13) What are the types of method references in Java?

Java 8 Interview Questions

Java SE 8 allows a total of three types of method references. These are:

Static Method Reference: It is used to refer to static methods from a class.

Syntax of static method reference:

Reference to an Instance Method: In this type of method reference in Java, we can refer to an instance method using a reference to the provided object and by anonymous object also. We can also refer to methods by class object and unidentified object.

The syntax of object instance method reference is:

Reference to a Constructor: In this type of method reference in Java, we can refer to a constructor using the new keyword. It references to a constructor can also be done with the assistance of a functional interface.

The syntax of a reference to a constructor in method references in Java is:


14) In Java SE 8, what do you mean by Streams?

Java implements a newly added package in Java SE 8 known as java.util.stream. The package contains many different interfaces, classes, and enum, that allows the user to perform functional-style operations on the elements. The entire idea of the Java streams is to facilitate functional-style processes on streams of elements.

A Java stream cannot be said to be a data structure. However, we can say that Java Stream is an abstraction. However, Java Streams is not a collection or set where we can save elements and store data. The primary relevant difference between a structure and a stream is that a stream doesn't store the data and components. A stream is a reflection of a non-changeable compilation of processes that are applied in any classification of the data.

Java 8 Interview Questions

In simple words, a Java Stream is a package that contains various classes and interfaces which are capable of doing the iteration of its own elements internally. Conversely, when we are working with iteration characteristics of the Java Collections, then we have to perform the iteration of the elements personally. (For example - we can use a Java Iterator or the Java for each loop in order to work with a Java Iterable).


15) What are the two types of common operations of Java Streams?

Java Stream holds two different types of operations that are:

Intermediate operations: Intermediate operations are the operations that return a stream so that the user can chain various intermediate operations without using semicolons, as we do in other programming languages like Scala.

Terminal operations: The terminal operations are the operations that are mainly void and null, and if not null, these operations return a non-stream as a result.

Java 8 Interview Questions

16) What are the differences between Collections and Streams?

S. N. Collections Streams
1. Collection API is open to use since Java SE 1.2. Streams API were included in Java SE 8.
2. The collections framework is used to store data. In simple words, it is used to store a set of objects. Streams are used in computing data. In simple words, it is used in the computation of a set of objects.
3. In Collections API, the iteration of elements can be done with the help of both the Spliterator and Iterator. The forEach() method can also be used by the user to perform a method on every element which is in the stream. In the Streams API, the users are not allowed to iterate the elements of the stream using the Spliterator and Iterator.
4. It is used to save an infinite quantity of elements. If the users want to do the processing on the elements of the Collections, then at that moment, Streams API is used.
5. Collections API uses the external iteration idea to iterate the elements of the stream, such as the Iterator. Stream API does internal iteration to iterate the elements of the stream. Streams API uses the forEach() method to do the internal iteration.
6. The objects of the Collections API are not lazily constructed. The objects of the Collections are constructed eagerly. The objects of the Streams API are lazily constructed.
7. In the Collections, when the collection object is computed entirely, then only the user can add elements to the object of the collection. In the Streams, when the stream object is computed entirely, then only the user can add elements to the object of the stream. In simple words, we can say that the Streams objects are computed on the demand of the user.
8. Whenever a user is using the Collections API, they can iterate and use elements from the object of the collection at any number of times. Whenever a user is using the Streams API, they can iterate and use elements from the object of the stream only a single time.

Java 8 Interview Questions

17) Explain some of the Intermediate operations which are used commonly in the Java programming language?

Some of the Intermediate operations that are used commonly in the Java are:

Stream.filter(): The Java Stream filter() method is one of the significant operations under intermediate operations. The filter() method accepts a Predicate to separate all components of the stream. It is under intermediate operations because it enables the users to call another stream operation (for example - Stream.forEach() method) on the result. The predicate should declare true if the component is to be introduced in the final Stream, and the Predicate will reflect false if the component should not be included in the final stream.

Stream.map(): The intermediate operation Stream.map(), with the help of the given function, transforms each component in the stream into some other object. In simple words, the Stream.map() operation converts the element of a stream into something else. It takes the given function and applies that function to every element of the stream, which at the end returns a stream of the values that are produced by the parameter function. Stream.map() also supports the user to create a calculation on the data inside a Java stream. For example, if a user has a list of strings, the user can transform each string from the given list to uppercase, lowercase, or to a substring of the original string and something entirely else.

Stream.sorted(): The Java Stream sorted() method is also an intermediate operation or method. This sorted() operation returns a classified (or we can say the sorted) view of the stream. The components in the stream are sorted in a usual manner unless the user passes or uses a custom Comparator if they want to sort the stream in a different way.

Java 8 Interview Questions

18) Explain some of the Terminal operations which are used commonly in the Java programming language?

Some of the Terminal operations which are used commonly in the Java programming language are:

Stream.forEach(): The Java Stream forEach() method or operation is one of the terminal operations in which starts the internal iteration of the components in the stream. In simple words, the Java Stream forEach() method helps in repeating over all of the components of a stream and execute some process on each of them. The forEach() method returns nothing, or we can say void. The process to be performed on the elements of the stream is transferred as the Java lambda expression.

Stream.collect(): The Java Stream.collect() method is practiced to take elements from a stream and save all of them in a group. Stream collect() is a method to get away from the environment of streams and receive a solid combination of values, like an array list or a list. The Java Stream collect() method is a terminal operation that starts the internal iteration of elements and collects the elements in the stream in a collection or object of some kind.

Stream anyMatch(): The anyMatch() method of Java stream is also one of the terminal operations that inputs a single Predicate or condition as a parameter and starts the internal iteration of the Stream. The anyMatch() method implements the Predicate parameter to every component of the Stream. If the given Predicate reflects true for any of the components of the stream, the anyMatch() method yields true. If no elements match the Predicate, anyMatch() will return false.

Stream allMatch(): The allMatch() method of Java stream is also one of the terminal operations that inputs a single Predicate or condition as a parameter and starts the internal iteration of the Stream. The allMatch() method implements the Predicate parameter to every component of the Stream. If the given Predicate reflects true for all of the components of the stream, the allMatch() method yields true. If not all the elements match the Predicate, the allMatch() will return false.

Stream noneMatch(): The noneMatch() method of Java stream is also one of the terminal operations that inputs a single Predicate or condition as a parameter and starts the internal iteration of the Stream. The noneMatch() method implements the Predicate parameter to every component of the Stream. The noneMatch() method will return true if no elements are matched by the Predicate and will return false if one or more elements are matched with the predicate.

Stream.count(): The Java Stream count() method is also a terminal operation that reflects the number of components present in the stream. The number of elements present in the stream is produced and returned in the form of a long return type. In simple words, the Java Stream count() method starts the internal iteration of the elements in the Stream and counts the elements present in it.

Stream.reduce(): The Java Stream reduce() method is also a terminal operation that helps in reducing all the components of the stream to only a single component. The reduce() method performs a reduction on the components of the stream with the provided function. The result of the reduce() method is an Optional (Optional is a process of restoring a nullable T reference with a non-null value), which holds the reduced value of the stream.


19) What do you mean by Default methods in Java SE 8?

The methods of the Java interface, which comprises the body solely, are acknowledged as the default methods in Java. These methods, as the name implies, use default keywords. The convenience of working with these default methods is "Backward Compatibility." B??kw?rd ??m??tibility means, if JDK transforms any Interf??e (with?ut the def?ult meth?d), then the ?l?sses whi?h im?lement this Interf??e will bre?k.

?n the ?ther h?nd, if the user ?dds the def?ult meth?d in ?n interf??e, then they will be ?ble t? ?r?vide the def?ult im?lement?ti?n. This w?n't ?ffe?t the im?lementing ?l?sses.

Syntax:


20) What are the major points of differentiation between the Iterator and the Spliterator?

Java 8 Interview Questions
Difference Iterator Spliterator
Introduction The Iterator was introduced in Java SE 1.2. The Spliterator was introduced in Java SE 1.8.
API uses In Collections API, the Iterator is used. In Streams API, the Spliterator is used.
Parallel Programming The user can iterate the Stream elements in sequential order with the help of Iterator. The user can iterate the Stream elements both parallelly as well as in sequential order with the help of Spliterator.
Universal Iterator Iterator can be called as a universal iterator. Spliterator cannot be called as a universal iterator.

21) What is an ideal situation where the user can use the Streams API in Java?

The Stream API in Java 8 can be efficiently utilized in many scenarios. These scenarios are:

  • Streams API are used where the user has to perform database operations.
  • Streams API are used to execute operations lazily.
  • IT helps in writing functional-style programming.
  • It is used in performing parallel processing.
  • Streams API are used in projects where the project demands the use of pipeline operations.
  • In order to achieve internal iteration, Streams API is very useful.

22) Using Java 8 Date and Time API, how can a user get the current date and time?

LocalDate/LocalTime and LocalDateTime classes analyze the growth where timezones are not needed. With Java SE 8, an innovativeDate-Time API is included to satisfy the subsequent shortcomings of the past date-time API. The underneath program is addressed with the guidance of the latest date-time API launched in Java SE 8. In order to get the current date and time, we have to use the built utilization of LocalDate, LocalTime, and LocalDateTime API to get the actual date and time.

In the below code, in the first and second print statements, we have recovered the popular date and time from the system timepiece with the time-zone set as default. In the third print statement, we have used LocalDateTime API, which will print both date and time.

Java8DateTime.java

Output:

Current Local Date: 2021-07-17
Current Local Time: 17:39:37.248
Current Local Date and Time: 2021-07-17T17:39:37.248

23) Explain the use of the Optional in Java SE 8?

Java 8 Interview Questions

Java 8 has launched a unique class, Optional in java.util package. It can assist in drafting a clean code without practicing multiple null tests. By working with Optional, we can define substitute values to restore or alternate code to run. This makes the code more understandable because the actions which were deceived are now obvious to the developer.

Optional can also be defined as an object of container which might contains or not contain a non-null value. You need to import the java.util package to run this class. If the situation exists, isPresent () will return true and get () will return the value. Additional methods based on the presence or absence of a value obtained are introduced, such as orElse (), which provides the default value when the value is not present, and ifPresent (), which creates a chunk of code when the value is present. This is the category based on the number, that is, their cases:

  • Final and immutable (though may include references to mutable objects).
  • Acknowledged even individually based on equals(), not based on reference equation(==).
  • Do not have available constructors.

Example

OptionalDemo.java

Output:

word is null

24) Explain Nashorn Javascript engine in Java SE 8?

Nashorn is a name given to a JavaScript engine that is included in JDK SE 8. With the guidance of Nashorn, it became easier for the user to produce the JavaScript code at Java virtual machine (JVM). Nashorn is included in the latest version of Java programming language that is Java SE 8 in order to help in restoring the existing JavaScript engine which is commonly known as Rhino. Nashorn is considerably more reliable than Rhino in phases of production.

The use of summoning dynamic characteristics, regeneration of the code of JavaScript directly into the bytecode straight inside the storage, etc., performs the Nashorn extra prominent in JDK 8. The user can accomplish the code of JavaScript by practicing the command-line tool plus by installing that code of JavaScript into the source code of Java.


25) Define PermGen and MetaSpace. Also mention the points of differentiation between PermGen and MetaSpace?

Java 8 Interview Questions

PermGen is known as the memory space which is used for collecting class data like byte code, static variables, and many more. The space allocated to PermGen is by default 64Mb. The PermGen can also be attuned with the help of -XXMaxPermSize.

In Java SE 8, the PermGen process area was restored with MetaSpace. Java developers have transferred permGem to the unique consciousness in the original OS, which is now commonly known as MetaSpace. MetaSpace can auto-enhance its capacity, by default. In MetaSpace, the classes can store and discharge during the total life sentence of the Java Virtual Machine (JVM).

Differences between PermGen and MetaSpace

  • Basic: PermGen is the memory operation for saving class data like a static variable, byte code, etc. In Java SE 8, the PermGen purpose section was substituted with MetaSpace.
  • Default Memory Allocation: The space allocated to PermGen is by default 64Mb. In metaSpace, it can, by default, auto increase its size.
  • Tuned-up Memory Flag: PermGen can be harmonized by applying-XXMaxPermSize. In metaSpace, we can reduce the upper bound of the memory by -XX: MaxMetaspaceSize.
  • Memory Area - PermGen is a unique Heap space. Since the introduction of Java SE 8, MetaSpace is now including a separate storage space in the original Operation System (OS).

26) Differentiate between the Internal and External iteration of Java SE 8?

Internal Iteration External Iteration
Internal Iteration was advanced in the latest version of Java, i.e., Java SE 8(JDK-8). External Iteration was advanced and used in the older versions of Java, that are, Java SE 7, Java SE 6 and so on.
In internal iteration, the iteration is done internally on the different objects. For example - Streams. In external iteration, the iteration is done externally on the different objects.
Internal iteration works in the Functional programming style. External iteration works in the Object-Oriented programming style (OOPS).
Internal Iterator can be called as passive in nature. External Iterator can be called as active in nature.
There are less chances of errors in Internal iteration. But internal iteration requires more coding. There are more chances of errors in External iteration. But external iteration requires less coding.

27) What do you mean by the Type Inference in Java? Is Type Inference in Java being also used in previous versions like Java 7 or before Java 7 or Type interference in Java are only introduced?

Type inference in Java is a characteristic of the Java programming language that presents the strength for the compiler to see at every process invocation and corresponding expression to circumscribe the class of arguments.

Type Inference in Java suggests defining the type through compiler at compile time. It is not a brand-new innovation in Java SE 8. It is open in Java SE 7 and before Java SE 7 too.

Before Java 7:

Now let's examine the arrays of the Java programming language. Here in the below example, we are defining an array of type strings including the values as given below:

In the above example, we have specified some String values on the right-hand side, however, we have not described their types. Java Compiler automatically assumes its type and produces an array of strings.

Java 7:

Oracle Corporation has launched "Diamond Operator", a unique trait in Java SE 7 to dodge undesirable Type definitions in Generics.

In the above example, we have not described Type information on the right-hand side. However, we have just explained Java SE 7's Diamond Operator.

Java SE 8:

Oracle Corporation has intensified this Type Inference theory a lot in Java SE 8. We use this idea to establish Lambda Expressions, Functions, Method References etc.

In the above example, the Java Compiler observes the type definition available at the left-hand side and determines the type of Lambda Expression parameters a and b as Integers.


28) Explain with example, LocalDate, LocalTime, and LocalDateTime APIs.

LocalDate

LocalTime

LocalDateTime


29) Write a program to generate and print five random intergers between 5 and 100 in sorted order with the help of forEach method in Java SE 8?

The below code processes and prints five random integers using the forEach method in Java SE 8. A user can set the limit variable to any number depending on how many random numbers you want to generate. The only thing the user needs to write in this code here is the sorted() method.

Java8ForEach.java

Output:

18
26
61
62
87

30) Write a Java 8 program which finds the minimum and maximum number of a Stream?

In this program, we have used min() and max() methods to get the highest and lowest number of a Stream. First of all, we have initialized a Stream that has Integers and with the help of the Comparator.comparing() method, we have compared the elements of the Stream.

When this method is incorporated with max() and min(), it will give you the highest and lowest numbers. It will also work when comparing the Strings.

Java8Stream.java

Output:

The highest number is: 6
The lowest number is: 1

31) What do you mean by StringJoiner class in Java SE 8? How can we achieve joining multiple Strings using StringJoiner Class?

In Java SE 8, a new class was advanced and practiced in the java.util package which is called as StringJoiner class. With the help of this class, a user can merge various strings by separating them with some delimiters. StringJoiner class also helps in adding prefix and suffix to the strings.

In this program, we will add delimiter "," in between the various distinguished given strings. According to the question, we have to join many different strings. For this, we will take the help of add() method in order to add them. Finally, after that we will print the String Joiner.

Java8StringJoiner.java

Output:

Gaurav,Tanmay,Harsh,Abhishek,Akash

32) Write a Java 8 program modifying the string by adding prefix and suffix to it?

In this program, we will add delimiter "," in between the two distinguished given strings. Also, we have given "(" and ")" brackets as prefix and suffix. According to the question, we have to join many different strings. For this, we will take the help of add() method in order to add them. Finally, after that we will print the String Joiner.

Java8PrefixSuffix.java

Output:

(Anubhav,Gaurav,Tanmay,Nikhil,Harsh)

33) Write a Java 8 program to iterate a Stream using the forEach method?

In this program, we are iterating a Stream starting from "number = 2", followed by the count variable incremented by "1" after each iteration.

Then, we are filtering the number whose remainder is not zero when divided by the number 2. Also, we have set the limit as ? 5 which means only 5 times it will iterate. Finally, we are printing each element using forEach.

Java8Stream.java

Output:

2
4
6
8
10




You may also like:


Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA