Primitive data types & Wrapper classes
Type Description Default Size Example Wrapper Class
byte whole number (-128 to 127) 0 8 bits byte b = 12; Byte
short whole number (-32768 to 32767) 0 16 bits short s = 123; Short
int whole number (-2^31 to 2^31-1) 0 32 bits int i = 100; Integer
long whole number (-2^63 to 2^63-1) 0 64 bits long l = 200; Long
float decimal number (1.4e-45 to 3.4e+38) 0.0 32 bits float f = 1.3f; Float
double decimal number (4.94e-324 to 1.797e+308) 0.0 64 bits double d = 0.5; Double
char '\u0000' to '\uffff' '\u0000' 16 bits char c = 'c'; Character
boolean true / false false 1 bit boolean b = true; Boolean
Abstract Class Interface
IS-A relationship HAS-A relationship
Can not create instance of an Abstract class Can not create an instance of an Interface
Classes should implement the interface
Other classes should extend this Abstract class
By default all are abstract (From Java 8, Interfaces can
0 or more abstract methods
have default and static non-abstract methods)
Can have static final variable.
It can have private, protected, public members,
private members are not allowed. (From Java 9, interfaces can
methods
also have private methods)
Method References
Description Lambda Expression Lambda using Method Reference
Static method call - pass as parameter -
Method accepts data and prints (data) -> System.out.println(data) System.out::println
using System.out.println
Static method call - pass as parameter -
Method accepts data and we pass it to another (o) -> Objects.isNull(o) Objects::isNull
method to check if it is null
Given object -> Instance method call-
for example, call the toUpperCase for the (data) -> data.toUpperCase() String::toUpperCase
given string
Given object -> Instance method call
with parameter- for example, call the concat (s1, s2) -> s1.concat(s2) String::concat
for the given string
Given object -> Instance method call
with parameters- for example, call the
(s1, s2, s3) -> s1.replaceAll(s2, s3) String::replaceAll
replaceAll for the given string with given
parameters
Given object -> pass as parameter -
pass the given object to another object method (data) -> list.add(data) list::add
as parameter
Create new object -> new Cat() () -> new Cat() Cat::new
Java8 Functional Interfaces
Functional Composition /
Name Input Type Return Type Method Bi Type
Lambda Chaining
Supplier<T> N/A T get N/A N/A
Consumer<T> T void accept andThen(Consumer<T>) BiConsumer<T, R>
and(Predicate);
Predicate<T> T boolean test or(Predicate) BiPredicate<T, R>
negate()
andThen(Function)
Function<T,R> T R apply BiFunction<T,U, R>
compose(Function)
Runnable N/A N/A run N/A N/A
Callable<T> N/A T call N/A N/A
Stream Operations
Type Behavior Methods
filter
map
limit
Returns new streams skip
Intermediate
Lazy distinct
sorted
flatMap
peek
forEach
collect
count
Stream is consumed min
Terminal
Can not be reused max
findAny
anyMatch
noneMatch
Intermediate Operations
Operation Behavior Input Data type Example
filter(i -> i % 2 == 0)
filter Used for filtering data Predicate<T>
filter(o -> Objects.nonNul(o))
Intermediate Operations
map(i -> i * i)
Transforms the received data
map Function<T, R> map(s -> s.toUpperCase())
from one form to another
map(b -> DriverFactory.get(b))
To limit the number of items which can
limit long limit(3)
flow through the pipeline
skip Skips the first few items long skip(3)
peek Just for debugging Consumer<T> peek(i -> System.out.println(i))
distinct Allows only distinct values in the pipeline N/A distinct()
sorted(Comparator.naturalOrder())
sorted Sorts the data (asc / desc) Comparator
sorted(Comparator.reverseOrder())
flatMap Flattens the data Function<T, R>
Terminal Operations
Operation Behavior Input Data type Example
forEach Used for consuming the given object Consumer<T> forEach(e -> e.click())
collect To collect the object into a list, set, map etc Multiple implementations collect(Collectors.toList())
count To count the object N/A count()
min(Comparator.naturalOrder())
min first element after comparing all Comparator
min(Comparator.reverseOrder())
max(Comparator.naturalOrder())
max last element after comparing all Comparator
max(Comparator.reverseOrder())
findAny Just give one from the stream N/A findAny()
findFirst() Give the first one from the stream N/A findFirst()
is there any element in the stream
anyMatch Predicate<T> anyMatch(i -> i > 5)
which satisfies the condition?
stream elements should not satisfy
noneMatch Predicate<T> noneMatch(i -> i > 5)
the given condition
Terminal Operations - Collectors
Collect Usage
To a list .collect(Collectors.toList())
To a set
.collect(Collectors.toSet())
(no duplicates)
.collect(Collectors.joining())
Join all
.collect(Collectors.joining(","))
To a map .collect(Collectors.groupingBy(...))
Stream Source
Source Usage
List list list.stream()
Set set set.stream()
map.entrySet().stream()
Map map map.keySet().stream()
map.values().stream()
int[] arr Arrays.stream(arr)
String a = "udemy"
String b = "hi" Stream.of(a, b, c.....)
String c = "hello"
Comparator
Comparator Usage
min(Comparator.naturalOrder())
Comparator.naturalOrder() max(Comparator.naturalOrder())
sorted(Comparator.naturalOrder())
min(Comparator.reverseOrder())
Comparator.reverseOrder() max(Comparator.reverseOrder())
sorted(Comparator.reverseOrder())
//Student name
Comparator.comparing(Function)
min(Comparator.comparing(s -> s.getName()))
Primitive Streams
Stream<T> Convert Primitive Streams Terminal Operations
mapToInt(Function) ==>
sum()
Stream<Integer> IntStream
average()
<== boxed()
mapToLong(Function) ==>
sum()
Stream<Long> LongStream
average()
<== boxed()
mapToDouble(Function) ==>
sum()
Stream<Double> DoubleStream
average()
<== boxed()