Java 8 - Persistent data structure - The Koch Family
The Koch Family The Koch Family

Latest news

جاري التحميل ...

Java 8 - Persistent data structure


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

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.

Comments



If you like the content of our blog, we hope to stay in constant communication, just enter your email to subscribe to the blog's express mail to receive new blog updates, and you can send a message by clicking on the button next ...

إتصل بنا

About the site

author The Koch Family <<  Welcome! I'm so glad that you stopped by Your Modern Family blog. Together, we will talk about raising kids, organizing the home and saving money! and Tips & tricks and more…

< Learn more ←

Blog stats

Sparkline 2513183

All Copyrights Reserved

The Koch Family

2020