Check out a free preview of the full Java Fundamentals course

The "Map" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses Maps as unordered unique key-value pairs that do not inherit from the collection interface. The put and putIfAbsent operators and how to set immutable objects are also covered in this segment.

Preview
Close

Transcript from the "Map" Lesson

[00:00:00]
>> So, map is a special data structure, right? It does not actually inherit from the collection interface. So, all of the other ones that we talked about explicitly inherit from the collection interface. Map does not, but it's still kind of considered a part of Java's overall collections framework.

[00:00:26]
So it does contain collection-like operations. And basically those can be used as any other collection. Some differences somewhere we'll see those. So maps are used to hold key value pairs. An example for this would be a list of maybe bank transactions where each map entry has a unique transaction ID serving as the key, and then the value would be the actual transaction object or something like that.

[00:01:03]
So we'll go ahead and create fruit. As a map, you'll notice it looks quite different than the other data structures. So back in our main, I'm gonna comment out queue, and I'm gonna add map demo And we'll go ahead and define that. Okay. So we can say map.

[00:01:40]
This time I'm not gonna call it fruit because it's a key value pair so it needs to be a little bit more involved. So we'll say fruit calories for example. And then that way we can say, okay, the key is the fruit apple, and then the value would be as calories or something like that, all right?

[00:02:01]
So we'll instantiate this. I'm gonna use HashMap, which is a popular implementation of map To add elements to a map, we don't use add this time. Again, map is not technically a child of the collection interface, so it does not have access to those methods that we got from interface.

[00:02:30]
So to add something, we would use put. Put is the one to use, and you gotta put two objects here. Maps take objects, the key is an object, the value is an object. So the first object is going to be the key. So we'll say apple, and then the value would be the calories, and that's 95.

[00:03:01]
Now, I was able to put this number here, but again, this is an object. So that is represented by an integer, not an int, okay? Let's add a few more, fruitCalories.put. A lemon is 20 calories. A banana is 105 calories And then we'll add one more, an orange is 45 calories All right, Let's print the map Did I already say if a map is ordered or unordered or did I not say?

[00:04:07]
Okay, so if we print this, notice it's not in the order that we added it. So a map is unordered. Also a map cannot contain duplicate keys. The keys must be unique. So if I were to say, fruitCalories.put, and I try to add another lemon, and I said it's 17.

[00:04:47]
What this will do is it's going to replace the lemon that's already there with 17. I'm sure you can imagine this is going wrong [LAUGH] where you thought you were adding another element, right? Cuz think about this, the way we're doing this is not how you would do it in real life because you might add things to a map later on.

[00:05:18]
You might have had the map come to you as a parameter and then you started adding objects to it, not realizing crap, there's already an object with that key, and now you've inadvertently overridden something. So there is another method that you should use. Instead if you just wanted to add a new element, you can say put if absent.

[00:05:43]
And that way, it'll at least check first. But if you definitely wanna override it, then yeah, use put. So if we say put if absent, we should get only that one lemon that's 20, and it didn't override it that time. And then if you wanna remove, you remove it by its key.

[00:06:14]
So I can say remove lemon. If you want to get the value of a specific object, you can do that as well. So we can do so by saying, let's say we want the value for banana, banana calories. What's the calories of a banana? So basically you're looking this up in the map.

[00:06:52]
So I can say fruitCalories.get, and I can give it the object, the key, right? So if I give it the key, it'll return me the value. So it says bananas, yeah, that's 105. If I wanna know if the map contains a certain key, I can do that as well.

[00:07:21]
So we'll say contains orange. And that method is called contains key, and then you give it the key. Yep, we got an orange. Yeah, question.
>> For these HashMaps, is it always paired values or could it be an arbitrary number?
>> What do you mean?
>> Well, we got orange, then we got a number.

[00:07:57]
Is it always a pair, like two values for each one? We got 11, 20, but could you assign another trade or another value to it other than just the string and the numeric?
>> In this specific one you mean or in general?
>> In general.
>> So you can do any type of object as the key, any type of object as the value, but they are a pair.

[00:08:25]
It's always gonna be just two, key value, key value. It doesn't have to be a string and it doesn't have to be an integer. Any type of object is not. Yes?
>> Is it a good idea to use generics with HashMaps?
>> Yeah, we'll talk about that in a bit.

[00:08:49]
>> But you couldn't have two values associated with one key?
>> No. And you couldn't even add the other key with a new value, right, because then becomes a duplicate and they don't allow duplicates. So one you need key, and one value for the key. The values can be the same across multiple keys, right?

[00:09:13]
So maybe two of these fruits had calories of 95. That's perfectly fine, but that's it, all right? And then I'll quickly show you about the immutability, I mean the immutable way. So this is really fun to do. But again, you have the limitations here. So I'll say, immutable fruit calories.

[00:09:41]
And we can say, why do I keep spelling that like that? And we could say map.of. What's really fun here, I don't know, it's fun to me, maybe I'm a nerd, but I can just specify the key and the value like this. And I can go on and on and have however many of these I want.

[00:10:03]
So lemon 20. I mean if I really wanted to be wild, I could even put all of this on one line like that. [LAUGH] I don't know maybe cuz I grew up doing maps like this. This is just amazing to me that I could do a map on one line.

[00:10:23]
Definitely don't recommend it except for fun. But it will basically take two things, this is one entry, this is one entry. I'll put it back so no one says Angie Jones taught them to do one line maps.
>> [LAUGH]
>> All right, so for readability, you would want it like this, right?

[00:10:41]
Each one on the line, but of course this is immutable, meaning, again you cannot then modify this map.
>> Can you look for the value? Same way like on
>> Like how we looked for the key?
>> No, like in line 82
>> Okay.
>> Can you say instead of say banana and say the value?

[00:11:02]
>> The value?
>> Yeah.
>> No, so when you do get like that, you have to get it by the key, cuz remember, the values don't have to be unique. So by giving it a value, that would basically be the opposite direction where you're thinking that the value associates with some unique key and that's not true.

[00:11:23]
>> So if you did wanna do that you'd have to probably loop through the entire HashMap?
>> Yeah, if I wanna say buy me all the objects that have this value or something like that, I can do that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now