Using streams for a null-safe conversion from an array to listConverting Array to ListCreate ArrayList from...
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
Should I warn new/prospective PhD Student that supervisor is terrible?
What is the meaning of the following sentence?
How to leave product feedback on macOS?
Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?
Ways of geometrical multiplication
How do you justify more code being written by following clean code practices?
When is "ei" a diphthong?
Why Shazam when there is already Superman?
Unable to disable Microsoft Store in domain environment
Showing mass murder in a kid's book
Do I have to know the General Relativity theory to understand the concept of inertial frame?
How to test the sharpness of a knife?
Do people actually use the word "kaputt" in conversation?
If the only attacker is removed from combat, is a creature still counted as having attacked this turn?
How to make money from a browser who sees 5 seconds into the future of any web page?
Why the "ls" command is showing the permissions of files in a FAT32 partition?
What is this high flying aircraft over Pennsylvania?
Why does the Persian emissary display a string of crowned skulls?
How do I Interface a PS/2 Keyboard without Modern Techniques?
Anime with legendary swords made from talismans and a man who could change them with a shattered body
Language involving irrational number is not a CFL
In One Punch Man, is King actually weak?
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
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
add a comment |
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
3
Your original code seems succinct enough
– GBlodgett
4 hours ago
Fair enough, but I'm curious as to the best way it can be done using streams.
– risingTide
4 hours ago
1
.map(Arrays::asList)
– shmosel
4 hours ago
add a comment |
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
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
java java-stream
edited 3 hours ago
risingTide
asked 4 hours ago
risingTiderisingTide
60341238
60341238
3
Your original code seems succinct enough
– GBlodgett
4 hours ago
Fair enough, but I'm curious as to the best way it can be done using streams.
– risingTide
4 hours ago
1
.map(Arrays::asList)
– shmosel
4 hours ago
add a comment |
3
Your original code seems succinct enough
– GBlodgett
4 hours ago
Fair enough, but I'm curious as to the best way it can be done using streams.
– risingTide
4 hours ago
1
.map(Arrays::asList)
– shmosel
4 hours ago
3
3
Your original code seems succinct enough
– GBlodgett
4 hours ago
Your original code seems succinct enough
– GBlodgett
4 hours ago
Fair enough, but I'm curious as to the best way it can be done using streams.
– risingTide
4 hours ago
Fair enough, but I'm curious as to the best way it can be done using streams.
– risingTide
4 hours ago
1
1
.map(Arrays::asList)
– shmosel
4 hours ago
.map(Arrays::asList)
– shmosel
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
You might use the map
:
List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())
2
@downvoter: Should let me know the reason :)
– Mạnh Quyết Nguyễn
4 hours ago
They don't . Because they are random clickers
– soorapadman
2 hours ago
add a comment |
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()));
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You might use the map
:
List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())
2
@downvoter: Should let me know the reason :)
– Mạnh Quyết Nguyễn
4 hours ago
They don't . Because they are random clickers
– soorapadman
2 hours ago
add a comment |
You might use the map
:
List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())
2
@downvoter: Should let me know the reason :)
– Mạnh Quyết Nguyễn
4 hours ago
They don't . Because they are random clickers
– soorapadman
2 hours ago
add a comment |
You might use the map
:
List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())
You might use the map
:
List<String> ccAddrs = Optional.ofNullable(ccAddress)
.map(Arrays::asList)
.orElse(Collections.emptyList())
answered 4 hours ago
Mạnh Quyết NguyễnMạnh Quyết Nguyễn
11k1930
11k1930
2
@downvoter: Should let me know the reason :)
– Mạnh Quyết Nguyễn
4 hours ago
They don't . Because they are random clickers
– soorapadman
2 hours ago
add a comment |
2
@downvoter: Should let me know the reason :)
– Mạnh Quyết Nguyễn
4 hours ago
They don't . Because they are random clickers
– soorapadman
2 hours ago
2
2
@downvoter: Should let me know the reason :)
– Mạnh Quyết Nguyễn
4 hours ago
@downvoter: Should let me know the reason :)
– Mạnh Quyết Nguyễn
4 hours ago
They don't . Because they are random clickers
– soorapadman
2 hours ago
They don't . Because they are random clickers
– soorapadman
2 hours ago
add a comment |
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()));
add a comment |
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()));
add a comment |
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()));
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()));
edited 4 hours ago
answered 4 hours ago
Jacob G.Jacob G.
16.3k52363
16.3k52363
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
Your original code seems succinct enough
– GBlodgett
4 hours ago
Fair enough, but I'm curious as to the best way it can be done using streams.
– risingTide
4 hours ago
1
.map(Arrays::asList)
– shmosel
4 hours ago