 Hey everybody, this is Brian and welcome to the 26 Java tutorial today We're going to be covering the map interface specifically the tree map Now what is the map interface? the map interface says that an Object that maps keys to values. What does that mean keys to values? Think of an employee you have an employee and each employee has their own unique ID number and you can kind of go down here and see the definition of the interface You know how there's like a clear contains key contains value blah blah blah. You notice right off the bat. There's no ad Well, how do you put things in you do a put? There's put and then there's the key and the value. So we're gonna just go right into some code here And we have our import Java dot util dot tree map and notice how it's Tree map you always understand the last part of this is the interface Tree is what the class is and it's following the map interface Let's go ahead and create a tree map here And right off the bat, you notice we have two generics anything within these brackets is a generic We have a K and a V. Well the K is the key and we're going to say Integer and the V is a value and we're going to say string And we'll say employees equal new Tree map and always try to use IntelliSense as much as possible just to save your your little digits some typing You don't want it up with arthritis later in life All right, so very simply we're saying tree map with our key as an integer and our value as a string those are generics and Then we're calling it employees and we're saying new tree map integer strings and we're calling the default constructor And we're gonna say employees put and notice how it's calling integer key string value And we're gonna say one And the name let's just say Bob. I like Bob Bob's a good name. You can spell it the same forward and backwards Right and Bob's employee ID is one. We're gonna say employee 22 is Mary and Employee 34 is Whoops Chad So there's our employees and that's how we put them in the list is using the put Now how do you extract these? Well, there's two methods one is to get them individually and we'll say system dot Out print line And we're gonna say employees dot Get Now you might have noticed while I was typing that there's a contains key You can use that within an if statement, you know if contains key one equal true then Bob exists We're just gonna do get for now, so we'll say get and we're gonna say we want to get let's get Mary 22 So we're going to get the employee with the ID of 22 Let's run this sure enough. There's Mary. Now. What happens if you? Let's copy and paste Mary here. Let's say we have a new employee Helen Who has the same ID as Mary? What happens here? Well, you notice how it prints out Helen instead of Mary now because what we've done is we said there's already an element with 22 Is the key so just overwrite the value Now as promised there is another way to do this and we'll say for And integer I in employees dot Keyset a Keyset think of it like a key ring it holds all the keys And we're just gonna copy and paste this out here and we're going to say Get the employee with that Key, so we're saying for each key in the entire tree map Get the key which is I and we're going to get And just print it out here see there's Mary Bob Mary Chad now where's this extra Mary come from we forgot to comment that out Sorry for the confusion. There's Bob Mary Chad very very simple Let's actually put Chad is zero Just to see what happens here You notice how it changes the order Chad suddenly jumped to the top of this because it orders it by key not by value Something to keep in mind when you're iterating over these tree maps So this is Brian. I hope you found this video educational and entertaining and thank you for watching