The new "foreach" statement and maps

The new "foreach" statement requires an object that implements the java.lang.Iterable<T> interface.
But why do not extend "for" for accepting objects that implements the Map interface?
Map<String,String> m = new TreeMap<String,String>();
for (String key, String value : m) {
     System.out.println (key + "=" + value);   
}The current alternative
Map<String,String> m = new TreeMap<String,String>();
for (Map.Entry<String,String> e : m.entrySet()) {
     System.out.println (e.getKey() + "=" + e.getValue());   
}is not so clean.

The new "foreach" statement requires an object that
implements the java.lang.Iterable<T> interface.Incidentally, I believe "foreach" also allows an object that is an array.
But why do not extend "for" for accepting objects that
implements the Map interface?I think that the use of "Iterable" is a very good idea. The interface contains just one method (iterator) and that method is inherently linked to the concept of "foreach". This means anyone can write their own class that implements Iterable, and their class can then be used with "foreach".
If we specified that "foreach" also worked with Map (with its umpteen methods, only one of which (entrySet) is relevant to "foreach"), this would prevent people writing their own classes which iterate through pairs of values (unless those classes happened to implement Map).
Ideally, there would be a new interface Pair<A,B> in java.lang (with getFirst() and getSecond() methods), and then if the compiler came across something like this:
    for (String str, Widget widget : items) { ... }where items is an instance of Iterable<Pair<String,Widget>>, then for each iteration, the "Pair" would be split into its component parts and stored in the variables "str" and "widget".
Unfortunately, it would be difficult to retrofit this concept to java.util.Map, since it would require Map to introduce a new method:
interface Map<K,V> extends Iterable<Pair<K,V>> {
    Iterator<Pair<K,V>> iterator();
}and Map.Entry would need to extend Pair, and therefore introduce two new methods:
interface Entry<K,V> extends Pair<K,V> {
    K getFirst();
    V getSecond();
}Both of these changes would break huge amounts of existing code that uses Map or Map.Entry.
Maybe one for Java 3 :)
Geoff

Similar Messages

  • When I load cds i get a message that states, "User not registered for online use." This problem has only begun since the new itunes update and is only a problem on my pc laptop.  Media content (track names, etc.) do not load.  Thanks

    When I load cds i get a message that states, "User not registered for online use." This problem has only begun since the new itunes update and is only a problem on my pc laptop.  Media content (track names, etc.) do not load.  Thanks

    See  Re: itunes 12 track names not found and user not registered for online use.
    tt2

  • Do you have the old for new service? Can I upgrade my iPhone 5s to the new iPhone 6 and how much does it cost?

    Do you have the old for new service? Can I upgrade my iPhone 5s to the new iPhone 6 and how much does it cost?

    Actually, in the US at least the website states
    You can recycle your iPhone at the Apple Retail Store and get immediate credit for its value toward the purchase of a new one.**
    from
    http://www.apple.com/retail/iphone/
    my 5S showed a top value of $305 allowance

  • HT1414 using Windows 7 OS, I had downloaded the new ioS 6 and started the update on the iPhone 4 and everything was going routine until the phone had been going thought what I though

    I attempted to update my iPhone 4 from ioS 5 to ioS 6 from iTunes on my laptop using Windows 7 OS, I had downloaded the new ioS 6 and started the update on the iPhone 4 and everything seemed to be going routine until the phone had been going thought what I though was the update process, when an error window opened up and it said that an error had occured and the iPhone 4 could not be updated. I have never had a problem like this before and it caught me off guard and I did not note all the information in the error window, but it did not appear to indicate anything other than that I would have to abort the update and did not think it would be that big of a problem. I tried to disconnect the phone and was going to do the update at a different time when I could investigate what the problem was. However I could not get iTunes to recognize the iPhone 4 after this and still cannot. After rebooting the laptop a few times and also trying to reset the phone a few times as well, I just have a dead phone. The iPhone 4 just has the image of a USB cable with an arrow pointing to an icon for iTunes. I have tried to get iTunes to recognize the iPhone, but cannot get it to. I am unable to do anything else with the phone. It seems like the phone is in a state where it does not have enough software loaded to do anything or I don’t know how to get it to. Any suggestions how I can get my iPhone 4 back into service. Note: iTunes still recognizes my iPad and iPods, so iTunes seems to be okay to me. Craig

    I had this problem on my iPod Touch 4g. Just press the home and sleep button. Wait and it will give an iTunes and cable logo. Now you can put in DFU mode (Hold Sleep and Home button 10 seconds and then release the power button kand keep pressing on the home button.) Now you will be able to restore it in iTunes.
    If you didn't sell it in al this time

  • The new MERGE statement in Oracle 9i

    Has anyone used the new Merge statement to process large amounts of data? Currently we use PL/SQL to update/insert into our tables when we are loading large amounts of data (close to one million rows) because we can set commit points and avoid rollback problems. I am wondering if we use Merge instead how this will affect rollback? Are we still going to have to code for this problem?
    Thanks in advance!

    Thanks for the suggestions. Our problem is that the base table contains 50 million rows and seven indexes, and each month we try to insert/update one million rows. Some of the data in the base table is historical so if we implemented your solution we would lose any records not being updated.
    What I am really trying to determine is if the MERGE statement has any redo log ramifications. Will we run in rollback space problems if we implement it instead of running PL/SQL in the following format:
    FOR cur_rec in c1 LOOP
    UPDATE table a
    SET col a = cur_rec.col a, ...
    WHERE ...
    IF SQL%NOTFOUND THEN
    INSERT (col a , col b, col c...)
    VALUES (cur_rec.col a, cur_rec.col b...);
    END IF;
    We commit every 10,000 records (as determined by SQL%ROWCOUNT). This can be time comsuming and Oracle claims that the new MERGE command will avoid costly overhead and reduce data scans. However, I am concerned that we may hit rollback problems if we implement a straight SQL statement such as MERGE. Any thoughts?

  • [svn:osmf:] 13028: Fix bug where NetStream would continue playback upon completion ( due to our not handling the new STOPPED state).

    Revision: 13028
    Revision: 13028
    Author:   [email protected]
    Date:     2009-12-16 18:10:28 -0800 (Wed, 16 Dec 2009)
    Log Message:
    Fix bug where NetStream would continue playback upon completion (due to our not handling the new STOPPED state).  Injection from trait refactoring.
    Modified Paths:
        osmf/trunk/framework/MediaFramework/org/osmf/net/NetStreamPlayTrait.as

    My daughter has had her Razr for about 9 months now.  About two weeks ago she picked up her phone in the morning on her way to school when she noticed two cracks, both starting at the camera lens. One goes completely to the bottom and the other goes sharply to the side. She has never dropped it and me and my husband went over it with a fine tooth comb. We looked under a magnifying glass and could no find any reason for the glass to crack. Not one ding, scratch or bang. Our daughter really takes good care of her stuff, but we still wanted to make sure before we sent it in for repairs. Well we did and we got a reply from Motorola with a picture of the cracks saying this was customer abuse and that it is not covered under warranty. Even though they did not find any physical damage to back it up. Well I e-mailed them back and told them I did a little research and found pages of people having the same problems. Well I did not hear from them until I received a notice from Fed Ex that they were sending the phone back. NOT FIXED!!! I went to look up why and guess what there is no case open any more for the phone. It has been wiped clean. I put in the RMA # it comes back not found, I put in the ID #, the SN# and all comes back not found. Yet a day earlier all the info was there. I know there is a lot more people like me and all of you, but they just don't want to be bothered so they pay to have it fix, just to have it do it again. Unless they have found the problem and only fixing it on a customer pay only set up. I am furious and will not be recommending this phone to anyone. And to think I was considering this phone for my next up grade! NOT!!!!

  • I've just updated my iPhone to the new iPhone OS and now it won't work and iTunes won't recognise that it's there.  I've uninstalled iTunes and reinstalled twice and it's not working.  Please help.

    I've just updated my iPhone to the new iPhone OS and now it won't work and iTunes won't recognise that it's there.  I've uninstalled iTunes and reinstalled twice and it's not working.  Please help.

    I have not had wifi since November when I upgraded to the newest IOS version.  I called the Apple help line and they told me some things to do and I tried them all and they worked for a few hours and then wifi shut off again.  I kept doing what they told me and it worked for a couple more days and then I could never turn it on again.  My blue tooth doesn't work either.  I had to actually increase my data usage with AT&T becuase of this.  When I did that in December I went to the Apple store and was told that it is a hardware issue and since my phone is out of warranty I would have to pay for a new phone because they could not fix the issue.  My upgrade isn't up until June 2014 so not only do I have to wait for the upgrade and I am not paying for a new phone but I have to pay $10 plus extra a month on my cell phone bill because I was going over on my Data!!!  I have been beyond upset about this and Apple is not doing anything about this nor did they offer up some way to resolve the issue because obviously it is not just some rare incident if several people are having this issue.  Who knows how many people out there have had this issue and have not said anything about it.  Now there is a newer update that I can't do because it requires wifi in order to download.
    June can not get here fast enough and I may not go back to Iphone after dealing with this for so long with no help from Apple.

  • I uploded the new itunes software and now my ipod touch is not getting reconized in my itunes. I went through all the trouble shooting support from apple. But I do have a avast antivirus on my computer i wonder if that is what is holding it up

    I uploded the new itunes software and now my ipod touch is not getting reconized in my itunes. I went through all the trouble shooting support from apple. But I do have a avast antivirus on my computer i wonder if that is what is holding it up

    Could be since it (security software) is listed as #5 in the following:
    iPhone, iPad, or iPod touch: Device not recognized in iTunes for Windows

  • When streaming a movie with the new Apple TV, and using a DSL Internet connection, all audio sound works, e.g., the music track, but the audio track with the actor's voices, does not work. What can I do?

    When streaming a movie with the new Apple TV, and using a DSL Internet connection, all audio sound works, e.g., the music track, but the audio track with the actor's voices, does not work. What can I do?

    Hi Brian,
    Thanks restoring and restarting didn't fix my problem - i have started it fresh and still it doesn't work. Basically I press ONCE on the remote, after the machine has not had any commands for 10 minutes, and the cursor skips from one end of the menu to the other - so I can't choose network or update software etc - I sometimes manage to stop it randomly in the middle of the menu if I press on a command in the middle of the cursor skipping all the menu steps...not sure what the problem is but I have basically not used my brand new apple tv since I bought it for that reason! I should call Apple support I suppose! grrrr hate wasting time with stuff like this!
    Thanks for your help though!
    Pernille

  • I am having a problem trying to get the new version of itunes to download correctly to my laptop. I have updated my ipad to the new ios 7, and windows is showing that it has been downloaded but when I connect my ipad, it is saying I have to download itune

    I really need HELP!!! I have a laptop that runs with windows vista, and I have been having issues trying to get it to download the new version of itunes that would be compatible to the new version of ios 7 that I have already updated on my ipad. I have downloaded this three times, it shows that the download is complete, but when I connect my ipad to my laptop it is still telling me that I have to download the new version again and it will not sync with my ipad until I do. Does anyone else have this issue and can anyone help me figure out how to correct it? I would be most grateful!!

    sign out from the creative cloud and then sign back in
    Sign in, Sign out | Creative Cloud desktop app

  • I have just bought a new iphone 4 as an upgrade to my iphone 3GS.  How can I transfer all my apps and data to the new iphone 4 and keep facetime etc on the new iphone 4.  If I do a restore of the iphone 3GS I only get the 3GS apps (i.e. no facetime)

    I have just bought a new iphone 4 as an upgrade to my iphone 3GS.  How can I transfer all my apps including saved levels in games etc. and data and folders to the new iphone 4 and keep facetime etc on the new iphone 4. When I have done this for someone else they got all their data etc but lost the Face Time App and teh iphone 4 looked like the iphone 3 screen.

    There is no FaceTime app for the iphone, FaceTime is built into the iOS. Follow the directions here:
    http://support.apple.com/kb/HT2109

  • I have the new iOS 7 and am getting texts from another family members phone and they are getting mine how do I disable this without changing accounts

    I have the new iOS 7 and am getting texts from another family members phone and they are getting mine how do I disable this without changing accounts

    Welcome to the Apple Community.
    You could simply set each device to only use a single telephone number (settings > messages > send & receive, but this isn't really an ideal solution since the other person can always change their settings and see your messages and send messages that appear as if they are from you. There is only one real solution and that's to have your own accounts.

  • I have the new iOS 7 and am getting texts from another family members phone and they are getting mine how to I disable this ??

    I have the new iOS 7 and am getting texts from another family members phone and they are getting mine how to I disable this ??

    You each need to have separate Apple IDs and separate user accounts. Better yet, separate computers as well.

  • Hi...i bought the new iphone 4 and would like to ask how can i transfer all my data from my old iphone to the new one?  If I will do "synchronization" through itunes with the old phone and the plug in the new one will that be the case?

    Hi...i bought the new iphone 4 and would like to ask how can i transfer all my data from my old iphone to the new one?  If I will do "synchronization" through itunes with the old phone and the plug in the new one will that be the case?

    Follow the instructions in this article to transfer your info: iPhone: Transferring information from your current iPhone to a new iPhone

  • TS3577 I want to erase the new Garage band and convert to my old one. As soon as it downloaded, I Lost all of my songs from the previous Sessions,HOW DO I GET THEM BACK?

    I want to erase the new Garage band and convert to my old one. As soon as it downloaded, I Lost all of my songs from the previous Sessions,HOW DO I GET THEM BACK?

    MacBook Pro, iOS 7.0.6
    Your signature lists both, Mac and iOS device - are you asking about GarageBand on your Mac or your iPhone/iPad?
    If you are asking about the upgrade from GarageBand '11 to GarageBand 10.0 on your Mac, the old GarageBand version should still be installed. The installer will have moved GarageBand '11 to a separate folder "GarageBand 6.0.5" in your Applications folder. Open this folder and click "GarageBand".  Does this version still see your old projects?  If not, type ".band" into the Spotlight searchfield in the upper right corner of your screen. Does that sfind your missing projects?
    If you are asking about GarageBand on iPad/iPhone, there it is not possible to restore the previous version after you upgraded. And I do not see, how this upgrade could have deleted your projects. Have they completely vanished, or can you not open them any longer? If your projects have vanished, restore your device from your backup: See:  iCloud: Backup and restore overview
    -- Léonie

Maybe you are looking for

  • Pop-up window for eBay

    I use dreamweaver to make some eBay adds for work, so I can just copy the code into eBay's web site. This also allows me to input more pictures and links in my advert. Is there a way to set a behaviour so a picture will open up in a new smaller pop u

  • Fields in Report

    Hello,     I need fields for following reports. Fields for Purchase orders Report. Fields  for Plant and Purchase Organization with all the vendors of the selected material. Fields for Customer Sales report, which gives complete information of sales

  • This window service has timer for update record once in a day at 5 pm.

    Hi Guys Please help me. Actully i create a window service. This window service has timer for update record once in a day at 5 pm. I have write a code but it is not working fin Here is my code. App.Config File:- <appSettings>       <add key="IsTrace"

  • How to load file.html in  JEditorPane

    Hello how to display HTML file including images in JEditorPane the file is on local system thanks

  • Extracting Multiple Table Data Dynamically..Table is an Input parameter

    Hi All Can any one update the Program/design of extracting multiple table data as a list or to an excel sheet For eg:- Mutliple Tables are entered in selection screen and upon executing the output should be to an excel sheet sequenctially according t