Using streams for a null-safe conversion from an array to listConverting Array to ListCreate ArrayList from arrayHow to simplify a null-safe compareTo() implementation?Converting array to list in JavaConvert list to array in JavaRetrieving a List from a java.util.stream.Stream in Java 8How to Convert a Java 8 Stream to an Array?Java 8 Stream and operation on arraysHow can I create a stream from an array?How to sum a list of integers with java streams?Java 8 stream - how to properly make NPE-safe stream

How long to clear the 'suck zone' of a turbofan after start is initiated?

How to safely derail a train during transit?

How can I quit an app using Terminal?

Inappropriate reference requests from Journal reviewers

Number of words that can be made using all the letters of the word W, if Os as well as Is are separated is?

Is there a problem with hiding "forgot password" until it's needed?

How do we know the LHC results are robust?

Trouble understanding the speech of overseas colleagues

Does The Brexit Deal Have To Be Agreed By Both Houses?

Is the destination of a commercial flight important for the pilot?

How to check is there any negative term in a large list?

How to Reset Passwords on Multiple Websites Easily?

What is the best translation for "slot" in the context of multiplayer video games?

What grammatical function is や performing here?

Invade the Pyramid if you Dare

Why escape if the_content isnt?

Rotate a column

Unreliable Magic - Is it worth it?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

Failed to fetch jessie backports repository

Was Spock the First Vulcan in Starfleet?

What is the intuitive meaning of having a linear relationship between the logs of two variables?

Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?

Tiptoe or tiphoof? Adjusting words to better fit fantasy races



Using streams for a null-safe conversion from an array to list


Converting Array to ListCreate ArrayList from arrayHow to simplify a null-safe compareTo() implementation?Converting array to list in JavaConvert list to array in JavaRetrieving a List from a java.util.stream.Stream in Java 8How to Convert a Java 8 Stream to an Array?Java 8 Stream and operation on arraysHow can I create a stream from an array?How to sum a list of integers with java streams?Java 8 stream - how to properly make NPE-safe stream













9















I'm looking for a succinct way to rewrite a piece of Java code so that it uses streams to convert an array to a list in a null-safe fashion. Here's the original code:



public MailObject toMailObject(final String[] ccAddresses) 
final MailObject mailObject = new MailObject();

// line of code to be altered
mailObject.setCcAddresses(ccAddresses == null
? Collections.emptyList() : Arrays.asList(ccAddresses));

// other necessary code

return mailObject;



I've thought of doing something like this:



// psuedocode that obviously doesn't compile
Optional.ofNullable(ccAddresses).SOMETHING.orElse(Collections.emptyList());


where SOMETHING would be along the lines of:



Arrays.stream(ints).collect(Collectors.toList());


but I can't seem to get the syntax quite right.



This question was helpful but didn't exactly address my issue. Could anyone point me in the right direction? I feel like I'm close...



Thank you very much for your time.










share|improve this question



















  • 5





    Your original code seems succinct enough

    – GBlodgett
    Mar 21 at 1:35











  • Fair enough, but I'm curious as to the best way it can be done using streams.

    – risingTide
    Mar 21 at 1:36






  • 1





    .map(Arrays::asList)

    – shmosel
    Mar 21 at 2:05















9















I'm looking for a succinct way to rewrite a piece of Java code so that it uses streams to convert an array to a list in a null-safe fashion. Here's the original code:



public MailObject toMailObject(final String[] ccAddresses) 
final MailObject mailObject = new MailObject();

// line of code to be altered
mailObject.setCcAddresses(ccAddresses == null
? Collections.emptyList() : Arrays.asList(ccAddresses));

// other necessary code

return mailObject;



I've thought of doing something like this:



// psuedocode that obviously doesn't compile
Optional.ofNullable(ccAddresses).SOMETHING.orElse(Collections.emptyList());


where SOMETHING would be along the lines of:



Arrays.stream(ints).collect(Collectors.toList());


but I can't seem to get the syntax quite right.



This question was helpful but didn't exactly address my issue. Could anyone point me in the right direction? I feel like I'm close...



Thank you very much for your time.










share|improve this question



















  • 5





    Your original code seems succinct enough

    – GBlodgett
    Mar 21 at 1:35











  • Fair enough, but I'm curious as to the best way it can be done using streams.

    – risingTide
    Mar 21 at 1:36






  • 1





    .map(Arrays::asList)

    – shmosel
    Mar 21 at 2:05













9












9








9


1






I'm looking for a succinct way to rewrite a piece of Java code so that it uses streams to convert an array to a list in a null-safe fashion. Here's the original code:



public MailObject toMailObject(final String[] ccAddresses) 
final MailObject mailObject = new MailObject();

// line of code to be altered
mailObject.setCcAddresses(ccAddresses == null
? Collections.emptyList() : Arrays.asList(ccAddresses));

// other necessary code

return mailObject;



I've thought of doing something like this:



// psuedocode that obviously doesn't compile
Optional.ofNullable(ccAddresses).SOMETHING.orElse(Collections.emptyList());


where SOMETHING would be along the lines of:



Arrays.stream(ints).collect(Collectors.toList());


but I can't seem to get the syntax quite right.



This question was helpful but didn't exactly address my issue. Could anyone point me in the right direction? I feel like I'm close...



Thank you very much for your time.










share|improve this question
















I'm looking for a succinct way to rewrite a piece of Java code so that it uses streams to convert an array to a list in a null-safe fashion. Here's the original code:



public MailObject toMailObject(final String[] ccAddresses) 
final MailObject mailObject = new MailObject();

// line of code to be altered
mailObject.setCcAddresses(ccAddresses == null
? Collections.emptyList() : Arrays.asList(ccAddresses));

// other necessary code

return mailObject;



I've thought of doing something like this:



// psuedocode that obviously doesn't compile
Optional.ofNullable(ccAddresses).SOMETHING.orElse(Collections.emptyList());


where SOMETHING would be along the lines of:



Arrays.stream(ints).collect(Collectors.toList());


but I can't seem to get the syntax quite right.



This question was helpful but didn't exactly address my issue. Could anyone point me in the right direction? I feel like I'm close...



Thank you very much for your time.







java java-stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 3:01







risingTide

















asked Mar 21 at 1:31









risingTiderisingTide

62041238




62041238







  • 5





    Your original code seems succinct enough

    – GBlodgett
    Mar 21 at 1:35











  • Fair enough, but I'm curious as to the best way it can be done using streams.

    – risingTide
    Mar 21 at 1:36






  • 1





    .map(Arrays::asList)

    – shmosel
    Mar 21 at 2:05












  • 5





    Your original code seems succinct enough

    – GBlodgett
    Mar 21 at 1:35











  • Fair enough, but I'm curious as to the best way it can be done using streams.

    – risingTide
    Mar 21 at 1:36






  • 1





    .map(Arrays::asList)

    – shmosel
    Mar 21 at 2:05







5




5





Your original code seems succinct enough

– GBlodgett
Mar 21 at 1:35





Your original code seems succinct enough

– GBlodgett
Mar 21 at 1:35













Fair enough, but I'm curious as to the best way it can be done using streams.

– risingTide
Mar 21 at 1:36





Fair enough, but I'm curious as to the best way it can be done using streams.

– risingTide
Mar 21 at 1:36




1




1





.map(Arrays::asList)

– shmosel
Mar 21 at 2:05





.map(Arrays::asList)

– shmosel
Mar 21 at 2:05












2 Answers
2






active

oldest

votes


















6














In my opinion, the code that you have so far is perfectly readable. I think using a Stream for something like this will just complicate things. If you still want to use a Stream, then something like the following would work:



mailObject.setCcAddresses(Stream.ofNullable(ccAddresses)
.flatMap(Arrays::stream)
.collect(Collectors.toUnmodifiableList()));


As you can see, this is a more unreadable, and I wouldn't recommend it over your simple ternary expression.




Your Optional solution is slightly more readable and would look like the following:



mailObject.setCcAddresses(Optional.ofNullable(ccAddresses)
.map(Arrays::asList)
.orElse(Collections.emptyList()));





share|improve this answer




















  • 5





    Stream.ofNullable requires Java 9 and Collectors.toUnmodifiableList() even requires Java 10. And when you’re using Java 9 or newer, you can likewise change the original code to mailObject.setCcAddresses(ccAddresses == null? List.of(): List.of(ccAddresses));, whose simplicity will highlight the silliness of using Optional or Stream here even more.

    – Holger
    Mar 21 at 9:16







  • 1





    Good insight onto the Java versions that the first solution uses...and also into the List.of() possibility! Between this and the full answer by @Jacob G. there are a lot of options here; thanks!

    – risingTide
    Mar 21 at 18:18


















9














You might use the map :



List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())





share|improve this answer























  • I had wondered if the .map() method might be of use; thanks for the full usage in context.

    – risingTide
    Mar 21 at 18:23











  • @risingTide you're welcome. :D

    – Mạnh Quyết Nguyễn
    Mar 22 at 1:48










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%2f55272522%2fusing-streams-for-a-null-safe-conversion-from-an-array-to-list%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









6














In my opinion, the code that you have so far is perfectly readable. I think using a Stream for something like this will just complicate things. If you still want to use a Stream, then something like the following would work:



mailObject.setCcAddresses(Stream.ofNullable(ccAddresses)
.flatMap(Arrays::stream)
.collect(Collectors.toUnmodifiableList()));


As you can see, this is a more unreadable, and I wouldn't recommend it over your simple ternary expression.




Your Optional solution is slightly more readable and would look like the following:



mailObject.setCcAddresses(Optional.ofNullable(ccAddresses)
.map(Arrays::asList)
.orElse(Collections.emptyList()));





share|improve this answer




















  • 5





    Stream.ofNullable requires Java 9 and Collectors.toUnmodifiableList() even requires Java 10. And when you’re using Java 9 or newer, you can likewise change the original code to mailObject.setCcAddresses(ccAddresses == null? List.of(): List.of(ccAddresses));, whose simplicity will highlight the silliness of using Optional or Stream here even more.

    – Holger
    Mar 21 at 9:16







  • 1





    Good insight onto the Java versions that the first solution uses...and also into the List.of() possibility! Between this and the full answer by @Jacob G. there are a lot of options here; thanks!

    – risingTide
    Mar 21 at 18:18















6














In my opinion, the code that you have so far is perfectly readable. I think using a Stream for something like this will just complicate things. If you still want to use a Stream, then something like the following would work:



mailObject.setCcAddresses(Stream.ofNullable(ccAddresses)
.flatMap(Arrays::stream)
.collect(Collectors.toUnmodifiableList()));


As you can see, this is a more unreadable, and I wouldn't recommend it over your simple ternary expression.




Your Optional solution is slightly more readable and would look like the following:



mailObject.setCcAddresses(Optional.ofNullable(ccAddresses)
.map(Arrays::asList)
.orElse(Collections.emptyList()));





share|improve this answer




















  • 5





    Stream.ofNullable requires Java 9 and Collectors.toUnmodifiableList() even requires Java 10. And when you’re using Java 9 or newer, you can likewise change the original code to mailObject.setCcAddresses(ccAddresses == null? List.of(): List.of(ccAddresses));, whose simplicity will highlight the silliness of using Optional or Stream here even more.

    – Holger
    Mar 21 at 9:16







  • 1





    Good insight onto the Java versions that the first solution uses...and also into the List.of() possibility! Between this and the full answer by @Jacob G. there are a lot of options here; thanks!

    – risingTide
    Mar 21 at 18:18













6












6








6







In my opinion, the code that you have so far is perfectly readable. I think using a Stream for something like this will just complicate things. If you still want to use a Stream, then something like the following would work:



mailObject.setCcAddresses(Stream.ofNullable(ccAddresses)
.flatMap(Arrays::stream)
.collect(Collectors.toUnmodifiableList()));


As you can see, this is a more unreadable, and I wouldn't recommend it over your simple ternary expression.




Your Optional solution is slightly more readable and would look like the following:



mailObject.setCcAddresses(Optional.ofNullable(ccAddresses)
.map(Arrays::asList)
.orElse(Collections.emptyList()));





share|improve this answer















In my opinion, the code that you have so far is perfectly readable. I think using a Stream for something like this will just complicate things. If you still want to use a Stream, then something like the following would work:



mailObject.setCcAddresses(Stream.ofNullable(ccAddresses)
.flatMap(Arrays::stream)
.collect(Collectors.toUnmodifiableList()));


As you can see, this is a more unreadable, and I wouldn't recommend it over your simple ternary expression.




Your Optional solution is slightly more readable and would look like the following:



mailObject.setCcAddresses(Optional.ofNullable(ccAddresses)
.map(Arrays::asList)
.orElse(Collections.emptyList()));






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 2:05

























answered Mar 21 at 2:00









Jacob G.Jacob G.

16.4k52464




16.4k52464







  • 5





    Stream.ofNullable requires Java 9 and Collectors.toUnmodifiableList() even requires Java 10. And when you’re using Java 9 or newer, you can likewise change the original code to mailObject.setCcAddresses(ccAddresses == null? List.of(): List.of(ccAddresses));, whose simplicity will highlight the silliness of using Optional or Stream here even more.

    – Holger
    Mar 21 at 9:16







  • 1





    Good insight onto the Java versions that the first solution uses...and also into the List.of() possibility! Between this and the full answer by @Jacob G. there are a lot of options here; thanks!

    – risingTide
    Mar 21 at 18:18












  • 5





    Stream.ofNullable requires Java 9 and Collectors.toUnmodifiableList() even requires Java 10. And when you’re using Java 9 or newer, you can likewise change the original code to mailObject.setCcAddresses(ccAddresses == null? List.of(): List.of(ccAddresses));, whose simplicity will highlight the silliness of using Optional or Stream here even more.

    – Holger
    Mar 21 at 9:16







  • 1





    Good insight onto the Java versions that the first solution uses...and also into the List.of() possibility! Between this and the full answer by @Jacob G. there are a lot of options here; thanks!

    – risingTide
    Mar 21 at 18:18







5




5





Stream.ofNullable requires Java 9 and Collectors.toUnmodifiableList() even requires Java 10. And when you’re using Java 9 or newer, you can likewise change the original code to mailObject.setCcAddresses(ccAddresses == null? List.of(): List.of(ccAddresses));, whose simplicity will highlight the silliness of using Optional or Stream here even more.

– Holger
Mar 21 at 9:16






Stream.ofNullable requires Java 9 and Collectors.toUnmodifiableList() even requires Java 10. And when you’re using Java 9 or newer, you can likewise change the original code to mailObject.setCcAddresses(ccAddresses == null? List.of(): List.of(ccAddresses));, whose simplicity will highlight the silliness of using Optional or Stream here even more.

– Holger
Mar 21 at 9:16





1




1





Good insight onto the Java versions that the first solution uses...and also into the List.of() possibility! Between this and the full answer by @Jacob G. there are a lot of options here; thanks!

– risingTide
Mar 21 at 18:18





Good insight onto the Java versions that the first solution uses...and also into the List.of() possibility! Between this and the full answer by @Jacob G. there are a lot of options here; thanks!

– risingTide
Mar 21 at 18:18













9














You might use the map :



List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())





share|improve this answer























  • I had wondered if the .map() method might be of use; thanks for the full usage in context.

    – risingTide
    Mar 21 at 18:23











  • @risingTide you're welcome. :D

    – Mạnh Quyết Nguyễn
    Mar 22 at 1:48















9














You might use the map :



List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())





share|improve this answer























  • I had wondered if the .map() method might be of use; thanks for the full usage in context.

    – risingTide
    Mar 21 at 18:23











  • @risingTide you're welcome. :D

    – Mạnh Quyết Nguyễn
    Mar 22 at 1:48













9












9








9







You might use the map :



List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())





share|improve this answer













You might use the map :



List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 2:06









Mạnh Quyết NguyễnMạnh Quyết Nguyễn

11k1930




11k1930












  • I had wondered if the .map() method might be of use; thanks for the full usage in context.

    – risingTide
    Mar 21 at 18:23











  • @risingTide you're welcome. :D

    – Mạnh Quyết Nguyễn
    Mar 22 at 1:48

















  • I had wondered if the .map() method might be of use; thanks for the full usage in context.

    – risingTide
    Mar 21 at 18:23











  • @risingTide you're welcome. :D

    – Mạnh Quyết Nguyễn
    Mar 22 at 1:48
















I had wondered if the .map() method might be of use; thanks for the full usage in context.

– risingTide
Mar 21 at 18:23





I had wondered if the .map() method might be of use; thanks for the full usage in context.

– risingTide
Mar 21 at 18:23













@risingTide you're welcome. :D

– Mạnh Quyết Nguyễn
Mar 22 at 1:48





@risingTide you're welcome. :D

– Mạnh Quyết Nguyễn
Mar 22 at 1:48

















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%2f55272522%2fusing-streams-for-a-null-safe-conversion-from-an-array-to-list%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

He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

Bunad

Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum