Статьи

Java 8: функциональный VS традиционный

Бизнес логика такая же:

Дано строковое выражение, состоящее из посещений / времени, например: «1 / 24ч, 1..3 / 3ч, 5 / *»

Тогда результатом должен быть следующий список строк:

«1/24»,
«1 / 3h»,»2 / 3h»,»3 / 3h»,
«5/1 ч», «5/2 ч», «5/3 ч», «5/4 ч», «5/5 ч», до «24/1 ч»

Таким образом, 2 вещи должны быть решены, точки и звезды для посещения и на время.

Я буду использовать Java 8, но я покажу вам разницу между реализацией этой логики с использованием Functional и реализацией ее традиционным способом с помощью циклов и ifs.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.marco;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
public class ExpressionConverter {
        private static final String COMMA = ",";
        private static final String SEPARATOR = "/";
 
        private final ImmutableList<Function<String, List<String>>> visitFunctions;
        private final ImmutableList<Function<String, List<String>>> timeFunctions;
 
        public ExpressionConverter(DotsVisitFunction dotsVisitFunction, StarVisitFunction starVisitFunction, StandardFunction standardFunction,
                        StarTimeFunction starTimeFunction) {
                this.visitFunctions = ImmutableList.of(dotsVisitFunction, starVisitFunction, standardFunction);
                this.timeFunctions = ImmutableList.of(starTimeFunction, standardFunction);
        }
 
        public List<String> convertVisitTimeExpressionFunctional(String visitTimeExpression) {
                return Lists.newArrayList(visitTimeExpression.split(COMMA)).parallelStream().filter(it -> !it.isEmpty())
                                .map(it -> interpretSingleExpressionFunctional(it)).flatMap(it -> it.parallelStream()).collect(Collectors.toList());
        }
 
        public List<String> convertVisitTimeExpressionTraditional(String visitTimeExpression) {
                List<String> result = new ArrayList<String>();
 
                for (String singleVisitTime : visitTimeExpression.split(COMMA)) {
                        if (!singleVisitTime.isEmpty()) {
                                result.addAll(interpretSingleVisitTimeExpressionTraditional(singleVisitTime));
                        }
                }
 
                return result;
 
        }
 
        private List<String> interpretSingleExpressionFunctional(String singleExpression) {
                String visit = singleExpression.split(SEPARATOR)[0];
                String time = singleExpression.split(SEPARATOR)[1];
                List<String> result = Lists.newArrayList();
 
                visitFunctions.stream().map(it -> it.apply(visit)).flatMap(it -> it.stream()).forEach(visitIt -> {
                        timeFunctions.stream().map(it -> it.apply(time)).flatMap(it -> it.stream()).forEach(timeIt -> {
                                result.add(visitIt + SEPARATOR + timeIt);
                        });
                });
                return result;
        }
 
        private List<String> interpretSingleVisitTimeExpressionTraditional(String singleExpression) {
 
                String visit = singleExpression.split(SEPARATOR)[0];
                String time = singleExpression.split(SEPARATOR)[1];
 
                List<String> result = Lists.newArrayList();
                List<String> visists = Lists.newArrayList();
                List<String> times = Lists.newArrayList();
 
                for (Function<String, List<String>> visitFunction : visitFunctions) {
                        visists.addAll(visitFunction.apply(visit));
                }
                for (Function<String, List<String>> timeFunction : timeFunctions) {
                        times.addAll(timeFunction.apply(time));
                }
                for (String visitIt : visists) {
                        for (String timeIt : times) {
                                result.add(visitIt + SEPARATOR + timeIt);
                        }
                }
                return result;
        }
}

Как видите, у нас есть 2 открытых и 2 частных метода, функциональный и традиционный.

Вот простой тест, ориентированный на производительность двух стилей:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.marco;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.math.BigDecimal;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
public class ExpressionConverterTest {
 
        private static final String EXPRESSIONS = "6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,6/7400,9/65h,55..57/14400";
 
        private Cache cache;
 
        private ExpressionConverter expressionConverter;
 
        @Before
        public void init() {
                cache = mock(Cache.class);
                expressionConverter = new ExpressionConverter(new DotsVisitFunction(cache), new StarVisitFunction(cache), new StandardFunction(),
                                new StarTimeFunction(cache));
 
                Set<Integer> totVisists = Sets.newHashSet();
                for (int i = 1; i <= 100; i++) {
                        totVisists.add(i);
                }
 
                when(cache.getVisits(anyLong(), (BigDecimal) any(), anyInt())).thenReturn(ImmutableSet.copyOf(totVisists));
        }
 
        @Test
        public void testPerf() {
 
                long averageTraditional = 0l;
                long averageFuntional = 0l;
 
                for (int a = 0; a < 10; a++) {
                        long start = System.currentTimeMillis();
 
                        for (int i = 0; i < 1000; i++) {
                                expressionConverter.convertVisitTimeExpressionTraditional(EXPRESSIONS);
 
                        }
                        System.out.println("Traditional java " + (System.currentTimeMillis() - start) + " ms");
                        averageTraditional += (System.currentTimeMillis() - start);
 
                        long start2 = System.currentTimeMillis();
                        for (int i = 0; i < 1000; i++) {
                                expressionConverter.convertVisitTimeExpressionFunctional(EXPRESSIONS);
                        }
                        System.out.println("Functional java " + (System.currentTimeMillis() - start2) + " ms");
                        averageFuntional += (System.currentTimeMillis() - start2);
                }
 
                System.out.println("Average Traditional java : " + (averageTraditional / 10) + " ms");
                System.out.println("Average Functional java : " + (averageFuntional / 10) + " ms");
 
        }
}

И это вывод:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Traditional java 1274 ms
 
Functional java 773 ms
 
Traditional java 1054 ms
 
Functional java 531 ms
 
Traditional java 961 ms
 
Functional java 493 ms
 
Traditional java 948 ms
 
Functional java 492 ms
 
Traditional java 949 ms
 
Functional java 491 ms
 
Traditional java 958 ms
 
Functional java 481 ms
 
Traditional java 1004 ms
 
Functional java 474 ms
 
Traditional java 949 ms
 
Functional java 471 ms
 
Traditional java 947 ms
 
Functional java 475 ms
 
Traditional java 942 ms
 
Functional java 472 ms
 
Average Traditional java : 998 ms
 
Average Functional java : 515 ms

Функционал в 2 раза быстрее традиционного. Почему?

Из-за этого: parallelStream ()

Parallel Stream разделит работу на несколько задач, используя вашу многоядерную систему.

Так что вы можете поместить ParallelsStream () везде и заменить все методы stream () на ParallelsStream () ??

Нет!

ПараллельнаяStream () на самом деле может вызвать значительное снижение производительности, если используется не в том месте.

Существует множество сообщений по этому аргументу, которые следует прочитать перед использованием этой функции, но просто запомните, если вы хотите использовать parallelStream (), обязательно измеряйте производительность до и после !!!!!!!!!!