How do I list, map and “print if count>0” with Java 8 / stream API?

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












19















Here's my code as per now.



List<Cat> cats = petStore.getCatsForSale();

if (!cats.empty)
logger.info("Processing for cats: " + cats.size());

for (Cat cat : cats)
cat.giveFood();



My colleague writes realy nice code using the Java stream API. I tried to rewrite it as one streaming statement, but I got stuck.



petStore.getCatsForSale().stream.forEach(cat -> cat.giveFood)
.countTheCats().thenDo(logger.info("Total number of cats: " + x)); // Incorrect... is this possible?


How can I do this? Ideally I want a single streaming statement...










share|improve this question



















  • 2





    What is count? Do you expect a log statement for every non empty cat?

    – Tim Biegeleisen
    Feb 5 at 8:26















19















Here's my code as per now.



List<Cat> cats = petStore.getCatsForSale();

if (!cats.empty)
logger.info("Processing for cats: " + cats.size());

for (Cat cat : cats)
cat.giveFood();



My colleague writes realy nice code using the Java stream API. I tried to rewrite it as one streaming statement, but I got stuck.



petStore.getCatsForSale().stream.forEach(cat -> cat.giveFood)
.countTheCats().thenDo(logger.info("Total number of cats: " + x)); // Incorrect... is this possible?


How can I do this? Ideally I want a single streaming statement...










share|improve this question



















  • 2





    What is count? Do you expect a log statement for every non empty cat?

    – Tim Biegeleisen
    Feb 5 at 8:26













19












19








19


1






Here's my code as per now.



List<Cat> cats = petStore.getCatsForSale();

if (!cats.empty)
logger.info("Processing for cats: " + cats.size());

for (Cat cat : cats)
cat.giveFood();



My colleague writes realy nice code using the Java stream API. I tried to rewrite it as one streaming statement, but I got stuck.



petStore.getCatsForSale().stream.forEach(cat -> cat.giveFood)
.countTheCats().thenDo(logger.info("Total number of cats: " + x)); // Incorrect... is this possible?


How can I do this? Ideally I want a single streaming statement...










share|improve this question
















Here's my code as per now.



List<Cat> cats = petStore.getCatsForSale();

if (!cats.empty)
logger.info("Processing for cats: " + cats.size());

for (Cat cat : cats)
cat.giveFood();



My colleague writes realy nice code using the Java stream API. I tried to rewrite it as one streaming statement, but I got stuck.



petStore.getCatsForSale().stream.forEach(cat -> cat.giveFood)
.countTheCats().thenDo(logger.info("Total number of cats: " + x)); // Incorrect... is this possible?


How can I do this? Ideally I want a single streaming statement...







java java-8 functional-programming java-stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 5 at 17:44









Peter Mortensen

13.7k1986112




13.7k1986112










asked Feb 5 at 8:21









vikingstevevikingsteve

25.8k1179116




25.8k1179116







  • 2





    What is count? Do you expect a log statement for every non empty cat?

    – Tim Biegeleisen
    Feb 5 at 8:26












  • 2





    What is count? Do you expect a log statement for every non empty cat?

    – Tim Biegeleisen
    Feb 5 at 8:26







2




2





What is count? Do you expect a log statement for every non empty cat?

– Tim Biegeleisen
Feb 5 at 8:26





What is count? Do you expect a log statement for every non empty cat?

– Tim Biegeleisen
Feb 5 at 8:26












4 Answers
4






active

oldest

votes


















11














I am not sure why you want to use streams as the current loop solutions works, but you may as well use a Stream<List<Cat>>:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.flatMap(cats ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
)
.forEach(Cat::giveFood);


Maybe an optimization:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.peek(cats -> logger.info("Processing for cats: " + cats.size()))
.flatMap(Collection::stream)
.forEach(Cat::giveFood);


Or use this other variant:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.mapToInt(cats ->
cats.forEach(Cat::giveFood);
return cats.size();
)
.findAny()
.ifPresent(count -> logger.info("Processing for cats: " + count));





share|improve this answer




















  • 3





    @CommonMan not really, its empty vs non-empty here. I doubt Optional would be useful here much.

    – nullpointer
    Feb 5 at 8:47






  • 3





    @nullpointer I don't really like any of the solutions here except the one from you, I think using Streams for everyday logic is just overkill and makes it extremly hard to understand the simplest of logic

    – Lino
    Feb 5 at 8:53






  • 1





    @Spara so that I have a "wrapper"-stream around the Cat-list (Stream<List<Cat>>) which allows me to operate on the whole list at once, but still inside the stream. Using cats.stream() though will give you a stream over all cats (Stream<Cat>)

    – Lino
    Feb 5 at 8:56






  • 3





    @Lino well, it already is! :P

    – nullpointer
    Feb 5 at 8:58






  • 3





    @vikingsteve Do not prefer fancy streams over readability.

    – ETO
    Feb 5 at 9:21


















18














Your current code is much better without a stream and can further be cut short to:



if (!cats.isEmpty()) 
logger.info("Processing for cats: " + cats.size());

cats.forEach(Cat::giveFood); // Assuming giveFood is a stateless operation





share|improve this answer

























  • Hi and thx for answer. Can this be done in a one liner? Of course I can write "if (!cats.isEmpty()) logger.info("...") but the purpose of this question is to write it with streams

    – vikingsteve
    Feb 5 at 8:43






  • 3





    In my opinion, is this the only acceptable solution. Don't do everything with Streams just because a tutorial told you so, just by looking at every answer here it's clear what the intentions of this one here is, but for every other? It's just a mess

    – Lino
    Feb 5 at 8:59


















4














cats.stream()
.peek(Cat::giveFood)
.findAny().ifPresent(cat -> logger.info("Processing for cats: " + cats.size()));





share|improve this answer




















  • 5





    Starting from java-9 you may leave your cats hungry ))). Execution of peek is not guaranteed anymore. Please read javadoc for more details.

    – ETO
    Feb 5 at 9:00






  • 1





    @ETO Please look at tags. It says java-8!

    – oleg.cherednik
    Feb 5 at 9:06







  • 3





    @oleg.cherednik I think ETO wanted to say that this solution may only work for java-8 which makes it not a favorable solution when one wants to migrate the version upwards :)

    – Lino
    Feb 5 at 9:11






  • 2





    @oleg.cherednik Yes, I know. Using peek for non-debugging purposes is considered harmful anyway.

    – ETO
    Feb 5 at 9:11






  • 3





    Moreover, starting from February 2019 there is no long-term support of java-8 for cemmercial users. So if you write java-8 code, consider avoiding potential migration bugs. One may not notice that java-9's peek is not working as it did before. Thus your code will have a hidden unobvious bug.

    – ETO
    Feb 5 at 9:17



















2














I agree with @Lino. This is another alternative based on his idea:



List<Cat> cats = petStore.getCatsForSale();

cats.stream().limit(1)
.flatMap(c ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
).forEach(Cat::giveFood);





share|improve this answer

























  • Why not use an IntStream.range(1)? No need to use cats.stream().limit(1) that way

    – Lino
    Feb 5 at 8:54






  • 1





    @Lino because of empty list

    – David Pérez Cabrera
    Feb 5 at 8:55










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%2f54530220%2fhow-do-i-list-map-and-print-if-count0-with-java-8-stream-api%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









11














I am not sure why you want to use streams as the current loop solutions works, but you may as well use a Stream<List<Cat>>:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.flatMap(cats ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
)
.forEach(Cat::giveFood);


Maybe an optimization:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.peek(cats -> logger.info("Processing for cats: " + cats.size()))
.flatMap(Collection::stream)
.forEach(Cat::giveFood);


Or use this other variant:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.mapToInt(cats ->
cats.forEach(Cat::giveFood);
return cats.size();
)
.findAny()
.ifPresent(count -> logger.info("Processing for cats: " + count));





share|improve this answer




















  • 3





    @CommonMan not really, its empty vs non-empty here. I doubt Optional would be useful here much.

    – nullpointer
    Feb 5 at 8:47






  • 3





    @nullpointer I don't really like any of the solutions here except the one from you, I think using Streams for everyday logic is just overkill and makes it extremly hard to understand the simplest of logic

    – Lino
    Feb 5 at 8:53






  • 1





    @Spara so that I have a "wrapper"-stream around the Cat-list (Stream<List<Cat>>) which allows me to operate on the whole list at once, but still inside the stream. Using cats.stream() though will give you a stream over all cats (Stream<Cat>)

    – Lino
    Feb 5 at 8:56






  • 3





    @Lino well, it already is! :P

    – nullpointer
    Feb 5 at 8:58






  • 3





    @vikingsteve Do not prefer fancy streams over readability.

    – ETO
    Feb 5 at 9:21















11














I am not sure why you want to use streams as the current loop solutions works, but you may as well use a Stream<List<Cat>>:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.flatMap(cats ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
)
.forEach(Cat::giveFood);


Maybe an optimization:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.peek(cats -> logger.info("Processing for cats: " + cats.size()))
.flatMap(Collection::stream)
.forEach(Cat::giveFood);


Or use this other variant:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.mapToInt(cats ->
cats.forEach(Cat::giveFood);
return cats.size();
)
.findAny()
.ifPresent(count -> logger.info("Processing for cats: " + count));





share|improve this answer




















  • 3





    @CommonMan not really, its empty vs non-empty here. I doubt Optional would be useful here much.

    – nullpointer
    Feb 5 at 8:47






  • 3





    @nullpointer I don't really like any of the solutions here except the one from you, I think using Streams for everyday logic is just overkill and makes it extremly hard to understand the simplest of logic

    – Lino
    Feb 5 at 8:53






  • 1





    @Spara so that I have a "wrapper"-stream around the Cat-list (Stream<List<Cat>>) which allows me to operate on the whole list at once, but still inside the stream. Using cats.stream() though will give you a stream over all cats (Stream<Cat>)

    – Lino
    Feb 5 at 8:56






  • 3





    @Lino well, it already is! :P

    – nullpointer
    Feb 5 at 8:58






  • 3





    @vikingsteve Do not prefer fancy streams over readability.

    – ETO
    Feb 5 at 9:21













11












11








11







I am not sure why you want to use streams as the current loop solutions works, but you may as well use a Stream<List<Cat>>:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.flatMap(cats ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
)
.forEach(Cat::giveFood);


Maybe an optimization:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.peek(cats -> logger.info("Processing for cats: " + cats.size()))
.flatMap(Collection::stream)
.forEach(Cat::giveFood);


Or use this other variant:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.mapToInt(cats ->
cats.forEach(Cat::giveFood);
return cats.size();
)
.findAny()
.ifPresent(count -> logger.info("Processing for cats: " + count));





share|improve this answer















I am not sure why you want to use streams as the current loop solutions works, but you may as well use a Stream<List<Cat>>:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.flatMap(cats ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
)
.forEach(Cat::giveFood);


Maybe an optimization:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.peek(cats -> logger.info("Processing for cats: " + cats.size()))
.flatMap(Collection::stream)
.forEach(Cat::giveFood);


Or use this other variant:



Stream.of(petStore.getCatsForSale())
.filter(cats -> !cats.isEmpty())
.mapToInt(cats ->
cats.forEach(Cat::giveFood);
return cats.size();
)
.findAny()
.ifPresent(count -> logger.info("Processing for cats: " + count));






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 5 at 17:46









Peter Mortensen

13.7k1986112




13.7k1986112










answered Feb 5 at 8:36









LinoLino

9,65722041




9,65722041







  • 3





    @CommonMan not really, its empty vs non-empty here. I doubt Optional would be useful here much.

    – nullpointer
    Feb 5 at 8:47






  • 3





    @nullpointer I don't really like any of the solutions here except the one from you, I think using Streams for everyday logic is just overkill and makes it extremly hard to understand the simplest of logic

    – Lino
    Feb 5 at 8:53






  • 1





    @Spara so that I have a "wrapper"-stream around the Cat-list (Stream<List<Cat>>) which allows me to operate on the whole list at once, but still inside the stream. Using cats.stream() though will give you a stream over all cats (Stream<Cat>)

    – Lino
    Feb 5 at 8:56






  • 3





    @Lino well, it already is! :P

    – nullpointer
    Feb 5 at 8:58






  • 3





    @vikingsteve Do not prefer fancy streams over readability.

    – ETO
    Feb 5 at 9:21












  • 3





    @CommonMan not really, its empty vs non-empty here. I doubt Optional would be useful here much.

    – nullpointer
    Feb 5 at 8:47






  • 3





    @nullpointer I don't really like any of the solutions here except the one from you, I think using Streams for everyday logic is just overkill and makes it extremly hard to understand the simplest of logic

    – Lino
    Feb 5 at 8:53






  • 1





    @Spara so that I have a "wrapper"-stream around the Cat-list (Stream<List<Cat>>) which allows me to operate on the whole list at once, but still inside the stream. Using cats.stream() though will give you a stream over all cats (Stream<Cat>)

    – Lino
    Feb 5 at 8:56






  • 3





    @Lino well, it already is! :P

    – nullpointer
    Feb 5 at 8:58






  • 3





    @vikingsteve Do not prefer fancy streams over readability.

    – ETO
    Feb 5 at 9:21







3




3





@CommonMan not really, its empty vs non-empty here. I doubt Optional would be useful here much.

– nullpointer
Feb 5 at 8:47





@CommonMan not really, its empty vs non-empty here. I doubt Optional would be useful here much.

– nullpointer
Feb 5 at 8:47




3




3





@nullpointer I don't really like any of the solutions here except the one from you, I think using Streams for everyday logic is just overkill and makes it extremly hard to understand the simplest of logic

– Lino
Feb 5 at 8:53





@nullpointer I don't really like any of the solutions here except the one from you, I think using Streams for everyday logic is just overkill and makes it extremly hard to understand the simplest of logic

– Lino
Feb 5 at 8:53




1




1





@Spara so that I have a "wrapper"-stream around the Cat-list (Stream<List<Cat>>) which allows me to operate on the whole list at once, but still inside the stream. Using cats.stream() though will give you a stream over all cats (Stream<Cat>)

– Lino
Feb 5 at 8:56





@Spara so that I have a "wrapper"-stream around the Cat-list (Stream<List<Cat>>) which allows me to operate on the whole list at once, but still inside the stream. Using cats.stream() though will give you a stream over all cats (Stream<Cat>)

– Lino
Feb 5 at 8:56




3




3





@Lino well, it already is! :P

– nullpointer
Feb 5 at 8:58





@Lino well, it already is! :P

– nullpointer
Feb 5 at 8:58




3




3





@vikingsteve Do not prefer fancy streams over readability.

– ETO
Feb 5 at 9:21





@vikingsteve Do not prefer fancy streams over readability.

– ETO
Feb 5 at 9:21













18














Your current code is much better without a stream and can further be cut short to:



if (!cats.isEmpty()) 
logger.info("Processing for cats: " + cats.size());

cats.forEach(Cat::giveFood); // Assuming giveFood is a stateless operation





share|improve this answer

























  • Hi and thx for answer. Can this be done in a one liner? Of course I can write "if (!cats.isEmpty()) logger.info("...") but the purpose of this question is to write it with streams

    – vikingsteve
    Feb 5 at 8:43






  • 3





    In my opinion, is this the only acceptable solution. Don't do everything with Streams just because a tutorial told you so, just by looking at every answer here it's clear what the intentions of this one here is, but for every other? It's just a mess

    – Lino
    Feb 5 at 8:59















18














Your current code is much better without a stream and can further be cut short to:



if (!cats.isEmpty()) 
logger.info("Processing for cats: " + cats.size());

cats.forEach(Cat::giveFood); // Assuming giveFood is a stateless operation





share|improve this answer

























  • Hi and thx for answer. Can this be done in a one liner? Of course I can write "if (!cats.isEmpty()) logger.info("...") but the purpose of this question is to write it with streams

    – vikingsteve
    Feb 5 at 8:43






  • 3





    In my opinion, is this the only acceptable solution. Don't do everything with Streams just because a tutorial told you so, just by looking at every answer here it's clear what the intentions of this one here is, but for every other? It's just a mess

    – Lino
    Feb 5 at 8:59













18












18








18







Your current code is much better without a stream and can further be cut short to:



if (!cats.isEmpty()) 
logger.info("Processing for cats: " + cats.size());

cats.forEach(Cat::giveFood); // Assuming giveFood is a stateless operation





share|improve this answer















Your current code is much better without a stream and can further be cut short to:



if (!cats.isEmpty()) 
logger.info("Processing for cats: " + cats.size());

cats.forEach(Cat::giveFood); // Assuming giveFood is a stateless operation






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 5 at 17:48









Peter Mortensen

13.7k1986112




13.7k1986112










answered Feb 5 at 8:36









nullpointernullpointer

43.2k10101200




43.2k10101200












  • Hi and thx for answer. Can this be done in a one liner? Of course I can write "if (!cats.isEmpty()) logger.info("...") but the purpose of this question is to write it with streams

    – vikingsteve
    Feb 5 at 8:43






  • 3





    In my opinion, is this the only acceptable solution. Don't do everything with Streams just because a tutorial told you so, just by looking at every answer here it's clear what the intentions of this one here is, but for every other? It's just a mess

    – Lino
    Feb 5 at 8:59

















  • Hi and thx for answer. Can this be done in a one liner? Of course I can write "if (!cats.isEmpty()) logger.info("...") but the purpose of this question is to write it with streams

    – vikingsteve
    Feb 5 at 8:43






  • 3





    In my opinion, is this the only acceptable solution. Don't do everything with Streams just because a tutorial told you so, just by looking at every answer here it's clear what the intentions of this one here is, but for every other? It's just a mess

    – Lino
    Feb 5 at 8:59
















Hi and thx for answer. Can this be done in a one liner? Of course I can write "if (!cats.isEmpty()) logger.info("...") but the purpose of this question is to write it with streams

– vikingsteve
Feb 5 at 8:43





Hi and thx for answer. Can this be done in a one liner? Of course I can write "if (!cats.isEmpty()) logger.info("...") but the purpose of this question is to write it with streams

– vikingsteve
Feb 5 at 8:43




3




3





In my opinion, is this the only acceptable solution. Don't do everything with Streams just because a tutorial told you so, just by looking at every answer here it's clear what the intentions of this one here is, but for every other? It's just a mess

– Lino
Feb 5 at 8:59





In my opinion, is this the only acceptable solution. Don't do everything with Streams just because a tutorial told you so, just by looking at every answer here it's clear what the intentions of this one here is, but for every other? It's just a mess

– Lino
Feb 5 at 8:59











4














cats.stream()
.peek(Cat::giveFood)
.findAny().ifPresent(cat -> logger.info("Processing for cats: " + cats.size()));





share|improve this answer




















  • 5





    Starting from java-9 you may leave your cats hungry ))). Execution of peek is not guaranteed anymore. Please read javadoc for more details.

    – ETO
    Feb 5 at 9:00






  • 1





    @ETO Please look at tags. It says java-8!

    – oleg.cherednik
    Feb 5 at 9:06







  • 3





    @oleg.cherednik I think ETO wanted to say that this solution may only work for java-8 which makes it not a favorable solution when one wants to migrate the version upwards :)

    – Lino
    Feb 5 at 9:11






  • 2





    @oleg.cherednik Yes, I know. Using peek for non-debugging purposes is considered harmful anyway.

    – ETO
    Feb 5 at 9:11






  • 3





    Moreover, starting from February 2019 there is no long-term support of java-8 for cemmercial users. So if you write java-8 code, consider avoiding potential migration bugs. One may not notice that java-9's peek is not working as it did before. Thus your code will have a hidden unobvious bug.

    – ETO
    Feb 5 at 9:17
















4














cats.stream()
.peek(Cat::giveFood)
.findAny().ifPresent(cat -> logger.info("Processing for cats: " + cats.size()));





share|improve this answer




















  • 5





    Starting from java-9 you may leave your cats hungry ))). Execution of peek is not guaranteed anymore. Please read javadoc for more details.

    – ETO
    Feb 5 at 9:00






  • 1





    @ETO Please look at tags. It says java-8!

    – oleg.cherednik
    Feb 5 at 9:06







  • 3





    @oleg.cherednik I think ETO wanted to say that this solution may only work for java-8 which makes it not a favorable solution when one wants to migrate the version upwards :)

    – Lino
    Feb 5 at 9:11






  • 2





    @oleg.cherednik Yes, I know. Using peek for non-debugging purposes is considered harmful anyway.

    – ETO
    Feb 5 at 9:11






  • 3





    Moreover, starting from February 2019 there is no long-term support of java-8 for cemmercial users. So if you write java-8 code, consider avoiding potential migration bugs. One may not notice that java-9's peek is not working as it did before. Thus your code will have a hidden unobvious bug.

    – ETO
    Feb 5 at 9:17














4












4








4







cats.stream()
.peek(Cat::giveFood)
.findAny().ifPresent(cat -> logger.info("Processing for cats: " + cats.size()));





share|improve this answer















cats.stream()
.peek(Cat::giveFood)
.findAny().ifPresent(cat -> logger.info("Processing for cats: " + cats.size()));






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 5 at 8:48

























answered Feb 5 at 8:38









oleg.cherednikoleg.cherednik

7,08821118




7,08821118







  • 5





    Starting from java-9 you may leave your cats hungry ))). Execution of peek is not guaranteed anymore. Please read javadoc for more details.

    – ETO
    Feb 5 at 9:00






  • 1





    @ETO Please look at tags. It says java-8!

    – oleg.cherednik
    Feb 5 at 9:06







  • 3





    @oleg.cherednik I think ETO wanted to say that this solution may only work for java-8 which makes it not a favorable solution when one wants to migrate the version upwards :)

    – Lino
    Feb 5 at 9:11






  • 2





    @oleg.cherednik Yes, I know. Using peek for non-debugging purposes is considered harmful anyway.

    – ETO
    Feb 5 at 9:11






  • 3





    Moreover, starting from February 2019 there is no long-term support of java-8 for cemmercial users. So if you write java-8 code, consider avoiding potential migration bugs. One may not notice that java-9's peek is not working as it did before. Thus your code will have a hidden unobvious bug.

    – ETO
    Feb 5 at 9:17













  • 5





    Starting from java-9 you may leave your cats hungry ))). Execution of peek is not guaranteed anymore. Please read javadoc for more details.

    – ETO
    Feb 5 at 9:00






  • 1





    @ETO Please look at tags. It says java-8!

    – oleg.cherednik
    Feb 5 at 9:06







  • 3





    @oleg.cherednik I think ETO wanted to say that this solution may only work for java-8 which makes it not a favorable solution when one wants to migrate the version upwards :)

    – Lino
    Feb 5 at 9:11






  • 2





    @oleg.cherednik Yes, I know. Using peek for non-debugging purposes is considered harmful anyway.

    – ETO
    Feb 5 at 9:11






  • 3





    Moreover, starting from February 2019 there is no long-term support of java-8 for cemmercial users. So if you write java-8 code, consider avoiding potential migration bugs. One may not notice that java-9's peek is not working as it did before. Thus your code will have a hidden unobvious bug.

    – ETO
    Feb 5 at 9:17








5




5





Starting from java-9 you may leave your cats hungry ))). Execution of peek is not guaranteed anymore. Please read javadoc for more details.

– ETO
Feb 5 at 9:00





Starting from java-9 you may leave your cats hungry ))). Execution of peek is not guaranteed anymore. Please read javadoc for more details.

– ETO
Feb 5 at 9:00




1




1





@ETO Please look at tags. It says java-8!

– oleg.cherednik
Feb 5 at 9:06






@ETO Please look at tags. It says java-8!

– oleg.cherednik
Feb 5 at 9:06





3




3





@oleg.cherednik I think ETO wanted to say that this solution may only work for java-8 which makes it not a favorable solution when one wants to migrate the version upwards :)

– Lino
Feb 5 at 9:11





@oleg.cherednik I think ETO wanted to say that this solution may only work for java-8 which makes it not a favorable solution when one wants to migrate the version upwards :)

– Lino
Feb 5 at 9:11




2




2





@oleg.cherednik Yes, I know. Using peek for non-debugging purposes is considered harmful anyway.

– ETO
Feb 5 at 9:11





@oleg.cherednik Yes, I know. Using peek for non-debugging purposes is considered harmful anyway.

– ETO
Feb 5 at 9:11




3




3





Moreover, starting from February 2019 there is no long-term support of java-8 for cemmercial users. So if you write java-8 code, consider avoiding potential migration bugs. One may not notice that java-9's peek is not working as it did before. Thus your code will have a hidden unobvious bug.

– ETO
Feb 5 at 9:17






Moreover, starting from February 2019 there is no long-term support of java-8 for cemmercial users. So if you write java-8 code, consider avoiding potential migration bugs. One may not notice that java-9's peek is not working as it did before. Thus your code will have a hidden unobvious bug.

– ETO
Feb 5 at 9:17












2














I agree with @Lino. This is another alternative based on his idea:



List<Cat> cats = petStore.getCatsForSale();

cats.stream().limit(1)
.flatMap(c ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
).forEach(Cat::giveFood);





share|improve this answer

























  • Why not use an IntStream.range(1)? No need to use cats.stream().limit(1) that way

    – Lino
    Feb 5 at 8:54






  • 1





    @Lino because of empty list

    – David Pérez Cabrera
    Feb 5 at 8:55















2














I agree with @Lino. This is another alternative based on his idea:



List<Cat> cats = petStore.getCatsForSale();

cats.stream().limit(1)
.flatMap(c ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
).forEach(Cat::giveFood);





share|improve this answer

























  • Why not use an IntStream.range(1)? No need to use cats.stream().limit(1) that way

    – Lino
    Feb 5 at 8:54






  • 1





    @Lino because of empty list

    – David Pérez Cabrera
    Feb 5 at 8:55













2












2








2







I agree with @Lino. This is another alternative based on his idea:



List<Cat> cats = petStore.getCatsForSale();

cats.stream().limit(1)
.flatMap(c ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
).forEach(Cat::giveFood);





share|improve this answer















I agree with @Lino. This is another alternative based on his idea:



List<Cat> cats = petStore.getCatsForSale();

cats.stream().limit(1)
.flatMap(c ->
logger.info("Processing for cats: " + cats.size());
return cats.stream();
).forEach(Cat::giveFood);






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 5 at 17:49









Peter Mortensen

13.7k1986112




13.7k1986112










answered Feb 5 at 8:48









David Pérez CabreraDavid Pérez Cabrera

4,08321331




4,08321331












  • Why not use an IntStream.range(1)? No need to use cats.stream().limit(1) that way

    – Lino
    Feb 5 at 8:54






  • 1





    @Lino because of empty list

    – David Pérez Cabrera
    Feb 5 at 8:55

















  • Why not use an IntStream.range(1)? No need to use cats.stream().limit(1) that way

    – Lino
    Feb 5 at 8:54






  • 1





    @Lino because of empty list

    – David Pérez Cabrera
    Feb 5 at 8:55
















Why not use an IntStream.range(1)? No need to use cats.stream().limit(1) that way

– Lino
Feb 5 at 8:54





Why not use an IntStream.range(1)? No need to use cats.stream().limit(1) that way

– Lino
Feb 5 at 8:54




1




1





@Lino because of empty list

– David Pérez Cabrera
Feb 5 at 8:55





@Lino because of empty list

– David Pérez Cabrera
Feb 5 at 8:55

















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54530220%2fhow-do-i-list-map-and-print-if-count0-with-java-8-stream-api%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?