JAVA: Summary / Functions 108
3
3
.
.
6
6
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
s
s
I
I
n
n
f
f
o
o
In Java all Functions need to be defined inside a Class in which case they are called Methods.
Content
Methods
Lambdas
Streams
M
M
e
e
t
t
h
h
o
o
d
d
s
s
In Java all Functions need to be defined inside a Class in which case they are called Methods.
Overloaded Methods are Methods declared in the same Class that have the same name but different signature.
They have different: Return Type or different number or Type of Input Parameters.
When during Inheritance Child Class declares Method with the same name and signature as an existing Parent's Method,
then it is said that Parent's Method has been Overridden (replaced) by the Child's Method.
Methods must have the same: Name, Return Type, number and Type of Input Parameters.
Method Syntax
public class Test {
//DECLARE METHOD.
public static String greet(String name, int age) {
return(name + " is " + age + " years old");
}
public static void main(String args[]) {
String result = greet("John", 20); //CALL METHOD.
System.out.print(result); //DISPLAY RESULT.
}
}
Overloaded Methods
public class Test {
//METHOD 1 Has the same name and return type but ONE Input Parameter
static void greet(String name) {
System.out.println("Hello " + name);
}
//METHOD 2 Has the same name and return type but TWO Input Parameters
static void greet(String name, int age) {
System.out.println(name + " is " + age + " years old");
}
public static void main(String args[]) {
greet("John" ); //Call Method 1.
greet("John", 20); //Call Method 2.
}
}
JAVA: Summary / Functions 109
Overridden Method
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test extends Person {
@Override //Use it to indicate intention of overriding existing Parent's Method
void greet(String name) {
System.out.println("Greetings " + name);
}
public static void main(String[] args) {
Test john = new Test();
john.greet("John"); //Greetings John
}
}
//===========================================================================================================
//CLASS: Person
//===========================================================================================================
class Person {
void greet(String name) {
System.out.println("Hello " + name);
}
}
JAVA: Summary / Functions 110
L
L
a
a
m
m
b
b
d
d
a
a
s
s
Lambda Expression is just a shorter Syntax for
creating Instance of Anonymous Inner Class
that Implements specified Functional Interface
that contains single Function/Method that we are interested in sending around
So it is that Instance that you can store into Variable or send as Function's Input Parameter.
And it is through that Instance that you call our Function/Method myInterface.greet("John", 20).
Assign Lambda Expression to Variable
class Test {
public static void main(String[] args) {
//CREATE INSTANCE OF ANONYMOUS INNER CLASS THAT IMPLEMENTS FUNCTIONAL INTERFACE.
MyInterface classInstance = (name, age) -> { return(name + " is " + age + " years old"); };
}
}
@FunctionalInterface
interface MyInterface {
public abstract String greet(String name, int age);
}
Send Lambda Expression as Function Parameter
class Test {
public static void main(String[] args) {
MyInterface classInstance = (name, age) -> { return(name + " is " + age + " years old"); };
displayMessage(classInstance);
}
public static void displayMessage(MyInterface myInterface) {
String result = myInterface.greet("John", 20);
System.out.println(result);
}
}
@FunctionalInterface
interface MyInterface {
public abstract String greet(String name, int age);
}
JAVA: Summary / Functions 111
S
S
t
t
r
r
e
e
a
a
m
m
s
s
Stream works as a section of a pipe that hooks up between Input and Output and modifies elements as they pass through.
You start with a normal Data Source like List and attach Stream 1 using something like myList.stream().
Elements of List are now Input to Stream 1. Stream 1 will not change Data Source elements.
Stream 1 will just forwards elements without processing them as they pass through.
Stream 1 is like an adapter that converts Data Source into a Stream to which other Streams can be connected.
Then you call a Method on Stream 1 which returns new Stream 2 that will process elements according to the Method.
Output of Stream 1 now becomes input to Stream 2 which might remove or change some elements.
You can continue calling Methods and adding Streams to perform additional processing of elements that pass through
On the last Stream 3 you call collect() Method in order to save elements into Data Collector like a List.
You need to do that because Streams don't save elements so that all the processing doesn't get lost.
Streams will start processing elements from Data Source only when you call collect() and not before.
Stream
E
E
x
x
a
a
m
m
p
p
l
l
e
e
This example shows how to convert elements of a List Source into upper case and store them into new List Collector
create Stream<String> from elements in a List<String> using Method myList.stream()
transform this Stream into a new Stream that converts elements into upper case using Method Stream.map()
collect elements from that new Stream into a new List<String> using Method Stream.collect()
Test.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
//CREATE DATA SOURCE.
List<String> myList = Arrays.asList("item 1", "item 2", "item 3", "item 4", "item 5");
//ATTACH STREAM TO DATA SOURCE.
Stream<String> stream = myList.stream();
//ATTACH ADITIONAL STREAM (that converts elements into upper case)
Stream<String> streamNew = stream.map( element -> { return element.toUpperCase(); } );
//COLLECT ELEMENTS INTO DATA COLLECTOR.
List<String> listOut = streamNew.collect(Collectors.toList());
//DISPLAY ELEMENTS FROM DATA COLLECTOR.
System.out.println(listOut); //[ITEM 1, ITEM 2, ITEM 3, ITEM 4, ITEM 5]
}
}
Data
Source
Data
Collector
Stream 1
Stream 2
Stream 3