hello guys welcome to this tutorial in the previous lecture we talked about what were the new features introduced in Java 8 so today I am going to show you a code snippet wherein you will see what was the new feature introduced in Java 8 and how does it make the developers life easier so here I have opened eclipse I’ll be writing the code in Eclipse I’ve just downloaded the Eclipse and I’ve just opened it so we’ll be creating a project and a class class to sort lists of string objects so to create a new project at the upper left hand corner you can see file tab this click on that new Java project just give any name I’ll be giving Java 8 sample and as you can see it has chosen execution environment Java standard edition 1.8 because that is the version which have installed so leave rest all as defaults click Next and this will be the project overview and justly finish so as you can see the project has been created and the system libraries have been already added to to create an execution environment using Java 1.8 so now you will create a class to create a class just right click new class give exam give the give the name Java salt and I’ll be including the main method say finish so let me go ahead and create lists of string objects list string some places you other list of string objects so I have created an object of an object of array list containing string objects so it is showing me an error asking me to import the package for the list class so you you can see many options here but you have to choose Java dot util the same here okay now I’ll be adding few places in the form of string let’s say Texas places dot add you can you can find the list of available options in add so I’ll be choosing the first one California the last okay so I’ve added four string objects here so now what I’ll do is I’ll create object of the class Java sort Java sort equals new Java sort so now I can call methods on this object although we have not created any method but we’ll be creating so what I’ll be doing is Java salt dot salt salt Java 7 let’s say we’ll create a method Java salt with salt with Java 7 and I’ll be salting this list using Java 7 obviously it has to give me an error because we have not created any method so it will give me a suggestion to create a method I’ll say yes so you’ll have a method here so now they all know that the salting in collection framework the sorting is done by implementing two interfaces one should be comparable interface another should be comparator interface if you are going with the natural order then you have to use comparable interface and if it is unless other than natural order then you have to implement a comparator interface so we’ll be using the second method and that is done by collections dot salt as you can see the salt has one list this will be the compatible the comparable interface uses just the list to salt in the Nash natural order but since we are using comparative method here will be using going with the second method so list here my list is places and here I have to create an object of comparator so I will be saying new comparator of type string which I am going to compare with and in the same the second argument itself I have to implement the method the compare method of comparator interface so I’ll add all of that as the second argument so let me see what is this import comparator here and then it says add unimplemented methods so it’s compulsory that for interface we have to override the method compare so let’s leave as it is string open and string OH – so next what we do is it has to return and int always the compare method it returns 3 3 int values that is negative 1 0 and positive 1 positive 1 meaning first string is greater than the second string if it is string it will check the first letter of first character of the string and it’s salts in the alphabetical order so if the string is the first string let’s say Texas if it if it if let’s say Texas and California the letter C comes before Texas so it means that California is less than Texas it has sorted in the alphabetical order so if if it compares Texas and California it means that Texas is greater than California if it is if it is greater than it returns 1 if it is less it returns negative 1 and if it is same it equal it returns 0 so in the return statement what we’ll be adding is my first string that is open dot compare to the another string my another string is Oh true so this will be the second argument for the salt method and this will be the this will be my implementation for salt with Java 7 similarly what I am going to do is I am going to call Java salt dot salt with Java 8 again we’ll be implementing this method here so if it asks me to create a method I’ll say yes so now so now what we do is all of this is not required when it comes to Java 8 because Java 8 is using a new feature which is called lambda expressions wherein it is minimizing the efforts which which is needed to create a function and to call a function so just remember lambda expression is nothing but is let’s say if you have a function like so the two arguments it is used to compare or the two arguments which will be passing it is used as the argument list for that and the return statement whatever you write it will be the expression which it will be returning so all of this is going to be shortened and let’s see how Shen’s dot salt so this would be like salt me places and I’ll give it the arguments that is o 1 comma au 2 and we will say like this this is useful now a lambda expression like so and I will say just as we used here open dot compared to co2 we’ll just add the same thing here yeah so so this so Java 7 what all we did here this is equivalent to the same one line just one line is does the same thing as it is doing here so let’s run the program before running the program we have to we will just print out the sort to just see the difference so let’s say it’s system dot out dot println places and also here we’ll do the same thing after Java ate it so it will print the places first and let me add another object string object to see the difference and then print the places so let’s save it and run and you can see the difference between Java 7 and Java 8 in Java 8 the we are using the lambda expressions more examples about this will be seen in the next lectures thank you

Tags:






Youtube
Facebook
Google Plus
Twitter
TutorialDrive


Apache Zookeeper Tutorial

Apache Kafka Tutorial

Apache Kafka Security

Elasticserarch n Kibana

Java 8 Tutorial

Log4J Tutorial

Apache Storm Tutorial

SQLite Tutorial

Apache Ant Tutorial

Related Posts

blog

Apache Kafka Commands Cheat sheet

Spread the loveKafka Topics List existing topics bin/kafka-topics.sh –zookeeper localhost:2181 –list Purge a topic bin/kafka-topics.sh –zookeeper localhost:2181 –alter –topic mytopic –config retention.ms=1000 … wait a minute … bin/kafka-topics.sh –zookeeper localhost:2181 –alter –topic mytopic –delete-config retention.ms
Read more…

blog

What is Apache Maven | Apache Maven complete tutorial from scratch pdf

Spread the love In this post you will learn the complete tutorial of Apache Maven build tool What is Maven ? Apache Maven is a software project management and comprehension tool. Based on the concept
Read more…

blog

Practical Guide for Web Development in 2018

Spread the loveWelcome to my practical guide  for web development in 2018 in terms of  technology and career. Before we start I just want to  mention a few things, you don’t need to learn  everything that
Read more…