The following is a series of posts about "functional programming in Java" which is the result of my understanding by reading the book "Java 8 in Action: Lambdas, Streams, and Functional-style Programming, by Alan Mycroft and Mario Fusco".
1. Why functional programming?
2. Functional programming in Java 8
3. Java 8 - Using Functions as Values
4. Java 8 - Persistent data structure
1. Why functional programming?
2. Functional programming in Java 8
3. Java 8 - Using Functions as Values
4. Java 8 - Persistent data structure
Persistent data structure is also known as a simple technique but it's very important. Its other names are functional data structure and immutable data structure.
Why is it "persistent"?
Their values persist and are isolated from changes happening elsewhere. That's it!This technique is described as below:
If you need a data structure to represent the result of a computation, you should make a new one and not mutable an existing data structure.
Destructive updates version
public static A doSomething(A a){
a.setProp1("new value");
return a;
}
Functional version
public static A doSomeThing(A a){
A result = new A();
result.setProp1(a.getProp1());
return result;
}
Foo: If so, for what?
Bar: Imagine a case that you call the former method twice, the A is changed and affected by the latest call. Magic issue! Then, it's hard for maintenance, guy. Got it?
Reference
[1]. Alan Mycroft and Mario Fusco, Java 8 in Action: Lambdas, Streams, and Functional-style Programming. Part 4. Beyond Java 8.