M
M
Mercury132015-10-07 14:03:33
Java
Mercury13, 2015-10-07 14:03:33

Java: for on collection or functionality?

I have a piece of code (from a homemade game - because the planets).

for (Planet p : planets) {
    st.print(p.prototype.code);
}

Since recently, NetBeans has suggested replacing the code with this one.
planets.stream().forEach((p) -> {
    st.print(p.prototype.code);
});

What does he give? Performance? Advertising some functional libraries?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cthulhudx, 2015-10-07
@Mercury13

Declarative-style programs tend to take up less space and are easier to read, that's all. You can also iterate through a collection in parallel by replacing stream() with parallelStream(), filter the elements you want with a predicate, and generally do a lot of useful things.

X
X-, 2015-10-07
NoNAME @XNoNAME

He offers a solution on java 8
If the application runs on jre 1.8, then you can replace. Otherwise, configure the project to use JDK 1.7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question