 Well, not a very, very encouraging response, but hopefully by the time we are done, I wouldn't have put you to sleep after that lunch. So anyway, my name is Premaran Chandrasekhar and I work for ThoughtWorks. And today we'll be talking about functional programming in Java. And one of my colleagues, I told him that I am going to this functional programming conference and so he asked me, what are you talking about? I mean, so I said I am going to talk about functional programming in Java and his facial expression dropped and I am like, what? So he tells me, he didn't actually tell me that this is what I inferred. You are going to a functional programming conference and you are going to talk about Java, really? So, yeah, I mean, it's still, you know, and to follow up from the keynote today in the morning, where the mainstream got completely slammed. So this is probably the ante for that, but we'll see. So the way that this was supposed to work is that this was supposed to be a workshop, which means that if you want, you are encouraged to actually code along, but if you don't, that's fine too. What we'll do is, you know, we'll walk through an inventory management example and I'll give you more details around what that actually means. So I've split it up into several tasks and these are very small tasks that you can do in two minutes, maybe even less and take it from there. So if you are in the mood to actually code, there is a Git repo available there and if you want to code along, you probably want to start with the legacy initial and the functional initial, which means, you know, it has only the portions that are not relevant to the talk itself, you know, basic couple of domain objects, which I'll talk about. And if you are not in the mood to program, then there is the completed solution, you know, or at least in my view, what I think is a completed solution. So you can follow along with that because the slides in this presentation are mostly along that. You know, so the complete ones are checked in to, this is in GitHub. So they are checked in in a way where these slides move. So for every, almost every problem you have a slide, you have a check-in and then it actually follows this presentation. But if you are like me, then you can just follow along the slides here and not worry about any of that. Okay? All right, I'll leave it up to you. So let's introduce the problem itself, right? So what we are talking about here is, like I said, an inventory management problem. So we are looking at this domain object called product. Product has a category, it has a name and it has a price, right, so fairly straightforward. And then we'll talk about this thing called an item. What is an item? An item is a product, but it also has a quantity associated with it. And you store items in a warehouse, okay? So a warehouse basically has a set of items, right? And what we'll try to do is, we'll try to build features around these domain objects, okay? So far so good, all right? So the first task is for us to print names of all items in inventory. So you've got the warehouse, the warehouse has a set of items, like I said. You basically, the task is to build, to output all the names of the items that are in inventory. And in inventory meaning, if you go back to item, an item is in stock if the quantity is greater than zero, right? So what you have to do is, you have to try and print that out. Do you want me to wait a few seconds before I actually show you what I would have done if this was me? How do you want to do this? Or should I just go ahead? Go ahead, okay, all right, I'll do that. So if I were to do this myself, right, I mean, and this is typically how I would do it, right? So you've got a typical for loop, you're iterating over the items, and this method you can think of as a method in the warehouse class, right? So and then if the item is in stock, I just print it out, okay, so far so good. So no, I mean, okay, fine, I mean, this is very imperative and this is very, you know, very verbose and all that, but if this is all I have to do, this works, right? Everybody here most likely understands what is happening here. It's not too hard to comprehend, correct? But if I were to tell you, you know, okay, here is another problem, print names of all in-stock items of a particular category, right? How would you do that? So if I were to do that, then I would do something like this, you know, so take in an input category and then it rate over the items, if the item is in stock and if the item is of a certain category, then print it out, okay, no issues there. Now if you look, if you combine the two, right, so you have got these two methods, one which takes in nothing as an input and the other which takes in a category as input and if you look at them, they look pretty much identical, isn't it, you know, so in terms of what they have, except for one small detail which is the conditional inside that loop, right? So if I were to tell you, you know, print the names of all items which are not in stock, would you create a third method and if I were to tell you some arbitrary combination like that, tell me all of the items whose price is greater than $100 for example, right? So the problem here is that if you get requirements like this, then it is not really sustainable for us to keep adding methods to this warehouse class, right? So firstly it is too much duplication, you know, I could do that, you know, I could basically say every time I get a new requirement I add another method and it will still work but it is pretty much, it will look almost like this, right? Again it is not sustainable because like, you know, you will end up with methods which almost do the same thing, so it is not sustainable and it is too brittle. So if I want to change something, I have to change it in 10 different places, right? So what is really the problem here, right? If you look at these two methods, like I said before, those two conditionals are the only things that are different, right? So in the first one is the item in stock, in the second one is the item in stock and is it of a certain category, right? So if I were to anticipate and build for an arbitrary, almost arbitrary set of conditions, then what I probably want to do is I want to externalize that search criteria. So instead of taking in these search parameters like this individually, if I were to abstract that in something called a search criteria, then maybe I do not have to do all of this duplication, right? So what I do then is I do this refactor and I say, okay, I will create an interface called and I will call it item search criteria, okay? And it has a single method in it called test and it takes in an item, okay? So if I were to reimplement this thing, right? So I have got item search criteria and then now instead of taking in anything else, it takes in this item search criteria and then it says pretty much like what it did last time. Now instead of testing for the conditional itself, it is actually saying, does it actually pass the criteria that I specify, right? So the advantage that I have is what I can do now is I can build something like this, you know? So I create an in-stock search criteria and then you see the conditional now is external to that method itself. And now I can say create an inventory method which passes in an in-stock search criteria. Has anyone done something like this? A lot of folks have done it, right? Or maybe you haven't, maybe you have done the part that is the first one that I was showing, right? So again, nevertheless, right? This is not hard to comprehend. Now so if I were to do this, so now I have got this class called in-stock search criteria. But because it is really, what you can do is you can inline that and you can create what is called an anonymous inner class in Java because really you don't want to create an in-stock category item search criteria and so on and so forth, right? You don't want to keep creating these classes. So what typically people do is they create these anonymous inner classes and then now you have basically shortened the amount of code that you write. Otherwise you have to write that class and then you have to use that class and so on and so forth. So to recap, so what we have is this inventory method which looked originally like that and now it looks something like this. So you have got this anonymous inner class and but in terms of the number of lines of code you have probably written more than what you did previously, right? So again I mean it is okay but I don't know if it probably does not, it solves the problem of there is probably less duplication and what you can do is you can test the search criteria object that you create individually every time and you can just test that and be sure that as long as I write and test the criteria object this inventory method will actually give me the right results, right? So if I were to ask you to do a third task which is now to print the names of products, okay? So remember we said items actually have a reference to a product, right? Because an item really is a product with just a quantity. So if I were to do that and if I were to use the same strategy that I use where you create an interface and now you say product search criteria, right? So again it looks pretty much similar to what we did the previous time instead now instead of item search criteria which takes in an item it takes in a product, right? So now we have two interfaces, you have product search criteria and item search criteria. Is there a way we can refactor this without using black magic? How would you do that? Anyone? Yep. So what I would do is I would say okay I mean if you look at these two they are identical almost except for the difference in the names and the input that they take in, right? But other than that they are pretty much the same. So what I would do is I would generalize this and say okay it takes in a generic type T and now it has this test on that generic type T. So you can pass in any arbitrary class and now you can have a search criteria for that class, right? So if you look back at that inventory method now it will start looking something like this. So I pass in the search criteria of an item and then now Java with its generic magic does this and then now I can pass in an item directly and then I can do the same thing that I was doing, right? So but still you know but this is still two verbose you know it is still quite a bit of clutter, correct? The only thing really that you are interested in is the line of code which says return item is in stock, right? So but if you do not have Java 8 what do you do? One thing that I try to do is what you can do is you can extract the search criteria item into a constant because it does not depend on anything else. This can actually be written rewritten as a constant so now I say this thing is the in stock search criteria okay for an item and then I can write it as inventory in stock at least it reads well, right? So if you do not look at the declaration of that in stock and if you are looking at this most of the time then you know okay this is trying to do a search for inventory in stock, right? A big deal you know so but if you use an IDE like IntelliJ IDEA for example what it will do is it can actually collapse it this way. So what you know this is actually Java 7 code or anything lesser than Java 7 but IntelliJ IDEA and maybe even Eclipse I have not really checked will actually allow you to look at it this way if you collapse if you expand it it will look like that in anonymous inner class but otherwise it will look like this, right? So this so people who complain that I have to look at lot of code you know that that is an answer for them, right? Okay so you have to look at less because this is the part that you are actually interested in but if you are doing Java 6 or Java 7 that is pretty much it you know what you can do is you can use a library like Java has these libraries called Guava which is from Google they have a whole bunch of functional they have a where I should not say functional I mean they actually say functional interfaces. So they have a bunch of interfaces that you have that they have already written so they have so instead of search criteria they call their search criteria something called a predicate Apache Commons also has something similar so you can use some of that so that you do not have to write all of this right but pretty much that is it you know as far as Java 6 is concerned or Java 7. So if you were to say okay let us move on to Java 8 right and I must say you know Venkat stole a little bit of my thunder this morning by showing you Java code but okay you know so some of it is going to be repetitive maybe but bear with me okay so what do we have we have the search criteria right we have this inventory method right and then we have the actual implementation which says okay here is how I actually use a search criteria object to find items in stock so that is pretty much to do to print an item that is in stock that is the amount of code that I have to write okay so now we do have Java 8 so the answer is okay we can use lambda right so if you were to use lambda so that code can now become that well that okay so as you can see here the thing that looked like that can look like this now so it is a lot more terse right it tells you what it is actually doing and because the Java 8 compiler is a little bit more advanced than what it was previously it has this additional capability of doing some kind of type inference right so what you can do is instead of writing it like this you can write it like that so what you can do is you can omit the fact that this is an item and the compiler will actually give you an error if you pass in anything that is not an item but implicitly the assumption is that it is an item okay so that is all well and good you can actually take it a little bit further you know so as you can see now here you are calling a method in the item class so what you can do is you can actually use something called method references which looks which look like I guess I have too many transitions there you go okay so you can do this double colon syntax where what you are saying is you are basically saying the exact same thing but you are saying it that way where now you have to write even lesser arguably right but all of the three syntaxes that you have seen so far they are all identical you know there is no difference as far as when it gets translated to bytecode it is the exact same thing that the compiler produces but now you have got a terseness if nothing else okay a little bit of Java 8 Gyan right I mean I promise not to bore you with too much of the theoretical or technical details but a little bit is probably needed here so you have got this interface called search criteria right now and it has a single method that is abstract meaning a single unimplemented method right so it is actually called a functional interface you know so in Java 8 this is called a functional interface and what is a functional interface a functional interface is one an interface which has a single method without an implementation the reason why without an implementation is in bold is because in Java 8 now you can actually have interfaces which have implementations in there you know so they call default implementation you know so and we will come to that and how and why they actually were required to do something like this because this is pretty drastic right so far Java has avoided having any behavior in interfaces under the pretext of trying to avoid the diamond problem right so but they have actually gone back on that so now just like in Scala you can have interfaces or traits just like in Scala traits you can have interfaces which have implementation in there excuse me did anyone have anything say okay so what you can do is so in addition to that right you can force the compiler to you can mark my interface interfaces like this with this functional interface annotation what that gives you is you know so it prevents somebody from adding another method right if they add another abstract method to this interface the compiler will actually give you an error saying this is declared as a functional interface which means it can only have a single abstract method so that is something that they have added a sugar in Java 8 the several of these functional interfaces which are available in this new package now so I will let you browse through that and you know get more so coming back that whole thing can now become that okay so if using the standard interfaces that are available in the Java in the Java standard package library you can do this now so items is a collection and then you have got this additional method called stream all collections now have this method called stream and it is actually a method on the I think it is either collection or interval you know one of the two I cannot remember which one so it is actually implemented method you know so you can override what stream does but it is actually there and all collections get it okay so one of the reasons why the designers of Java chose to enhance interfaces to have implementation is basically to allow something like this to happen where they did not want you the implementer of a collection to provide a stream method from scratch so they actually do that for you can obviously override it but it is already there so you do not have to do anything so as long as you have got a collection interface you get that stream method for free so what is this doing so you basically have this thing called a stream which is you can think of it as you know the simplest analogy is that you have got a stream of data coming in so if you are looking at a YouTube video right you actually you are getting a stream of data which then renders into a video right so you can think of it in those terms right so now given that I have got a stream of items I am filtering it and then using filtering it using the item is in stock which we just saw and then what I am doing is I am mapping it meaning I am converting it adapting it into a collection again or a stream actually of names of those items so now I will end up with a because I have transformed this I will end up with a stream of strings which are basically the names of those items and then finally for each I will print them out okay so all of that became that much right pretty neat huh so some of the more popular standard functional interfaces you know so filter is one you saw you know which again gives you a subset or can give you a subset of the of the stream that you are working with okay and then map again to convert one form into another so if you have got items you want to convert them into the names of those items you would use something like a map right and then for each which you again saw which is basically a bunch of actions that you want to do so this is more this is less functional because what this does is it actually it does some mutating you can do anything pretty much you know but it does not return you back a result so in that sense it is not functional like a map or a filter okay it is also called a terminal operation in Java parlance meaning it is something that forces work to get done remember in the morning Venkat was talking about these things being lazy right so a lot of the methods on stream actually return back a stream itself you know so you when you filter a stream you get back a filtered stream when you map you get back another a mapped stream so until you have got and all of those are called intermediate operations intermediate operations do not require the api to do any work you know so if you were to just stop by doing a filter or a map nothing will happen okay but a for each for example will actually force work to get done right so so having having said that so if you were to do this now filter in stock items and then print the whole item itself not just the name so this is fairly straightforward to do now so you should you should have got the drift of this so now you say items are stream filter in stock items so we were mapping them to the name right now so we say okay we will map it to something else so let us say we have okay so there you go so you have let us say you have got an overridden two string method on the item so you can do that right so what if you do not have a method right so you can always always go back to the to the lambda syntax so you have got two you have got method references and you have got lambda so in this case it is doing some arbitrary thing with the item itself so it is taking the name and then concatenating the quantity you know so but you could do anything you want to do with right so let us say if you let us say you want to print the number of items in inventory so if you want to do that how would you do that so in Java 6 you would do something like this right you saw this you start with the sum of 0 then you add you keep accumulating the quantity and then finally you output the sum so in reality what actually are we doing here if you were to decompose this we are doing three things one we are providing an initial or a default value so if there is no item in the collection then it will return a 0 and again it is also an initial value because everything starts with a 0 when you are doing a sum right second thing you are doing is you are accumulating a result right so when you say sum equals sum plus quantity that is basically accumulating the result and then finally you are you are combining the result into the sum itself and returning it so if you were to do this in a functional way what you would do is something like that so you would you would call this the reduce operation it is exactly the same as what was happening there so you are starting with an initial value you are then accumulating and then you are finally consolidating into a result okay if this seems like Greek and Latin then there is an easier way to do this right so this is equivalent to that but again it might take you a little while to get used to this way of thinking about it right so there is a an easier way so what you do is you have a stream of item and then you map them to the items quantity and then you reduce them using a sum function so the reduce here takes in a function by itself you know and you will see them being called higher order functions in some places so here what you are saying is after the map you will basically end up with a collection of ints or rather technically a stream of ints and then you will apply the sum function to each one of those items in the stream starting with the value of 0 right so if you were to do that then integer already has a method called sum so you can use that instead of doing that writing out the sum function yourself so you can use the built in right it is identical again even more conveniently what you can do is you can say I have got a stream of items I want to map them to a stream of ints now map to int actually returns me a specialized stream called int stream and then on the int stream I have got a method called sum and I can just use that so this is a lot more intention clarifying than the previous two right although all of these three are just as functional as the other but this is probably more intention clarifying yeah terminal operations yeah correct yeah so you can think of this as being equivalent to a builder right so has anybody use the builder pattern so if you are you know so if you are creating a complex object you know a fairly frequently used pattern is you know you say let us say I am creating what an item right an item has a price has a name has association with other things and so and lot of them are not mandatory so you can basically keep building and then finally say I want an item right so you can think of this stream being equivalent to a builder and the sum being the in this case the terminal operation sum is the final thing where you actually build the result but if you do not do that then nothing is going to happen right okay so next task if you were to print the number of items in all our warehouses so assume you have got a bunch of warehouses and you want to print the number of items in all of those warehouses right so if it helps the Java way would be something like this right you start with a total of 0 then you iterate over the warehouses then you iterate over each item in each of those warehouses and then you accumulate the quantity and then you finally print the total right so again I mean this is something that most people understand but again if you look at it that the double for loop is pretty ugly right so if you were to rewrite this in functional terms then what you would do is you would say use this thing called the flat map right so you can think of a flat map being equivalent and then and other languages call this the fold operation right so flat map being you have got a collection of collection and you want a flat collection of those collection of collection so you have got a collection of warehouses and the warehouse each warehouse has a collection of items so you flat map items in the warehouse so you get flattened collection of items across all of the warehouses right so that is a flat map and then you do the same thing that you did previously you map them to an end and then you summit any questions what we can also do okay we just saw this right yeah sorry. So the next task like print then item with the least amount across warehouses. So you want to find out which item has the least number of items okay. So the way to do that is again in Java 6 now what you have to do is not only do you have to track the minimum quantity you also have to track that item which has that minimum quantity. So this one looks very similar to the previous example except now you are also tracking the item which has the least quantity and then you print it out. So in functional terms what you would do is you would say warehouses.stream and then you would flat map it to stream of items and then you would say min and then here what you are doing is you are using a min function function to compare two items you know. So you are flat mapped it to items and then this thing will compare two items using the quantity. So again this is obviously expressible slightly more conveniently. So what you can do is you can say comparative dot comparing item dot quantity or item colon quantity right so it is exactly the same. What you can also do is you can make item implement the comparable interface thereby giving items some natural ordering. So numbers have a natural order one is greater than two and so on right. So if you do that with the item then what you can do is you can do this. You can say flat map it to a stream of items and then compare them using the natural order of the item. Notice that I very subtly switched over to using a parallel stream and this is something that Venkat mentioned in the morning. So it is very straight forward to actually do this in a way where you do it sequentially using the stream method and then you do it using the parallel stream where now it is all of this is going to happen you know in parallel. So the fact of actually having to deal with multi-threaded code is greatly diminished by just doing that right. Yes so the way I understand a question is you know is it internally implemented the same way as the double for loop right perhaps it is perhaps it is perhaps it is not you know so the thing is that it is the thing to consider here is that that part of doing it is abstracted away from you know so the double for loop is basically focusing on how to do it right whereas this focuses on what and this is also something that Venkat mentioned in the morning. So if there were a better way of doing it then you do not have to think about it as being implemented as a double for loop to do that. Now in reality maybe it is implemented as a double for loop or maybe it is not but that again is not your concern anymore right it is a concern of the implementer of the library. So there are two ways right so now one what you can do is you can flat map it and then you can filter or you can filter and then flat map right. So you can filter first and then flat map either way it does not matter because these are mutually exclusive operations so as long you want to get to the result of filtering in some using some criteria and then getting a list of getting a stream of items right so does not matter how you arrive at it. So in the case of a min which is the terminal operation you cannot conclude without having looked at every item in which is under consideration. So it does not matter if you are using the min as your terminal operation you have to do you have to go through every one right but the flat mapping itself can happen in parallel so you have got let us say 10 warehouses each of the items in those warehouses can be collected concurrently right as opposed to doing it serially in the case of the double for loop. So in the double for loop you are forcing yourself to say first I look at warehouse number 1's items then warehouse number 2 and so on and so forth whereas now with this you can go all parallel and keep collecting the min and then say okay here is the min from each of the warehouses and then I can say okay get me the min from the min right say that again okay sure. Oh okay yeah in that case then it is so it is not min so it is the first let us say you say find first right then there is no need to do anything more than just looking at the first item and returning you the first item and the order is again in this case non-deterministic because you have not specified any order right so yeah I mean there it might not be implemented as a double for loop right but in the case of a min you probably have to go through every one of them so the find first is a lot more is a lot less expensive than the min but again it depends on each terminal operation individually. So if you are doing all of this in Java 6 what do you do right you do not have Java 8 how many actually I forgot to ask how many of us are actually working with Java 8 right now okay how many of us are working with Java 5 but greater or equal to Java sorry Java 7 to Java 5 Java 5 to Java 7 a majority okay and is there anyone who is working with something 3 Java 1.5 like 1.4 1.3 okay I am sorry for you. So firstly you know you need to stop whining so it is not the end of the world so Java 8 is pretty cool in terms of the API that it introduces but it is not the end of the world a lot of it is available it is not as you know so lambda really I mean lambda and the method references they are all syntactic sugar on top of what is the anonymous inner class right anonymous instances they are basically all of them are anonymous instances. So it is still possible to do all of this with libraries like Guava if you haven't looked at it go look at it again you know so it may not result in as terse code as you see here but you can actually tend towards that so it is possible though it does require a little bit of effort and sometimes it might feel a little unnatural so like you said you know the if you have got very small use cases then having to go through the pain of creating an interface and then implementing an anonymous inner class for that interface might be too much of a hassle to do but if you have got a lot of search functionality then you might want to you know consider either using something that Guava itself provides or implementing something of your own if Guava is not an option one thing that you can do is as you can see here streams are immutable they are read only they basically are not on the collection so they could have done all of this on the collection interface itself right but the reason why the designers of the language shows to create this thing called a stream was they wanted to do this make this clear distinction of this actually being a read only view of your underlying collection right so firstly I mean you if you want to get to being functional prefer working with final variables so what that means is try not reassigning variables try not stooping to using setters for example try passing in dependencies through your constructor so then what you can do is then the Java compiler will allow you to mark your instance variables as final right or your or your method parameters as final so that you I guess nobody does reassigning a method parameters anymore but but instance members right avoid reassigning them by passing in initial values through a constructor and make them final and see if you can actually get it to a place where you do not have to modify it definitely is possible but but again try doing that and see how much you have to change right so if you are if you are in Java 6 again something you can do is to minimize the use of wide method wide method are evil because so have you know so remember the first task that we worked on the the one where I said create a you know here is a list of items and print the name of those items that method was actually a wide method right if you were to write a test for that method you will have to go through a lot to be able to test the fact that you are printing the name correctly so prefer the use of method which taken input do not mutate the input and return some output and then you will also find that it is a lot more easier not only to test but also to to to understand what is happening wide methods prevent that from you know they they basically say I am going to do something with the input that you gave me I do not know what right and the only way to test that is through a side effect so an example of that is let us say I have got I have got a method which updates the price of an item right so I say set price and now inside the item I have a price attribute I update the value of it to a new value now how do I test that I have to test that by saying item dot get price and assert its value right so the only way to test those kinds of method is through some kind of side effect so again you will find that your designs improve when you try avoiding wide methods but again I mean avoiding wide methods may not be possible all the time given that wide methods are unavoidable try to eliminate them to the periphery of what you are doing so so for example you saw those terminal operations right those terminal operations were the last thing that you did and and that it it is like that for a reason right because everything that follows before actually does either a transform or a filter right and most operations can be expressed in the form of a transform or a filter so map flat map or filter right these these three are the most important functional operation so all of those actually taken input and return output and the final thing that you do is your transform or is your actual mutation if you have to do it restricted to being at the periphery of what you are doing and then what you can do is you can you can test you can you can control the design of what you are doing for a majority of a system without the use of wide methods this one is fairly straightforward avoid mutable global so now when I say mutable global in Java it basically means static right so so the singleton is is a classic example of that so avoid avoid actually writing singletons you can use semantic singleton so so you have got what I mean by semantic singleton is for example if you are working with something like a dependency injection contained like like spring right so spring can actually tell you you can write the object just like you would do as if it were not a singleton and then you can mark it either in the class itself as being a singleton which means the container will only create one instance of it ever right so so avoid use of singleton and avoid use of use of global state but but that does not mean static methods are evil you know so I would like to clarify so static methods that do not mutate are actually functional right as you can see the sum function it is is is a static method on on integer but but it is not it is not evil because what it does is it takes in two inputs and returns back output so that is okay but a static mutable thing is is bad because that is something that you will find yourself mutating in several places and and when you mutate something in several places you have to keep track of them you have to you have to worry about concurrency and all of those those things that Venkat mentioned in the morning right finally avoid avoid returning returning null you know null is the is the bane of of of you know I do not know who it was who said that the single most evil thing is is is the is the null keyword so what that means is you either return something that is not null which is a substitute of the null or return empty collections right as opposed to returning null the reason why then you can you can actually focus on on doing the real thing that you want to do as opposed to you know you know spreading your code with with null checks everywhere right finally if you are in the Java word read this book effective Java is really awesome you know although he does not mention Joshua block does not actually mention functional anywhere explicitly but the style of code that he actually advocates is very much along the functional paradigm and this this existed years ago I mean years ago years before functional took off as as the next big thing right so so that book actually gives you lots of ideas on how to actually do it in a way where you can you where you can avoid all of those anti-patterns that that I mentioned here again you know so I mentioned looking looking at the the guava or the or the pache commons lang library right so this is something that that the guava documentation actually says and I thought I just say it here you know so so if if you are in Java 6 Java 7 land you know use some caution now you go from this this this conference here and and do not try to make everything functional tomorrow right so so again that that is really good advice from the guava documentation and those guys are really awesome as well you know so so Google does not mess around right but again it is it is it is pretty good advice that is it thank you very much any questions use of nulls right and this is also something that is very common in a lot of other languages so the the concept here is remember again you want to find the item with the least quantity so now what you can do is when you return a min right of the of the item you actually do not return an item you return what is called an optional item okay so what this means is I may not return you anything and an optional is something that that is a new that is a new thing in in Java 8 it already exists in these libraries like guava and so on and so forth but this is now part of the standard library what this tells you is I may not give you anything because there may not be anything to give you right if there are no warehouses and if those warehouses have no item then there is no way I can give you a min because there is not a min right so in that case you return an optional and the advantage of using an optional so one question is okay I have got an optional so how do I know if it has something or not right you have to actually ask if you have to ask the option do you have anything if you do then give it to me right that is the way to you actually conclude but the advantage that you get by using an optional is that you can interchange the use of an optional with a collection so you can iterate over it just like you would do on a stream or a collection right so what it allows you to do is you can now treat that item that you get the optional item you can further chain more operations on it and it will not blow up with an null pointer exception because optional is smart enough to not return something and do the right thing when there is nothing in the min to do okay so another thing to do is if you are forced to return nulls right now in java 8 you can use optional which is part of the standard library but if you are not on java 8 guava or apache commons have this concept of an optional which you can consider using but again it has its singles because the ones that are in guava and apache commons line are not substitutable for iteration you know so it is not a direct thing that you can use like this here where it is a substitute for a null it is just a substitute for a null but it is still something that you can use as documentation if you are forced to return null you return an optional of an item and avoid you know the proliferating whole bunch of null checks that is the thing I had yeah please so yeah so the benchmark this off category method it actually takes in a category that is that is not part of this function you see this category is passed in so I am looking here to see if an item is in stock and it belongs to a certain category here you see that now this category is not part of this function right so you cannot actually create a constant out of it you are actually passing in context which is not part of the function into the function so in java 7 when you are doing anonymous inner classes or anything below that you have to declare that as final I mean lots of have encountered that right in java 8 the same constraint still remains okay the only difference is that you do not have to declare it as final explicitly you can remove the final keyword but if you try to mutate that within the context of your function it will actually complain and give you a compiler error okay so but other languages like Ruby or Scala will allow you to do just all of this just fine you do not have to worry about those kind of semantics so again you know so the to answer your questions a question explicitly if you have got a whole bunch of java developers then then it then it might probably be easier to make the transition to java 8 because in terms of the number of new concepts that you have to learn they are probably much smaller but you get a lot of the benefits but if you are looking for a more radical change then you probably might want to look elsewhere any more questions okay thank you very much