Printing the letters from A to Z using a Java stream

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












15














I have this code, but it gives me an error:




Type mismatch: cannot convert from int to Character




Stream.iterate('a', i -> i + 1).limit(26).forEach(System.out::println);


Although it is fine to write int i = 'a';



I know I can write it like this, but that seems like too much code for a simple task.



Stream.iterate('a', i -> (char)(i + 1)).limit(26).forEach(System.out::println);


Why is the Java type inference failing?










share|improve this question



















  • 6




    Related stackoverflow.com/a/32424763/1746118
    – nullpointer
    Dec 28 '18 at 1:21















15














I have this code, but it gives me an error:




Type mismatch: cannot convert from int to Character




Stream.iterate('a', i -> i + 1).limit(26).forEach(System.out::println);


Although it is fine to write int i = 'a';



I know I can write it like this, but that seems like too much code for a simple task.



Stream.iterate('a', i -> (char)(i + 1)).limit(26).forEach(System.out::println);


Why is the Java type inference failing?










share|improve this question



















  • 6




    Related stackoverflow.com/a/32424763/1746118
    – nullpointer
    Dec 28 '18 at 1:21













15












15








15


3





I have this code, but it gives me an error:




Type mismatch: cannot convert from int to Character




Stream.iterate('a', i -> i + 1).limit(26).forEach(System.out::println);


Although it is fine to write int i = 'a';



I know I can write it like this, but that seems like too much code for a simple task.



Stream.iterate('a', i -> (char)(i + 1)).limit(26).forEach(System.out::println);


Why is the Java type inference failing?










share|improve this question















I have this code, but it gives me an error:




Type mismatch: cannot convert from int to Character




Stream.iterate('a', i -> i + 1).limit(26).forEach(System.out::println);


Although it is fine to write int i = 'a';



I know I can write it like this, but that seems like too much code for a simple task.



Stream.iterate('a', i -> (char)(i + 1)).limit(26).forEach(System.out::println);


Why is the Java type inference failing?







java java-8 char java-stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 10:55









Peter Mortensen

13.5k1983111




13.5k1983111










asked Dec 27 '18 at 21:41









fastcodejavafastcodejava

24k19109162




24k19109162







  • 6




    Related stackoverflow.com/a/32424763/1746118
    – nullpointer
    Dec 28 '18 at 1:21












  • 6




    Related stackoverflow.com/a/32424763/1746118
    – nullpointer
    Dec 28 '18 at 1:21







6




6




Related stackoverflow.com/a/32424763/1746118
– nullpointer
Dec 28 '18 at 1:21




Related stackoverflow.com/a/32424763/1746118
– nullpointer
Dec 28 '18 at 1:21












2 Answers
2






active

oldest

votes


















25














The reason why i -> i + 1 does not compile is because you're attempting to implicitly convert an int to a Character which the compiler cannot do itself alone.



In other words, you can think of Stream.iterate('a', i -> i + 1) as:



Stream.iterate('a', (Character i) -> 
int i1 = i + 1;
return i1; // not possible
);


As you have noted, explicitly casting to char solves it:



Stream.iterate('a', i -> (char)(i + 1))...


Btw this is better done as:



IntStream.rangeClosed('a', 'z').forEach(c -> System.out.println((char)c));


This is better because:



  1. No boxing overhead thus more efficient

  2. if you were to stop at say letter h with the use of iterate you'd have to do more brain processing than just entering h as the upper bound with rangeClosed because you'd need to find the number to truncate the infinite stream upon.

  3. Along with the boxing iterate generates an infinite stream which in this specific case has more overhead than the finite one with rangeClosed. Further, it's far easier to run IntStream.rangeClosed in parallel, not that you want to in this specific case but it's something to keep in mind. here is some discussion on Generators as sources by Brian Goetz.

etc...






share|improve this answer






















  • "iterate generates an infinite stream which again has more overhead than a finite one." why's that?
    – Alexander
    Dec 28 '18 at 6:11










  • @Alexander Firstly, I will improve my wording as it may not be the best to remove ambiguity. Thanks. I have also included a reference to a blog which might be of interest.
    – Aomine
    Dec 28 '18 at 7:53







  • 2




    "an infinite stream which in this specific case has more overhead than the finite one with" that's better :) Good callout on the split performance of this.
    – Alexander
    Dec 29 '18 at 17:39


















17














How about just:



Stream.iterate('a', i -> ++i).limit(26).forEach(System.out::println);



i -> i + 1 does not work because i is a Character and i + 1 causes an implicit narrowing conversion (JLS 5.1.3), which is not allowed. You can explicitly cast it as was shown. However ++i works because (From JLS 15.15.1):




Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.




The ++ operator takes care of the narrowing conversion without us having to explicitly cast it






share|improve this answer


















  • 1




    Good answer, would have been even better if you explain why ++i works and i + i doesn't.
    – fastcodejava
    Dec 28 '18 at 0:45






  • 1




    @fastcodejava I have edited my answer to try to explain.
    – GBlodgett
    Dec 28 '18 at 1:43






  • 3




    1 👏🏻 for awesome explanation @GBlodgett
    – Deadpool
    Dec 28 '18 at 1:50










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951142%2fprinting-the-letters-from-a-to-z-using-a-java-stream%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









25














The reason why i -> i + 1 does not compile is because you're attempting to implicitly convert an int to a Character which the compiler cannot do itself alone.



In other words, you can think of Stream.iterate('a', i -> i + 1) as:



Stream.iterate('a', (Character i) -> 
int i1 = i + 1;
return i1; // not possible
);


As you have noted, explicitly casting to char solves it:



Stream.iterate('a', i -> (char)(i + 1))...


Btw this is better done as:



IntStream.rangeClosed('a', 'z').forEach(c -> System.out.println((char)c));


This is better because:



  1. No boxing overhead thus more efficient

  2. if you were to stop at say letter h with the use of iterate you'd have to do more brain processing than just entering h as the upper bound with rangeClosed because you'd need to find the number to truncate the infinite stream upon.

  3. Along with the boxing iterate generates an infinite stream which in this specific case has more overhead than the finite one with rangeClosed. Further, it's far easier to run IntStream.rangeClosed in parallel, not that you want to in this specific case but it's something to keep in mind. here is some discussion on Generators as sources by Brian Goetz.

etc...






share|improve this answer






















  • "iterate generates an infinite stream which again has more overhead than a finite one." why's that?
    – Alexander
    Dec 28 '18 at 6:11










  • @Alexander Firstly, I will improve my wording as it may not be the best to remove ambiguity. Thanks. I have also included a reference to a blog which might be of interest.
    – Aomine
    Dec 28 '18 at 7:53







  • 2




    "an infinite stream which in this specific case has more overhead than the finite one with" that's better :) Good callout on the split performance of this.
    – Alexander
    Dec 29 '18 at 17:39















25














The reason why i -> i + 1 does not compile is because you're attempting to implicitly convert an int to a Character which the compiler cannot do itself alone.



In other words, you can think of Stream.iterate('a', i -> i + 1) as:



Stream.iterate('a', (Character i) -> 
int i1 = i + 1;
return i1; // not possible
);


As you have noted, explicitly casting to char solves it:



Stream.iterate('a', i -> (char)(i + 1))...


Btw this is better done as:



IntStream.rangeClosed('a', 'z').forEach(c -> System.out.println((char)c));


This is better because:



  1. No boxing overhead thus more efficient

  2. if you were to stop at say letter h with the use of iterate you'd have to do more brain processing than just entering h as the upper bound with rangeClosed because you'd need to find the number to truncate the infinite stream upon.

  3. Along with the boxing iterate generates an infinite stream which in this specific case has more overhead than the finite one with rangeClosed. Further, it's far easier to run IntStream.rangeClosed in parallel, not that you want to in this specific case but it's something to keep in mind. here is some discussion on Generators as sources by Brian Goetz.

etc...






share|improve this answer






















  • "iterate generates an infinite stream which again has more overhead than a finite one." why's that?
    – Alexander
    Dec 28 '18 at 6:11










  • @Alexander Firstly, I will improve my wording as it may not be the best to remove ambiguity. Thanks. I have also included a reference to a blog which might be of interest.
    – Aomine
    Dec 28 '18 at 7:53







  • 2




    "an infinite stream which in this specific case has more overhead than the finite one with" that's better :) Good callout on the split performance of this.
    – Alexander
    Dec 29 '18 at 17:39













25












25








25






The reason why i -> i + 1 does not compile is because you're attempting to implicitly convert an int to a Character which the compiler cannot do itself alone.



In other words, you can think of Stream.iterate('a', i -> i + 1) as:



Stream.iterate('a', (Character i) -> 
int i1 = i + 1;
return i1; // not possible
);


As you have noted, explicitly casting to char solves it:



Stream.iterate('a', i -> (char)(i + 1))...


Btw this is better done as:



IntStream.rangeClosed('a', 'z').forEach(c -> System.out.println((char)c));


This is better because:



  1. No boxing overhead thus more efficient

  2. if you were to stop at say letter h with the use of iterate you'd have to do more brain processing than just entering h as the upper bound with rangeClosed because you'd need to find the number to truncate the infinite stream upon.

  3. Along with the boxing iterate generates an infinite stream which in this specific case has more overhead than the finite one with rangeClosed. Further, it's far easier to run IntStream.rangeClosed in parallel, not that you want to in this specific case but it's something to keep in mind. here is some discussion on Generators as sources by Brian Goetz.

etc...






share|improve this answer














The reason why i -> i + 1 does not compile is because you're attempting to implicitly convert an int to a Character which the compiler cannot do itself alone.



In other words, you can think of Stream.iterate('a', i -> i + 1) as:



Stream.iterate('a', (Character i) -> 
int i1 = i + 1;
return i1; // not possible
);


As you have noted, explicitly casting to char solves it:



Stream.iterate('a', i -> (char)(i + 1))...


Btw this is better done as:



IntStream.rangeClosed('a', 'z').forEach(c -> System.out.println((char)c));


This is better because:



  1. No boxing overhead thus more efficient

  2. if you were to stop at say letter h with the use of iterate you'd have to do more brain processing than just entering h as the upper bound with rangeClosed because you'd need to find the number to truncate the infinite stream upon.

  3. Along with the boxing iterate generates an infinite stream which in this specific case has more overhead than the finite one with rangeClosed. Further, it's far easier to run IntStream.rangeClosed in parallel, not that you want to in this specific case but it's something to keep in mind. here is some discussion on Generators as sources by Brian Goetz.

etc...







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 7:53

























answered Dec 27 '18 at 21:42









AomineAomine

41.6k74071




41.6k74071











  • "iterate generates an infinite stream which again has more overhead than a finite one." why's that?
    – Alexander
    Dec 28 '18 at 6:11










  • @Alexander Firstly, I will improve my wording as it may not be the best to remove ambiguity. Thanks. I have also included a reference to a blog which might be of interest.
    – Aomine
    Dec 28 '18 at 7:53







  • 2




    "an infinite stream which in this specific case has more overhead than the finite one with" that's better :) Good callout on the split performance of this.
    – Alexander
    Dec 29 '18 at 17:39
















  • "iterate generates an infinite stream which again has more overhead than a finite one." why's that?
    – Alexander
    Dec 28 '18 at 6:11










  • @Alexander Firstly, I will improve my wording as it may not be the best to remove ambiguity. Thanks. I have also included a reference to a blog which might be of interest.
    – Aomine
    Dec 28 '18 at 7:53







  • 2




    "an infinite stream which in this specific case has more overhead than the finite one with" that's better :) Good callout on the split performance of this.
    – Alexander
    Dec 29 '18 at 17:39















"iterate generates an infinite stream which again has more overhead than a finite one." why's that?
– Alexander
Dec 28 '18 at 6:11




"iterate generates an infinite stream which again has more overhead than a finite one." why's that?
– Alexander
Dec 28 '18 at 6:11












@Alexander Firstly, I will improve my wording as it may not be the best to remove ambiguity. Thanks. I have also included a reference to a blog which might be of interest.
– Aomine
Dec 28 '18 at 7:53





@Alexander Firstly, I will improve my wording as it may not be the best to remove ambiguity. Thanks. I have also included a reference to a blog which might be of interest.
– Aomine
Dec 28 '18 at 7:53





2




2




"an infinite stream which in this specific case has more overhead than the finite one with" that's better :) Good callout on the split performance of this.
– Alexander
Dec 29 '18 at 17:39




"an infinite stream which in this specific case has more overhead than the finite one with" that's better :) Good callout on the split performance of this.
– Alexander
Dec 29 '18 at 17:39













17














How about just:



Stream.iterate('a', i -> ++i).limit(26).forEach(System.out::println);



i -> i + 1 does not work because i is a Character and i + 1 causes an implicit narrowing conversion (JLS 5.1.3), which is not allowed. You can explicitly cast it as was shown. However ++i works because (From JLS 15.15.1):




Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.




The ++ operator takes care of the narrowing conversion without us having to explicitly cast it






share|improve this answer


















  • 1




    Good answer, would have been even better if you explain why ++i works and i + i doesn't.
    – fastcodejava
    Dec 28 '18 at 0:45






  • 1




    @fastcodejava I have edited my answer to try to explain.
    – GBlodgett
    Dec 28 '18 at 1:43






  • 3




    1 👏🏻 for awesome explanation @GBlodgett
    – Deadpool
    Dec 28 '18 at 1:50















17














How about just:



Stream.iterate('a', i -> ++i).limit(26).forEach(System.out::println);



i -> i + 1 does not work because i is a Character and i + 1 causes an implicit narrowing conversion (JLS 5.1.3), which is not allowed. You can explicitly cast it as was shown. However ++i works because (From JLS 15.15.1):




Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.




The ++ operator takes care of the narrowing conversion without us having to explicitly cast it






share|improve this answer


















  • 1




    Good answer, would have been even better if you explain why ++i works and i + i doesn't.
    – fastcodejava
    Dec 28 '18 at 0:45






  • 1




    @fastcodejava I have edited my answer to try to explain.
    – GBlodgett
    Dec 28 '18 at 1:43






  • 3




    1 👏🏻 for awesome explanation @GBlodgett
    – Deadpool
    Dec 28 '18 at 1:50













17












17








17






How about just:



Stream.iterate('a', i -> ++i).limit(26).forEach(System.out::println);



i -> i + 1 does not work because i is a Character and i + 1 causes an implicit narrowing conversion (JLS 5.1.3), which is not allowed. You can explicitly cast it as was shown. However ++i works because (From JLS 15.15.1):




Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.




The ++ operator takes care of the narrowing conversion without us having to explicitly cast it






share|improve this answer














How about just:



Stream.iterate('a', i -> ++i).limit(26).forEach(System.out::println);



i -> i + 1 does not work because i is a Character and i + 1 causes an implicit narrowing conversion (JLS 5.1.3), which is not allowed. You can explicitly cast it as was shown. However ++i works because (From JLS 15.15.1):




Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.




The ++ operator takes care of the narrowing conversion without us having to explicitly cast it







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 1:41

























answered Dec 27 '18 at 21:47









GBlodgettGBlodgett

9,42041633




9,42041633







  • 1




    Good answer, would have been even better if you explain why ++i works and i + i doesn't.
    – fastcodejava
    Dec 28 '18 at 0:45






  • 1




    @fastcodejava I have edited my answer to try to explain.
    – GBlodgett
    Dec 28 '18 at 1:43






  • 3




    1 👏🏻 for awesome explanation @GBlodgett
    – Deadpool
    Dec 28 '18 at 1:50












  • 1




    Good answer, would have been even better if you explain why ++i works and i + i doesn't.
    – fastcodejava
    Dec 28 '18 at 0:45






  • 1




    @fastcodejava I have edited my answer to try to explain.
    – GBlodgett
    Dec 28 '18 at 1:43






  • 3




    1 👏🏻 for awesome explanation @GBlodgett
    – Deadpool
    Dec 28 '18 at 1:50







1




1




Good answer, would have been even better if you explain why ++i works and i + i doesn't.
– fastcodejava
Dec 28 '18 at 0:45




Good answer, would have been even better if you explain why ++i works and i + i doesn't.
– fastcodejava
Dec 28 '18 at 0:45




1




1




@fastcodejava I have edited my answer to try to explain.
– GBlodgett
Dec 28 '18 at 1:43




@fastcodejava I have edited my answer to try to explain.
– GBlodgett
Dec 28 '18 at 1:43




3




3




1 👏🏻 for awesome explanation @GBlodgett
– Deadpool
Dec 28 '18 at 1:50




1 👏🏻 for awesome explanation @GBlodgett
– Deadpool
Dec 28 '18 at 1:50

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951142%2fprinting-the-letters-from-a-to-z-using-a-java-stream%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?