Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: how does one explain this syntax? Date: Sun, 22 May 2016 08:41:30 -0700 Organization: A noiseless patient Spider Lines: 101 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 22 May 2016 15:37:54 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="fc10d1b501433caa198830294d826923"; logging-data="17828"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/egcUkKJM0zH4YuluxhdKN" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 In-Reply-To: Cancel-Lock: sha1:NyauJOE/wPgq8Am+ZB73M0/tAqs= Xref: csiph.com comp.lang.java.programmer:34368 On 5/21/2016 22:51, ruben safir wrote: > This passes for a demonstration and teaching example for a TreeMap > > > import java.util.TreeMap; > import java.util.Set; > import java.util.Iterator; > import java.util.Map; > > public class Details { > > public static void main(String args[]) { > > /* This is how to declare TreeMap */ > TreeMap tmap = > new TreeMap(); < > /*Adding elements to TreeMap*/ > tmap.put(1, "Data1"); > tmap.put(23, "Data2"); > tmap.put(70, "Data3"); > tmap.put(4, "Data4"); > tmap.put(2, "Data5"); > > /* Display content using Iterator*/ > Set set = tmap.entrySet(); < constructor for another class? > Iterator iterator = set.iterator(); < while(iterator.hasNext()) { > Map.Entry mentry = (Map.Entry)iterator.next(); < is this? > System.out.print("key is: "+ mentry.getKey() + " & Value is: > "); < System.out.println(mentry.getValue()); > } > > } > } > 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)); } } C:\Users\Knute Johnson>java test key is: 1 & value is: Data1 key is: 2 & value is: Data5 key is: 4 & value is: Data4 key is: 23 & value is: Data2 key is: 70 & value is: Data3 Keeping a SortedMap can be expensive and if you only occasionally need it to be sorted you can sort the key set before you interrogate the Map. import java.util.*; public class test1 { public static void main(String... args) { Map tmap = new HashMap<>(); tmap.put(1,"Data1"); tmap.put(23,"Data2"); tmap.put(70,"Data3"); tmap.put(4,"Data4"); tmap.put(2,"Data5"); for (Integer key : new TreeSet(tmap.keySet())) System.out.printf("key is: %d & value is: %s\n", key,tmap.get(key)); } } C:\Users\Knute Johnson>java test1 key is: 1 & value is: Data1 key is: 2 & value is: Data5 key is: 4 & value is: Data4 key is: 23 & value is: Data2 key is: 70 & value is: Data3 -- Knute Johnson