- Hey, I'm Cameron McKenzie @cameronmcnz on Twitter,
I wanted to talk to you about maps in Java,
more specifically, how you can put multiple values per key,
in a Java map, Which is something you really can't do
with a standard collections API.
So how do we fix this problem that Java
does not allow you to have multiple values per key?
Well I'll show you a couple of strategies.
We're just gonna create a new class called,
multiple values per key,
and it's gonna have a little main method.
We're just gonna write a little bit of code
and test some things out here.
I just wanna actually kind frame the problem,
and the problem is if you've got a hash map like this,
I might as well organize my imports,
cause this not gonna like that hash map.
There we go.
If you got a hash map like this,
you wanna keep track of some of your favorite halo cars.
How would you do that? Well, maybe, we've got the Ford GT,
which is one of my favorite halo cars of all time.
Of course, one of my other favorite halo cars is,
the Dodge Viper.
Everybody loves the Dodge Viper.
They're coming out with this Mustang Mach-E as well,
which is going to crap all over the Mustang Heritage,
by creating an SUV with the Mustang name.
But who knows, maybe that electrical SUV
with the Mustang name, will be a halo car as well.
And so all this code compiles, but I mean,
what happens if we actually try and gain access
to that Ford property, system.out.println halo cars,
Well, if I run this as a Java application,
you'll see that the Ford GT has disappeared,
and has been replaced by the Mustang Mach-E.
And I'm telling you nobody, but nobody,
is gonna be happy about that.
So how can you get around that?
I'm just gonna comment out that code,
Just so a source toggle comment on that.
Moving on, how can we get away...
how can we get around this limitation?
Well, I'll tell you what one clever trick is.
It's this right here.
What you do is you end up creating a map.
I'll do a organize imports there,
and import all of those things.
Am just gonna import everything from Java.util,
cause that's what I'm gonna be using.
So what you do is you create a map and of course,
maps don't have any special feature
where they can have multiple values per key.
But, genericize it.
And now we're gonna say that it's got a key
and that key is associated not with just one value,
but with multiple values.
So now you can have one key,
but you can throw multiple values in there.
And watch this.
I'm gonna create a new multi value map for Ford.
And then I'm gonna throw the Ford GT in there,
And then I'm gonna throw the Ford Mustang Mach-E in there,
And then I'm going to throw in the car
that killed Canada's greatest hero,
not Terry Fox, Tim Horton, right there, the Ford Pantera.
And if I wanted to, I could say system.out.println,
and if I went in and I tried to gain access
to that array list right there by using the key,
watch what happens if I run this code.
Boom, GT Mustang, Mach-E, and Pantera.
So now I've created a collection class,
that allows me to have multiple values per key.
I can have GT Mustang, Pantera.
Before, I could only have one.
Remember the Mustang Mach-E overwrote the GT.
Now that's one way of doing it.
But there's other approaches.
One cool approach is to use apache commons.
And I've got a little PALM file here
I've got this PALM file because, well,
I'm running a Maven project here inside of eclipse,
I've added this dependency on org.apache.commons
common collections, four specifically here.
I've got some guava here too,
but I'm going to deal with that later.
Okay, so what happens when I use this Apache Commons?
I'm gonna toggle out all of that code there,
toggle that and comment out that code,
- Let's see what happens when I try and use a little bit
of this, a gua... of this Apache commons code here.
I shouldn't even have mention guava.
So what we have is we have this thing called a MultiMap.
I'm going to have to organize my imports.
You can see it comes from that collections class.
There's actually a couple of different versions of this one.
This one's deprecated than the latest
edition of Apache commons, but you get the idea.
And what I can do here is I can throw in Mustang, Mach-E.
I don't have to create some crazy array.
I don't have to create a key mapping to an array with
multiple values in it. Like I did up here.
All I have to do is create this multi value map class,
and I can put multiple elements in it,
and watch what happens if I
try and print out Ford life is good.
In case when I run this code,
it prints out all of the values that I put in there.
I'm gonna comment that out and then I'm gonna jump over to
guava and you can see this is deprecated there.
So that's a little bit of a drag.
Now I do believe there's a class called the multi value map
in a patchy commons which isn't deprecated,
but a roll your own depending on
which version of Apache commons you're on.
But let's take a look at the MultiMap from guava.
Now watch this, there's a very subtle difference,
between this one and the previous one.
This MultiMap, it is pronounced with a lowercase M,
whereas the previous one, was pronounced with
an upper case M. Very subtle difference in my accent
when I say one in the other,
but notice we don't get the punitive message here,
saying that, that is deprecated. Here I can go over to Ford,
I can throw in that Mustang, Mach-E.
I can come over here. I can even put in the Pantera.
And then at any point in time in the future,
well, I can just get the information back there.
Look, I missed a semi-colon make sure you got to have that.
And I can just say hey, I want all of those values
that are associated with Ford,
bring it back as a collection that I've got to do an import
on that collection as well.
Bring all of those, all of those values associated with
the key Ford back to me.
And then I can just print it out,
system.out.println values,
and watch what happens here.
Run as a Java application and boom,
I've got the Mustang Maki and the Pantera coming back to me.
There you go.
Those are a couple of interesting ways that you can approach
the problem of Java nodding a lot,
not allowing you to have multiple values per key in a map.
And there you go.
That's how easy it is to put multiple values per key
in a Java map. Now, if you enjoyed that tutorial,
head over to the serverside.com
I'm the editor in chief over there,
and we've got lots of great tutorials on Java,
enterprise software development, spring DevOps, you name it.
Also. If you're interested in my personal antics,
you got to follow me on Twitter at cameronmcnz
and subscribe on YouTube.