Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: how does one explain this syntax? Date: Sun, 22 May 2016 22:22:47 +0200 Lines: 40 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Zh6Qe5KKicmTdyoF1aEcNAunWdNc+hreAgkVXW/hRDVULb7AA= Cancel-Lock: sha1:yZ8+pM7L2E7xisdWeA+bU05HBTg= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:34370 On 22.05.2016 17:41, Knute Johnson wrote: > It's not a modern example of how to iterate over a TreeMap but I suspect > that it was also to give an example of how to use Map.Entry. If you > just want to print out the entries of the map in order you would use > something similar to this now. > > import java.util.*; > > public class test { > public static void main(String... args) { > TreeMap tmap = new TreeMap<>(); > tmap.put(1,"Data1"); > tmap.put(23,"Data2"); > tmap.put(70,"Data3"); > tmap.put(4,"Data4"); > tmap.put(2,"Data5"); > > for (Integer key : tmap.keySet()) > System.out.printf("key is: %d & value is: %s\n", > key,tmap.get(key)); > } > } And why not do this instead? for (final Entry entry : tmap.entrySet()) System.out.printf("key is: %d & value is: %s\n", entry.getKey(), entry.getValue()); Note: tmap.get(key) is usually less efficient than fetching the value from an Entry. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/