Java 8 Key Features Explained
Java 8 Key Features Explained
com/java-8-features
Java 8 Features
Java 8 introduced several new features and
enhancements that revolutionized the way developers
write Java code. Here's a breakdown of the key
features:
1. Lambda Expressions
• Description: Enables functional programming by
allowing you to write anonymous functions (short
and concise).
• Syntax:
1 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
package [Link].java8;
import [Link];
import [Link];
import [Link];
import [Link].*;
2 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link]([Link](5)); // Output:
true
[Link]([Link](-3)); // Output:
false
3 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link]([Link]()); // Output:
Appearance Add new Edit this post
Random number (e.g., 42)
[Link]([Link]()); // Output:
Another random number (e.g., 85)
[Link]([Link]("hello")); //
Output: HELLO
[Link]([Link]("java")); //
Output: JAVA
[Link]([Link]("Hello, ",
"World!")); // Output: Hello, World!
[Link]([Link]("Java ", "8"));
// Output: Java 8
}
}
4 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
... ).
Appearance Add new Edit this post
5. Consumer for Integer ( Consumer<Integer> ):
◦ Checks if a number is even
( Consumer<Integer> integerConsumer = ... ).
6. Sorting Names ( [Link] ):
◦ Sorts a list by string length using a
lambda ( [Link]((a, b) -> ...) ).
7. forEach Method ( [Link](printName) ):
◦ Iterates over a list of names and prints
each ( Consumer<String> printName = ... ).
8. Supplier for Random Numbers
( Supplier<Integer> ):
◦ Generates random numbers ( Supplier<Integer>
randomNumber = ... ).
9. Function for String Transformation
( Function<String, String> ):
◦ Converts strings to uppercase
( Function<String, String> toUpperCase =
... ).
10. BiFunction for Concatenation
( BiFunction<String, String, String> ):
◦ Combines two strings into one
( BiFunction<String, String, String>
concatenate = ... ).
2. Functional Interfaces
• Description: An interface with a single abstract
method (SAM).
• Key Annotation: @FunctionalInterface (Optional
but recommended).
• Common Interfaces:
◦ Predicate<T> : Takes one argument, returns a
boolean.
◦ Consumer<T> : Takes one argument, returns
nothing.
◦ Function<T, R> : Takes one argument, returns a
result.
5 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
@FunctionalInterface
Appearance Add new Edit this post
interface Greeting {
void sayHello(String name);
}
1. Consumer
• Purpose: Represents an operation that takes a
single input and performs an action without
returning a result.
• Methods:
◦ void accept(T t)
2. Supplier
• Purpose: Represents a function that supplies a
value without taking any input.
• Methods:
◦ T get()
3. Predicate
• Purpose: Represents a condition (boolean-valued
function) on a single input.
• Methods:
◦ boolean test(T t)
4. Function
• Purpose: Represents a function that takes one
input and produces one output.
• Methods:
6 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
◦ R Add
Appearance
apply(T
new
t)
Edit this post
5. BiFunction
• Purpose: Represents a function that takes two
inputs and produces one output.
• Methods:
◦ R apply(T t, U u)
6. UnaryOperator
• Purpose: A specialization of Function for
operations on a single operand that return the
same type.
• Methods:
◦ T apply(T t)
7. BinaryOperator
• Purpose: A specialization of BiFunction for
operations on two operands of the same type that
return the same type.
• Methods:
◦ T apply(T t1, T t2)
8. BiConsumer
• Purpose: Represents an operation that takes two
inputs and performs an action without returning a
result.
• Methods:
◦ void accept(T t, U u)
9. DoublePredicate,
IntPredicate, LongPredicate
• Purpose: Specialized versions of Predicate for
double , int , and long types.
• Methods:
◦ boolean test(double d) (or int / long )
7 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
10. DAdd
ounew
Appearance bleEdit
Cothisnpost
sumer,
IntConsumer, LongConsumer
• Purpose: Specialized versions of Consumer for
double , int , and long types.
• Methods:
◦ void accept(double d) (or int / long )
11. ToDoubleFunction,
ToIntFunction, ToLongFunction
• Purpose: Converts an input to a double , int , or
long respectively.
• Methods:
◦ double applyAsDouble(T t) (or int / long )
12. ToDoubleBiFunction,
ToIntBiFunction,
ToLongBiFunction
• Purpose: Converts two inputs to a double , int , or
long respectively.
• Methods:
◦ double applyAsDouble(T t, U u) (or int / long )
13. DoubleSupplier,
IntSupplier, LongSupplier
• Purpose: Specialized versions of Supplier for
double , int , and long types.
• Methods:
◦ double getAsDouble() (or int / long )
14. ObjDoubleConsumer,
ObjIntConsumer,
ObjLongConsumer
8 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
• PurposAdd
Appearance
e: new
Takes Edit
an this
object
post
and a primitive type
( double , int , or long ) and performs an
operation.
• Methods:
◦ void accept(T t, double d) (or int / long )
1. Runnable (Introduced in
Java 1.0)
• Purpose: Represents a task that can be run
concurrently in a thread.
• Method:
◦ void run()
2. Callable (Introduced in
Java 5)
• Purpose: Represents a task that can return a
result and may throw an exception.
• Method:
◦ V call() throws Exception
3. Comparator (Introduced in
9 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Java Add
1.2,
Appearance new present
Edit this post in
[Link] package)
• Purpose: Compares two objects for ordering.
• Method:
◦ int compare(T o1, T o2)
4. ActionListener (Introduced
in Java 1.0)
• Purpose: Represents an action event listener for
GUI components.
• Method:
◦ void actionPerformed(ActionEvent e)
5. EventListener (Introduced
in Java 1.0)
• Purpose: Represents the general interface for all
event listeners.
• Method: No method (marker interface for event
handling)
6. RunnableFuture (Introduced
in Java 5)
• Purpose: Extends Runnable and Future , combining
the ability to run a task and return a result.
• Method:
◦ boolean cancel(boolean mayInterruptIfRunning)
◦ V get()
10 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
8. StAdd
renew
Appearance am’Edit
s thisforEach
post (Prior
to Java 8)
• Even before Java 8's [Link]() , you could
use loops or iterators for similar purposes,
though Stream itself was introduced in Java 8.
1. Optional (Java 8)
• While Optional itself isn't a functional
interface, it often works with functional
programming principles. It is used to represent a
value that may or may not be present.
11 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
import [Link];
Supplier<T>
12 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
import [Link];
Appearance Add new Edit this post
public class SupplierExample {
public static void main(String[] args) {
Supplier<Integer> randomNumber = () -> (int)
([Link]() * 100);
[Link]("Random number: " +
[Link]());
}
}
•
◦ Output: Random number like 42
Predicate<T>
import [Link];
Output:
true false
13 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Function<T, R>
import [Link];
◦
▪ Output: Length of 'Hello': 5
BiConsumer<T, U>
import [Link];
14 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
◦
▪ Output: Alice is 25 years old.
BiFunction<T, U, R>
import [Link];
▪
▪ Output: Sum: 8
UnaryOperator<T>
15 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Appearance
Denew
▪Add finitiEdit
on:this
A post
special case of Function that
takes one argument of type T and returns a
result of the same type T .
▪ Use Case: Used for operations that return
the same type of input.
▪ Method:
▪ T apply(T t)
▪ Code Example:
import [Link];
[Link]([Link]("hello"));
}
}
▪ Output: HELLO
BinaryOperator<T>
import [Link];
16 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link]("Multiplication: " +
Appearance Add new Edit this post
[Link](4, 5));
}
}
▪ Output: Multiplication: 20
1. ToIntFunction<T>
▪ Definition: Represents a function
that takes one argument and
returns an int value.
▪ Use Case: Used for extracting an
int value from an object.
▪ Method:
▪ int applyAsInt(T t)
▪ Code Example:
import
[Link]
tion; public class
ToIntFunctionExample {
public static void main
(String[] args) {
ToIntFunction<String>
stringToLength = str ->
[Link]();
[Link]("Length:
" +
[Link]("J
ava" )); } }
Output: Length: 4
2. ToDoubleFunction<T>
▪ Definition: Represents a function
that takes one argument and
returns a double value.
▪ Use Case: Used for extracting a
17 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
import
[Link]
unction; public class
ToDoubleFunctionExample {
public static void main
(String[] args) {
ToDoubleFunction<String>
stringToDouble = str ->
[Link]() * 1.5 ;
[Link]("Double
value: " +
[Link]
("Java" )); } }
18 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
package [Link].java8;
import [Link].*;
19 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
20 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Output Example:
Consumer Output: Hello, Consumer!
Supplier Output: 42
Predicate Output: Is 4 even? true
Predicate Output: Is 5 even? false
Function Output: Length of 'Java' is 4
BiConsumer Output: Alice is 30 years old.
BiFunction Output: Sum of 5 and 3 is 8
UnaryOperator Output: 'hello' to uppercase is
HELLO
BinaryOperator Output: Product of 4 and 5 is
20
ToIntFunction Output: Length of 'Hello' is 5
ToDoubleFunction Output: Length of 'Hello' as
double is 7.5
Explanation of Code:
1. Consumer: Prints a string to the
console.
2. Supplier: Generates and returns a
random number between 0 and 100.
3. Predicate: Checks if a number is even
(returns true or false ).
4. Function: Takes a string and returns
its length.
21 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
package [Link].java8;
import [Link];
import [Link];
import [Link];
import [Link].*;
22 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link]([Link](-3)); //
Output: false (-3 is negative)
23 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link]([Link]("hello"));
// Output: HELLO
[Link]([Link]("java"));
// Output: JAVA
[Link]([Link]("Hello,
", "World!")); // Output: Hello, World!
[Link]([Link]("Java ",
"8")); // Output: Java 8
}
}
→ Next
24 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
▪ Using [Link]() :
import [Link];
25 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
▪ Using [Link]() :
import [Link];
26 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Example:
import [Link];
27 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Method to
Source Example
Create Stream
List<String> list =
Collection stream() method
...; [Link]()
Stream<String> stream
Individual [Link]()
= [Link]("a", "b",
Values method
"c")
[Link]()
int[] numbers = {1,
Primitive for primitive
2, 3};
Arrays types ( int[] ,
[Link](numbers)
double[] )
28 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Appearance Real-World
Add new Edit this post Use Case
(Gaming):
In a gaming project, suppose you have a list
of players, and you want to filter out all
players whose scores are below a certain
threshold and then sort the remaining players
by their scores.
import [Link];
import [Link];
import [Link];
class Player {
String name;
int score;
29 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
.collect([Link]());
Appearance Add new Edit this post
[Link]([Link]::println);
// Output: David: 2000, Alice: 1500, Charlie:
1200
}
}
import [Link];
import [Link];
30 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link]()
.filter(name -> [Link]() > 3)
.forEach([Link]::println); //
Output: Alice, Charlie, David
}
}
Features:
Primitive Streams
▪ IntStream, LongStream, and DoubleStream:
These are specialized streams for handling
primitive types ( int , long , and double ),
providing better performance by avoiding
autoboxing (the conversion between
primitives and objects).
31 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
import [Link];
Features:
32 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
Stream No Yes
IntStream Yes No
LongStream Yes No
DoubleStream Yes No
Types Of Stream
API In Java 8
33 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
package [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
34 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
package [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
35 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
List
Appearance Add new Edit this post
List<String> list =
[Link]("apple", "banana", "cherry");
.collect([Link]());
[Link](result); // Output:
[apple, banana, cherry]
.collect([Link]([Link](),
l -> list))
.forEach((key, value) ->
[Link]("Identity: " + key + " => "
+ value));
36 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
package [Link];
import [Link];
import [Link];
import [Link];
37 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link](val ->
Appearance Add new Edit this post
[Link]("First element: " + val));
// Output: 1
38 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
>= 1000)
Appearance Add new Edit this post
// 3. Apply a transformation to
convert all player names to uppercase
.map(String::toUpperCase)
.filter(player ->
[Link]().equals(name))
.flatMap(player ->
[Link]().stream()))
39 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
quests
Appearance Add new Edit this post
class Player {
private String name;
private int score;
private String rank;
private List<String> quests;
40 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
uniform formatting.
Appearance Add new Edit this post
4. filter(name ->
[Link]("A")) : Filters the
list of player names to only include
those starting with the letter "A".
5. flatMap(name ->
[Link]().filter(player ->
[Link]().equals(name)).flatM
ap(player ->
[Link]().stream())) : For
each player, we extract their quests,
and use flatMap to flatten the
nested lists into a single stream of
quest names.
6. distinct() : Removes any duplicate
quest names from the resulting
stream.
7. sorted() : Sorts the quest names in
alphabetical order.
8. limit(3) : Limits the result to the
first 3 quests, demonstrating a cap
on the stream's size.
9. collect([Link]()) :
Collects the result into a list,
which is the final output.
Output:
High-ranking players' quests: [Quest1, Quest2,
Quest3]
41 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
.filter(player ->
[Link]().equals(name))
.flatMap(player ->
[Link]().stream()))
// 6. Remove duplicates from the
quest names
.distinct()
// 7. Sort quest names
alphabetically
.sorted()
// 8. Limit to the first 3 quest
names
.limit(3)
42 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
43 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
[Link] = rank;
Appearance Add new Edit this post
[Link] = quests;
}
44 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
45 of 46 02/11/25, 10:21 am
Java 8 Features [Link]
43 min read
By Nitesh Synergy
SHARE
46 of 46 02/11/25, 10:21 am