Computational Intelligence - May 2016 - 45

Program 3. Use of streams in Java 8

Program 4. Use of Parallel Streams in Java 8

List list= Arrays.
asList(0,3,5,6,8,10,12,15);
System.out.println("The count of even
numbers is" + list.stream().filter
(i -> i % 2 == 0).count());
System.out.println("The count of odd
numbers is" + list.stream().filter
(i -> i % 2 == 1).count());

List list = Arrays.
asList(0,3,5,6,8,10,12,15);
System.out.println("The count of even
numbers is" + list.parallelStream().
filter(i -> i % 2 == 0).count());
System.out.println("The count of odd
numbers is" + list.parallelStream().
filter(i -> i % 2 == 1).count());

process of the sequence of elements by applying some final
operation (for example forEach, findFirst, collect,
and count). In terms of map-reduce operations, intermediate
operations can be regarded as map operations whereas terminal
operations can be matched to reduce operations.
In addition, all the Java collections (lists, sets, maps, etc.) contain the method stream() that returns a java.util.
Stream object covering all the elements of the collection. Using
Java 8 Streams, the example above can be rewritten in a much
more concise and readable manner, as shown in Program 3.
Program 3 uses the intermediate operation filter that
accepts a predicate (a function that takes a given object
and returns a true/false value) and produces a new stream
object which only covers those elements that satisfy the given
predicate (it discards those elements which do not satisfy the
predicate). In this particular example, the predicate is
defined by the lambda expressions. Next, the terminal operation count() is called on the result to return the number
of elements in the filtered stream. As can be seen, using
streams and lambda expressions, we can replace with two
lines of code the following elements: one class, one interface,
three static methods, and two anonymous classes.
4) parallel Streams
Let us imagine that our running example involves processing
a very large sequence of numbers. It is straightforward to see
that this problem can benefit from parallel processing on
multi-core processors: the problem can be independently
divided into sub-problems (that is, counting even/odd numbers for non-overlapping sub-chains) which can be solved
simultaneously (in parallel, such that each sub-chain is iterated
in a separate thread/core), and finally combining the different
sub-solutions to obtain a global solution (summing up all the
partial counts).
Java 8 enables coding of this parallel task in a trivial and
effortless manner because all the stream operators can either be
applied sequentially (as described above, on a single thread) or in
parallel (where multiple threads are used). Parallel streams are
invoked using the parallelStream() method, present in all
the Java collections. Program 4 shows a running example of
using parallel streams in Java 8. There is not a need to mention
that coding this parallel operation using older Java versions might
require a far more complex, hard to read and debug, Java code.

B. Bayesian Networks

Bayesian networks (BNs) [31]-[33] are widely used PGMs for
reasoning under uncertainty. A BN defined over a set of random variables X = {X 1, f, X n} can be graphically represented by a directed acyclic graph (DAG). Each node, labelled Xi in
the example graph depicted in Fig. 1, is associated with a factor
or conditional probability table p (X i|Pa (X i)), where Pa (X i) are
the parents of Xi in the graph. A BN representation defines a
joint probability distribution p (X) over the variables involved
which factorizes, according to the chain rule, as follows
p ( X) =

n

% p (X i|Pa (X i)).

i= 1

Fig. 1 shows an example of a BN model over five variables.
A conditional probability distribution is associated with each
node in the network describing its conditional probability distribution given its parents in the network, and the joint distribution factorizes as p (X 1, f, X 5) = p (X 1) p (X 2|X 1) p (X 3|X 1)
p (X 4|X 2, X 3) p (X 5|X 3) .
A BN is called hybrid if some of its variables are discrete
while others are continuous. In the AMIDST modelling framework, we specifically consider conditional linear Gaussian (CLG)
BNs [32], [36], [37]. In a CLG model, the local distributions of
the continuous variables are specified as CLG distributions and
the discrete variables are required to only have discrete parents.
III. Designing Data Structures for PGMs using Java 8

One of the main conclusions drawn when developing the
AMIDST toolbox is that the design of the basic data structures
for PGMs should be carefully set up to enable the use of a
functional programming style when developing the subsequent

X1

X3

X2

X4

X5

FIgURE 1 Example of a BN model with five variables.

may 2016 | ieee Computational intelligenCe magazine

45



Table of Contents for the Digital Edition of Computational Intelligence - May 2016

Computational Intelligence - May 2016 - Cover1
Computational Intelligence - May 2016 - Cover2
Computational Intelligence - May 2016 - 1
Computational Intelligence - May 2016 - 2
Computational Intelligence - May 2016 - 3
Computational Intelligence - May 2016 - 4
Computational Intelligence - May 2016 - 5
Computational Intelligence - May 2016 - 6
Computational Intelligence - May 2016 - 7
Computational Intelligence - May 2016 - 8
Computational Intelligence - May 2016 - 9
Computational Intelligence - May 2016 - 10
Computational Intelligence - May 2016 - 11
Computational Intelligence - May 2016 - 12
Computational Intelligence - May 2016 - 13
Computational Intelligence - May 2016 - 14
Computational Intelligence - May 2016 - 15
Computational Intelligence - May 2016 - 16
Computational Intelligence - May 2016 - 17
Computational Intelligence - May 2016 - 18
Computational Intelligence - May 2016 - 19
Computational Intelligence - May 2016 - 20
Computational Intelligence - May 2016 - 21
Computational Intelligence - May 2016 - 22
Computational Intelligence - May 2016 - 23
Computational Intelligence - May 2016 - 24
Computational Intelligence - May 2016 - 25
Computational Intelligence - May 2016 - 26
Computational Intelligence - May 2016 - 27
Computational Intelligence - May 2016 - 28
Computational Intelligence - May 2016 - 29
Computational Intelligence - May 2016 - 30
Computational Intelligence - May 2016 - 31
Computational Intelligence - May 2016 - 32
Computational Intelligence - May 2016 - 33
Computational Intelligence - May 2016 - 34
Computational Intelligence - May 2016 - 35
Computational Intelligence - May 2016 - 36
Computational Intelligence - May 2016 - 37
Computational Intelligence - May 2016 - 38
Computational Intelligence - May 2016 - 39
Computational Intelligence - May 2016 - 40
Computational Intelligence - May 2016 - 41
Computational Intelligence - May 2016 - 42
Computational Intelligence - May 2016 - 43
Computational Intelligence - May 2016 - 44
Computational Intelligence - May 2016 - 45
Computational Intelligence - May 2016 - 46
Computational Intelligence - May 2016 - 47
Computational Intelligence - May 2016 - 48
Computational Intelligence - May 2016 - 49
Computational Intelligence - May 2016 - 50
Computational Intelligence - May 2016 - 51
Computational Intelligence - May 2016 - 52
Computational Intelligence - May 2016 - 53
Computational Intelligence - May 2016 - 54
Computational Intelligence - May 2016 - 55
Computational Intelligence - May 2016 - 56
Computational Intelligence - May 2016 - 57
Computational Intelligence - May 2016 - 58
Computational Intelligence - May 2016 - 59
Computational Intelligence - May 2016 - 60
Computational Intelligence - May 2016 - 61
Computational Intelligence - May 2016 - 62
Computational Intelligence - May 2016 - 63
Computational Intelligence - May 2016 - 64
Computational Intelligence - May 2016 - 65
Computational Intelligence - May 2016 - 66
Computational Intelligence - May 2016 - 67
Computational Intelligence - May 2016 - 68
Computational Intelligence - May 2016 - 69
Computational Intelligence - May 2016 - 70
Computational Intelligence - May 2016 - 71
Computational Intelligence - May 2016 - 72
Computational Intelligence - May 2016 - Cover3
Computational Intelligence - May 2016 - Cover4
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202311
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202308
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202305
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202302
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202211
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202208
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202205
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202202
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202111
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202108
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202105
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202102
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202011
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202008
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202005
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_202002
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201911
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201908
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201905
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201902
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201811
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201808
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201805
https://www.nxtbook.com/nxtbooks/ieee/computationalintelligence_201802
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring17
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring16
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring15
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring14
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_summer13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_spring13
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_winter12
https://www.nxtbook.com/nxtbooks/ieee/computational_intelligence_fall12
https://www.nxtbookmedia.com