If you can write object-oriented programs in Java, and you'd like to write even better Java, then this course is for you. Starting with real business problems, see how concepts from functional programming can help you in Java. From function values to streams, learn the significance of some of Java 8's additions -- and get the same benefits in Java 6! This course teaches ways of thinking that help functional programmers write code that doesn't break. This course is designed for Java programmers, although any object-oriented developer can benefit from the very clear explanations of concepts. It assumes no familiarity with functional programming. After this course, you'll have more design options and ideas, and you'll find writing Java more fun than before.
Jessica Kerr is a software developer with a hobby of presenting at conferences and teaching custom on-site git workshops.
Section Introduction Transcripts
Section Introduction Transcripts
Why Functional Programming? I'm Jessica Kerr. Welcome to Functional Programming with Java. I've been a Java developer for almost 12 years and a functional programmer for 2 years. I'm very excited about Java 8 because it makes functional programming in Java easier and it brings a functional programming style into idiomatic Java. To tell why this is exciting I'll go back three years before I knew anything about functional programming. My team had an interesting project. An application that took in dozens of input factors, things like prices and inflation, and combined them to predict important values like profit. The team spent a few months writing the user interface and writing it again, before we started on the meat of the project, the calculation engine. In the project plan management had allocated four weeks. I looked at that number and laughed. Months' maybe, not weeks. We dug in, applied our best object-oriented programming skills, and sure enough, 16 weeks of effort later the engine worked, but if I had known then what I know now I would not have laughed at 4 weeks. I'd have said, that sounds doable in Scala or F#. If the client requires Java 5 then more like 8 weeks. With this same technology knowing the concepts behind functional programing would have cut my work in half. This is why I'm excited about bringing functional programming into Java.
Improving Code With Functional Programming It's time to see two examples of how to improve code with functional programming. We'll look at these both in Java 8 and Java 6. I want to show you an example of how passing functions as values can remove code duplication. Just the other day at work we had a program that was running slowly and we wanted to know which parts were running slowly, specifically, we wanted to print out timing for different sections of the code. Let's pretend that was about this little program. For demonstration purposes, each of the methods inside this example sleep for a little while randomly and then return our hard-coded value, but let's pretend that they're actually working. We want to know how long it takes to calculate the costs, calculate the revenue, and finally, calculate the profit. To add timing information the easiest thing is to record the time before a calculation starts, then after it's complete check how much time has passed and print that information. I can do this for each of the calculations that might be slow. Store the time, do the work, find the difference, print it. There's a pattern here. You can tell because I'm copying and pasting and when I'm copying and pasting I am prone to error. In each of these hunks of code we do the same thing with storing the time, finding how much time has passed, and printing something. In between there's a piece of code that's different and the description of the activity is different. Also, notice that from each block of code there's one value we need to keep and two that are useless.
Streams: Coding in Context Welcome to module 3 of Functional Programming With Java. In this module we'll take a closer look at Streams. We'll look at how they can be lazy, how they can be made parallel. We'll look at the different kind of methods that you can call on a stream and the consequences that those methods have. Then I'll step back and talk about what it really means to map and filter and do these other calculations on data that's inside a stream. A stream is a context and I'll show you some other contexts that are also very useful to operate on data within. Finally, we'll see the power of functional programming and stream processing to let your program process data that doesn't fit in memory. First, I promised in the last module that I'd show you how the internal iteration on a stream is at least as efficient as a hand coded for loop.