I need to iterate through a TreeMap.

I won't be able to use another collection instead of a TreeMap, I am merely changing some code in someone else's program. Is it at all possible to do this?

I'm thinking:

while (Treemap.hasNext())

or something similar. Is this possible in Java?

Recommended Answers

All 4 Replies

I don't know. Have you checked the API for Treemap?

I think I have a solution:

// tm is a TreeMap with values in it
		
		 Set set = tm.entrySet();
			// Get an iterator
			Iterator i = set.iterator();
			// Display elements
			while(i.hasNext()) {

It will work, but don't forget that each element of the iterator will have one of these:
java.util.Map.Entry

Hey thanks! You've been really helpfull. Appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.