Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34370

Re: how does one explain this syntax?

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
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 <dqeiopFiq3jU1@mid.individual.net> (permalink)
References <nhrhd5$7r6$1@reader1.panix.com> <nhsjoi$hd4$1@dont-email.me>
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 <nhsjoi$hd4$1@dont-email.me>
Xref csiph.com comp.lang.java.programmer:34370

Show key headers only | View raw


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<Integer,String> 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<Integer, String> 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/

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

how does one explain this syntax? ruben safir <ruben@mrbrklyn.com> - 2016-05-22 01:51 -0400
  Re: how does one explain this syntax? Martin Gregorie <martin@address-in-sig.invalid> - 2016-05-22 10:05 +0000
  Re: how does one explain this syntax? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-22 07:21 -0400
    Re: how does one explain this syntax? Popping mad <rainbow@colition.gov> - 2016-05-22 20:21 +0000
      Re: how does one explain this syntax? Lars Enderin <lars.enderin@telia.com> - 2016-05-22 22:32 +0200
      Re: how does one explain this syntax? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-22 17:32 -0400
      Re: how does one explain this syntax? Lew <lewbloch@gmail.com> - 2016-05-23 14:36 -0700
    Re: how does one explain this syntax? Lew <lewbloch@gmail.com> - 2016-05-23 14:34 -0700
  Re: how does one explain this syntax? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-05-22 08:41 -0700
    Re: how does one explain this syntax? Robert Klemme <shortcutter@googlemail.com> - 2016-05-22 22:22 +0200
      Re: how does one explain this syntax? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-05-22 22:13 -0700
        Re: how does one explain this syntax? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-23 07:32 -0400
          Re: how does one explain this syntax? Robert Klemme <shortcutter@googlemail.com> - 2016-05-23 04:57 -0700
  Re: how does one explain this syntax? Popping mad <rainbow@colition.gov> - 2016-05-22 20:23 +0000
  Re: how does one explain this syntax? Roedy Green <inquiry@mindprod.com> - 2016-05-30 21:51 -0700
  Re: how does one explain this syntax? Roedy Green <inquiry@mindprod.com> - 2016-05-30 21:54 -0700
  Re: how does one explain this syntax? Roedy Green <inquiry@mindprod.com> - 2016-05-30 22:00 -0700
  Re: how does one explain this syntax? Roedy Green <inquiry@mindprod.com> - 2016-05-30 22:17 -0700

csiph-web