Java 8 Q&A | 🔥 Asked in Myntra | Find Nth Highest Salary Using Java Streams API | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java techie recently one of my friend attend interview in myntra where the interviewer asked him on tricky question on javaite programming that is write a program to find nth highest salary using Java 8 stream API nowadays If You observe in each and every Java interview you'll get a question on jobite programming at least one so that's the reason I thought to walk you through this use case with very simple approach okay all right so without any further delay let's get started [Music] so let's quickly create a new class I'll name the class name something like nth highest salary demo okay now I will Define the main method so that we can execute our code from here let me Zoom this for you now let's assume I have a input like this I have this map with me okay and you can see the key is the employee name and the value is the salary now we need to find out the second highest salary so if You observe here 1700 is the highest and second highest is the 1600 so I need to display these two key and value or you can Define you can display the key as well now the requirement here you need to write a program completely using javoid stream to find out this as a output who is having the second highest salary also you need to make your code generic so that anytime let's say I want to search fourth highest salary I can able to get the result okay so that is how you need to design your code so the approach is trade for Android I can simply sort the map in descending order then 1700 will come first then this one will come second and since we know the index in list begin from the zero I'll get the first index that is the quick solution right let's give a try with that then we will find out what is the drawback with that then I will come to the proper solution so first I need to sort a map in descending order okay so map one dot entry set convert it to the Stream fine then just use sorted and I want to sort by value right so there is a direct method in map map dot entry dot comparing by value okay now this sorting algorithm will sort your map based on the value now again I will just convert it to the list then simply I will get the first index because in the zero Index this entry will be present and the first Index this entry will be present this is what our second highest salary since the index begin from zero in collection we need to get the one index okay that is that is simple logic right so let me assign it to the variable let's say I'll Define result now if I'll print this let me run this we are getting the result as this one Ankit 1200 it means the setting is happened but it is in ascending order right if you have your thousand is the less salary and the next is the 1200 we don't want to find the lowest second lowest salary right we want to find out the second highest salary so that means either from this ascending order get the last I mean get the size of your map and subtract minus one or the simplest approach is just search in the descending order okay I mean I just want to reverse the setting so there is also a method from the collections Dot reverse order and give your comparator okay now if I'll run this I got the result here can you see here Tom 1600 so rather than right here since I want to design a generic method I will copy this or I'll just Define a method here itself okay public static who will return me this map entry or if you want you can return the key as well get nth highest salary you can pass the which number you want int number okay and also we want to pass the input as a map so you can take this as a input I mean this is simple right I am just extracting this piece of code to a method fine now I'll completely copy this piece of code and I'll just return it back from here what happened okay this need to be map not entry fine now simply I will call this method rather than right here I will remove everything I will call the method what I have written here get enthused salary and give the number i1 second has already right and then I will pass this map this is simple right now if I'll just print this assign it to the variable let's say results and if I'll do this out results the output will be same there is nothing change we just modify our code or we just follow the proper coding standard see while attending the interview never write a code in main method directly just gather all the acceptance criteria from the interviewer I mean what should be the input or what should be the output format is expecting from you based on that always write a method to demonstrate in front of interviewer okay that is the best practice I could suggest you so this is how we got the result now the question here is this the correct solution is this piece of code will work for any type of input that is your something the tricky part here also if You observe we are hard coding the number here right that is what we don't want so we are already giving the parameter as a number what user want we'll just take that value and we'll do the minus one since the index begin from 0 we just subtracting it by minus one that's it simple right now let me run this for second highest we are getting 1600 that is correct because that is the same code we we tried before now let me try for I want fourth highest user the employee who is getting fourth highest salary just run it we got this guy 1400 so if You observe in the map 17 16 15 and then 1400 this is the fourth highest right similarly since we make it generic you can just pass the number what you want to try but again the question here is this piece of code will work or not for all kind of scenario I'll prove it right away then we'll find out the better solution if it will not work okay that is how the tricky question he faced initially he also designed the same approach then interviewed just slightly change the input and then code didn't accept that input and it give the random order okay I'll show you that don't worry let's say I got this as a input this is the map too thousand two hundred then twelve hundred thirteen hundred now if You observe carefully if I want to find out the second highest salary 1300 is the fastest and the second heist is the 1200 But If You observe there are many employees who is getting the second highest salary so our code will not work we'll prove it right away so let me call the same method I'll directly print it okay get enter salary second I'll give this map to we'll see whether it is working or not can you see here term 1300 this is completely wrong right we should get the result 1200 with all the employee I mean since this is the second highest number we should get all the corresponding key from this map as a name of employee who is getting the second highest seller in this structure so whatever the code we have written is not working that is your something you need to get the acceptance criteria before write down any code now let me tell you the solution for this okay so just comment this out I'll write the logic here then we'll extract it to the method Now The Simple Solution If You observe here I can do the group by by this map value so when I say Group by let's say 1300 is getting Tom and Daniel these two are getting the 1300 so I should take the key as a this particular number and value as the number of employee name or name of the employee list of employee name right similarly if I do the group by using this 1200 1200 will be key then Ankit wavana James these three will be the employee name like this I can maintain the group by from this map then based on that we can apply the sorting and we can subtract minus one number minus one number minus one or what we have done here right let's give a try step by step so first I will take the map dot entry set converted to the Stream then just use collect collectors dot grouping by we want to group by this value from the map simple right get value from the map and just do the group by now if I'll return this let's see how we are getting the result then we'll just formatize we need it okay as for our requirement we can format it now let me print it let's run the code can you see here we are getting we are just doing the group by value okay so this is the salary and Ankit James and wavana these three are getting 1200 now 1300 with the entire entry set 1000 with entire entry set but what is our requirement we just want to get 1200 only the name not value as well okay this complete entry set we don't want we want only the key from the map that is what the mapping we need to do the simple step is to do that mapping what I'll do just tell to this grouping by hey I want to just get only the name from the map I mean from the output If You observe carefully we just want this name we don't want the entire object or enter entry set so you can tell him here okay so hey collectors do the mapping for me what is the mapping you want I want to do the mapping using key use the method reference fine once you've mapped it then just give me a list so you can Define here collectors dot to list let me format it properly fine where there is error let me check okay simple the return type is changed so just remove it I mean this particular local variable I'll remove it then assign to the new value let's say okay let it be map fine now we just tell to the collectors just map the key don't map the enter entry set here okay I just need only this key not enter entry set now let's run it we are getting the value and corresponding key value corresponding key this is what we are expecting right now the steps is very simple since we have this is a key this is a value this is key and value we just need to sort it in descending then we'll get the first element that is what our requirement right it's not first based on the number you want though the way we have done in the previous approach okay simple step so simply just get the entry set then convert it to the Stream fine then just sort it by key so again if You observe here we want to sort it by key now that is what our requirement because we want the list of employee name who is getting the same salary that is what I am looking for okay so here I just need to sort by key again in reverse order so first let me sort map dot entry Dot comparing by key I want it to in the descending order so just copy this comparator and then use collections dot reverse order give this comparator fine once you get it just convert it to the list once you convert it to the list get the number what you are looking for for now let's test it for the second highest salary okay so just change the return type so maybe I have already defined salary result okay just print this so up to this is the tricky part next as usual right we are setting the map based on the key in the form of descending order then we are converting it to the list and we are getting the first index because I need the second highest salary if I need the third highest salary I need to change it to the two that will make Dynamic okay don't worry now let me run this this is the guy these are the three person who is getting 1200 which is the second highest salary number in the map okay now I will move this piece of code to the method then we'll verify by giving the other input okay so let me copy this or first let me write Dyno method I'll copy the same method structure 10th High salary or I'll just change fine now I'll just simply copy this logic so I need to change the return type as well just change the return type let me copy this fine and here we want to make it dynamic number minus one if you search for the third highest then 3 minus 1 equal to 2 okay fine now let me call this method I'll remove this piece of code so what all the field you need to give let's say I want second test and I want to give the map to okay now let me run this I should get 1200 and all the three employee name we are getting now let's say I want to verify for third highest salary let's see we'll get only one name as as far I remember no 2 right so let me verify in the map yeah there are two employee who is getting this third highest number okay now just give this map one to the Dynamic logic what we have written it will work for everything okay map one the third highest let's see what will be the output can you see here 1500 so if You observe in the map one 17 16 and 15 this is the third highest right so the second code what we have written is completely generic code it will work for all kind of input just play with this piece of code rather than buy hard the code just understand why why we have written each and every method or what is the purpose of each and every method that is how you can able to write the code or you can able to improve your coding skill rather than buy at the syntax okay so that is the reason I thought to explain this use case with each and every method and their use okay so just give a try if you find any interesting Java programming interview question please feel free to share with me in the comment section I'll definitely give a try and I will try to solve that in front of you okay do let me know in a comment section if you have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 9,491
Rating: undefined out of 5
Keywords: Java 8, javatechie java 8, java 8 interview Question, java 8 programming, find Nth highest salary using java stream, javatechie
Id: eBDN04LlEOg
Channel Id: undefined
Length: 18min 45sec (1125 seconds)
Published: Sat Jun 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.