This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Using maps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using maps " Watch "Using maps " New topic

Using maps

james loveday

Joined: Mar 06, 2009
Posts: 1
Hi all,

I'm hoping that somebody could look at this code and perhaps give me some pointers or something to think about to improve the efficiency. Ok so I have a map that holds strings and student class objects, from that I get the name of the student and using the score variable test that against the range of conditions, increasing the grade int accordingly then printing out the grade and the integer grade total. Like I said this works well enough but does anybody have any pointers that they could give me to think about to improve the efficiency. Please no Iterators I don't want to use one.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43282
Welcome to JavaRanch

As you say, it works, and it looks nice code.

I presume you are using the Map elsewhere with get(name) and put(name, Student)?

Can you use the values() method of your Map to get a Collection<Student> and iterate through that with the for-each loop? It will probably only improve prformance slightly because hashing and finding values from a Map is usually a fast operation.

You are in fact using an Iterator; there is an Iterator which hides behind the for-each loop.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Using maps
It's not a secret anymore!