Holding on to Data

I’m starting to learn that a lot of the concepts in programming are tied to data and how to access and manipulate it. I guess this makes sense as the underlying logic of all computers is linked to bits and how computers interpret data using binary logic.

So as I’m still learning how to write object oriented code, I have started thinking about how functional programming works in Java 8 using streams. It is difficult to switch to this way of thinking and data is pretty slippery when its being passed through streams and IntStreams. I am probably trying to force the streams that I am writing into List Collections when I do not actually need to do this. One of the more useful things about using streams is that the state of a list does not have to be changed and stored at every stage so I think if I stop trying to force it and just pass it to the next method it might be easier to handle.

I’ve definitely noticed when using IntStreams in place of for loops (especially when using IntStreams within other IntStreams) that getting back return values is not so easy and it feels forced. I guess this is because the main idea behind streams and IntStreams is to pass around data and less about mutating and holding onto it.

When looking at code examples online of streams its also noticeable that they look much cleaner and are easier to read (if you have an idea of how they work and what they are trying to achieve). This is because there is no state they are trying to keep track of, just a series of sequential steps. You have to change one thing, then act on that changed data straight away and usually only at the final point when you need to return that data do you change it in some way.

Here is the for loop I previously had for my winningLines combination for any size board:

public List<Line> createRow(List<Symbol> contentOfBoard, int size) {
List<Line> lines = new ArrayList<>();
for (int i = 0; i < size * size; i += size) {
List<Symbol> rows = new ArrayList<>();
for (int j = i; j < i + size; j++) {
lines.add(new Line(rows));
return lines;

and here is what I am turning it into:

 public Stream<Stream<Integer>> streamRows(List<Symbol> contentOfBoard, int size) {
return IntStream.range(0, contentOfBoard.size())
.map(index -> index * size)
.mapToObj(rowStart -> IntStream.iterate(rowStart, index -> index++).boxed())

(the .boxed() operator here converts the IntStream that has been mapped to an Object back into an Integer)

It might not be much shorter but there seems to be less going on.

Because it is not collected and stored in a List variable like my code in the for loops, the data, in the form of a Stream of IntStreams needs to be passed down to another method, maybe one where any winning lines are calculated.

It is becoming more clear why they are called Streams, I’m thinking its because they sort of represent a stream of data flowing from one place to another, although this is just a guess and I might be reading too much into it..

I still have a lot of methods to change in my TicTacToe for the IntStreams to be used to calculate all the different row combinations but I think I am finally starting to see how streams can be useful!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.