Java Fundamentals

Iterating with Enhanced For Loop

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

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

Angie demonstrates looping through an array using an enhanced for loop. Looping through a Map using an enhanced loop is also covered in this segment.

Preview
Close

Transcript from the "Iterating with Enhanced For Loop" Lesson

[00:00:00]
>> So remember we talked about the enhanced for loop when we discussed arrays. We use these for the array, but you can also use them to loop through collections, all right? So we're going to actually iterate through that list using the enhanced for loop. Okay, so going back to collections demo, I'm going to comment out set demo and uncomment out list demo.

[00:00:35]
Did I say list? Yes, all right, great. So inside of list, before we do all of this stuff, we'll comment all this out so we don't get confused. And let's go ahead and add some looping in here. So, again, a reminder that enhance for loop works like you put for and then inside of this you would need to define the variable that you want each object to be assigned to every time this loop runs, right?

[00:01:15]
So we say String fruit, and we would like to iterate over the list, okay? Now, we have an error here, and the reason that we have an error here is because we used a little bit of a shortcut when we defined our collection. And, when we did this Java, notice I used var somewhere, hold on.

[00:01:50]
I used var here under removed. And notice when I said that, what was returned back to me was a generic object, right? So when I made queue here, I didn't specify what type of data thet it holds, so it's is holds any type of object by default. It's best practice to be specific on the type of objects that your collection can hold and more specifically is required if you're gonna try to use this enhanced for loop.

[00:02:23]
So the way that we specify that hey, this is be a list of strings is in these angle brackets, you would write the object type. And notice that error on enhance for loop went away. The reason that error was there, again is a part of where I'm saying I have an object and I'm trying to store an object into a string variable, I can't do that, right?

[00:02:54]
It's a different type. Okay, so once you have defined what type of collection it is, what's the data it'll hold, you can use the enhance for loop and then it gets pretty straightforward. We could just say print out the fruit. And remember fruit, again, is each individual element within the collection.

[00:03:21]
So when we print this, boom, we see that each line Questions on enhanced for loop, yeah?
>> I got a question on that type.
>> Yeah.
>> So before you specified string for list, now wants us to at least put an empty angle brackets next to ArrayList.
>> He recommended that for you?

[00:03:53]
>> Yeah, I think that's right.
>> I don't have a problem. It's probably a suggestion. So you might have flagged it as a warning, all right? So, they want you to do this. Is that right? Where does it go? Before the thing, I mean, in my opinion, it's overkill but if you care about warnings, [LAUGH] which you should, then yeah, you could add it there as well.

[00:04:26]
I never do just because it's just too much it's too much and I don't need it, right? But it does help with just making sure everything is safe that's getting added by me adding that like that. Now if I tried to add a different type of fruit, I mean a different type of object.

[00:04:46]
Let's say, new rectangle for example. And let's import that. Come on. Then, I would get an error because I said that this is a string whereas I might not get an error before. It'll just let me add that rectangle because, I don't know, we just take any kind of object, polymorphism, right?

[00:05:14]
But, now that I specified a string, I can make sure that the only things that are being added are actual strings. All right, so I want us to look at the enhanced for loop for a map. Okay, so map is always gonna kinda be a little bit differently, worked a little bit differently.

[00:05:37]
We'll go ahead and comment out all this stuff from the main, I wanna say run the map. Okay, so close this, and we're in the map. Map demo method. All right, so in order to use the enhanced for loop, again you start with the for. Now, remember in the slides I said map dot entry is what you get from, iterating through these things, so that would be the type, Map.Entry.

[00:06:23]
And we'll call this calorieInfo, and then what you're iterating over is the fruitCalories.entrySet. Now notice here, I have a set that is the entry. I have a set that is just the keys and then I have a collection that is just the values. So, someone asked me earlier about getting the values and stuff.

[00:06:55]
You could do that with that values and that would give you a collection of just the values. You can also do just the keys. If you want the pair you would do entry set. Again, we have an error because what? We did not specify the type that this map holds, so we have a string and we have an integer, not it, right, because that's not gonna work, it is not an object.

[00:07:27]
So I have to use the wrapper class. Now, that's how I would use the enhanced for loop to get inside of a map, from here, it becomes pretty easy. I can just say calorieinfo.getKey. Plus, now I'll do a colon, And then calorieInfo.getValue. Okay, let's run map one. And boom, we see key and value printed, yeah?

[00:08:28]
>> You repeat why you have to use the integer wrapper class?
>> Yes, so collections can only hold objects, they can't hold primitive data types. So integer would be the wrapper class that represents an integer data type and it happens to be an object. So that's what you would need to use.

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