Map.Entry

2 minutes
Share the link to this page
Copied
  Completed
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behaviour of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.

Transcript

Hello, in this Java programming video, we are going to look at the map dot entry feature. So we've looked at maps, pretty cool. And we can, you know, print out the map like so we can get an individual value from the map like so for a particular key. But how would you let's say loop over all of now the entire map, you'd use some sort of iterator. And this is where the entry aspect of it, you know, comes into play. So what we would want to do is basically to enter into a call in it equals and now what you want to do is get the name of the map, so map, dot entry set, and then do dot iterator and that will use this iterator object right here, to be able to loop over our entire Mac.

So to do that, while it next So while this, you know, still more values in the Mac, keep, you know, looping around. And what we're going to do, what we are going to do is going to create a map dot entry. And this is where the entry part of the source tutorial comes in. So you do map dot entry, because remember, it's not just a value or a key, it's both you might want to know both of them. So map dot entry. Like so, the data and I'm gonna call it just m equals, we're gonna cast the result of the next you know, return to a map entry map dot entry.

Like so and it got next And now we can just print them out. So what we can do is system dot print m dot get key. So we're going to get the key down. Dash and I will print it run through and get value. You run this out will print will comment out this one, so we don't get confused so that the entire map be printed out like so. And here, we've got it all printed out individually.

So if you want to loop over it, this is do it. So that's it for the map entry feature in Java. If you have any questions, feel free to reach out and I look forward to seeing you in the next Awesome job in video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.