Method call with nullable and not nullable generic array












0















I would like to write a function in Kotlin which takes a String array and sums up the length of all items in the array. I thought about something like this:



fun sumItems(values: Array<String?>): Int {
var sum: Int = 0
values.forEach { if(it != null) sum += it.length }
return sum
}


This works great but unfortunately I can not call this method for Array<String> because I get a type mismatch error then. I also can not create a function sumItems(values: Array<String>): Int because it has the same JVM signature. I could cast my argument to Array<String?> but this is unsafe.



So is there any better way to do this in Kotlin?










share|improve this question























  • do you really need that function as is? isn't fun sumItems(values: Array<String>) : Int already enough? If not, then just use Array<out String?> as parameter type. Yoni Gibbs answer already shows this and also how that method can even be more simplified...

    – Roland
    Nov 20 '18 at 15:12








  • 1





    I would declare it as sumItems(values: Array<String>) and when I need to pass an Array<String?> I would filter it with filterNotNull.

    – m0skit0
    Nov 20 '18 at 15:15











  • I would take the same route as @m0skit0. That makes the code way more clearer and cleaner ;-)

    – Roland
    Nov 20 '18 at 15:17











  • However filterNotNull returns a list :(

    – m0skit0
    Nov 20 '18 at 15:18













  • The problem with calling filterNotNull is that you're creating a second collection in memory. And you're having to do the loop twice. In most cases this is probably negligible, but if working with a lot of data it's not the most efficient way.

    – Yoni Gibbs
    Nov 20 '18 at 15:19


















0















I would like to write a function in Kotlin which takes a String array and sums up the length of all items in the array. I thought about something like this:



fun sumItems(values: Array<String?>): Int {
var sum: Int = 0
values.forEach { if(it != null) sum += it.length }
return sum
}


This works great but unfortunately I can not call this method for Array<String> because I get a type mismatch error then. I also can not create a function sumItems(values: Array<String>): Int because it has the same JVM signature. I could cast my argument to Array<String?> but this is unsafe.



So is there any better way to do this in Kotlin?










share|improve this question























  • do you really need that function as is? isn't fun sumItems(values: Array<String>) : Int already enough? If not, then just use Array<out String?> as parameter type. Yoni Gibbs answer already shows this and also how that method can even be more simplified...

    – Roland
    Nov 20 '18 at 15:12








  • 1





    I would declare it as sumItems(values: Array<String>) and when I need to pass an Array<String?> I would filter it with filterNotNull.

    – m0skit0
    Nov 20 '18 at 15:15











  • I would take the same route as @m0skit0. That makes the code way more clearer and cleaner ;-)

    – Roland
    Nov 20 '18 at 15:17











  • However filterNotNull returns a list :(

    – m0skit0
    Nov 20 '18 at 15:18













  • The problem with calling filterNotNull is that you're creating a second collection in memory. And you're having to do the loop twice. In most cases this is probably negligible, but if working with a lot of data it's not the most efficient way.

    – Yoni Gibbs
    Nov 20 '18 at 15:19
















0












0








0








I would like to write a function in Kotlin which takes a String array and sums up the length of all items in the array. I thought about something like this:



fun sumItems(values: Array<String?>): Int {
var sum: Int = 0
values.forEach { if(it != null) sum += it.length }
return sum
}


This works great but unfortunately I can not call this method for Array<String> because I get a type mismatch error then. I also can not create a function sumItems(values: Array<String>): Int because it has the same JVM signature. I could cast my argument to Array<String?> but this is unsafe.



So is there any better way to do this in Kotlin?










share|improve this question














I would like to write a function in Kotlin which takes a String array and sums up the length of all items in the array. I thought about something like this:



fun sumItems(values: Array<String?>): Int {
var sum: Int = 0
values.forEach { if(it != null) sum += it.length }
return sum
}


This works great but unfortunately I can not call this method for Array<String> because I get a type mismatch error then. I also can not create a function sumItems(values: Array<String>): Int because it has the same JVM signature. I could cast my argument to Array<String?> but this is unsafe.



So is there any better way to do this in Kotlin?







generics kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 15:00









CilencoCilenco

2,6641148101




2,6641148101













  • do you really need that function as is? isn't fun sumItems(values: Array<String>) : Int already enough? If not, then just use Array<out String?> as parameter type. Yoni Gibbs answer already shows this and also how that method can even be more simplified...

    – Roland
    Nov 20 '18 at 15:12








  • 1





    I would declare it as sumItems(values: Array<String>) and when I need to pass an Array<String?> I would filter it with filterNotNull.

    – m0skit0
    Nov 20 '18 at 15:15











  • I would take the same route as @m0skit0. That makes the code way more clearer and cleaner ;-)

    – Roland
    Nov 20 '18 at 15:17











  • However filterNotNull returns a list :(

    – m0skit0
    Nov 20 '18 at 15:18













  • The problem with calling filterNotNull is that you're creating a second collection in memory. And you're having to do the loop twice. In most cases this is probably negligible, but if working with a lot of data it's not the most efficient way.

    – Yoni Gibbs
    Nov 20 '18 at 15:19





















  • do you really need that function as is? isn't fun sumItems(values: Array<String>) : Int already enough? If not, then just use Array<out String?> as parameter type. Yoni Gibbs answer already shows this and also how that method can even be more simplified...

    – Roland
    Nov 20 '18 at 15:12








  • 1





    I would declare it as sumItems(values: Array<String>) and when I need to pass an Array<String?> I would filter it with filterNotNull.

    – m0skit0
    Nov 20 '18 at 15:15











  • I would take the same route as @m0skit0. That makes the code way more clearer and cleaner ;-)

    – Roland
    Nov 20 '18 at 15:17











  • However filterNotNull returns a list :(

    – m0skit0
    Nov 20 '18 at 15:18













  • The problem with calling filterNotNull is that you're creating a second collection in memory. And you're having to do the loop twice. In most cases this is probably negligible, but if working with a lot of data it's not the most efficient way.

    – Yoni Gibbs
    Nov 20 '18 at 15:19



















do you really need that function as is? isn't fun sumItems(values: Array<String>) : Int already enough? If not, then just use Array<out String?> as parameter type. Yoni Gibbs answer already shows this and also how that method can even be more simplified...

– Roland
Nov 20 '18 at 15:12







do you really need that function as is? isn't fun sumItems(values: Array<String>) : Int already enough? If not, then just use Array<out String?> as parameter type. Yoni Gibbs answer already shows this and also how that method can even be more simplified...

– Roland
Nov 20 '18 at 15:12






1




1





I would declare it as sumItems(values: Array<String>) and when I need to pass an Array<String?> I would filter it with filterNotNull.

– m0skit0
Nov 20 '18 at 15:15





I would declare it as sumItems(values: Array<String>) and when I need to pass an Array<String?> I would filter it with filterNotNull.

– m0skit0
Nov 20 '18 at 15:15













I would take the same route as @m0skit0. That makes the code way more clearer and cleaner ;-)

– Roland
Nov 20 '18 at 15:17





I would take the same route as @m0skit0. That makes the code way more clearer and cleaner ;-)

– Roland
Nov 20 '18 at 15:17













However filterNotNull returns a list :(

– m0skit0
Nov 20 '18 at 15:18







However filterNotNull returns a list :(

– m0skit0
Nov 20 '18 at 15:18















The problem with calling filterNotNull is that you're creating a second collection in memory. And you're having to do the loop twice. In most cases this is probably negligible, but if working with a lot of data it's not the most efficient way.

– Yoni Gibbs
Nov 20 '18 at 15:19







The problem with calling filterNotNull is that you're creating a second collection in memory. And you're having to do the loop twice. In most cases this is probably negligible, but if working with a lot of data it's not the most efficient way.

– Yoni Gibbs
Nov 20 '18 at 15:19














2 Answers
2






active

oldest

votes


















2














Try this:



fun sumItems(values: Array<out String?>) = values.sumBy { it?.length ?: 0 }


You might want to make it an extension method instead:



fun Array<out String?>.sumItems() = sumBy { it?.length ?: 0 }


This will work for both Array<String> and Array<String?> because of the out modifier on the generic type. What this states is that the values parameter (or the receiver of the extension method) must be an array that produces nullable strings. Obviously Array<String?> produces nullable strings so that's valid to pass in. But Array<String> also produces nullable strings, because strings can always be cast to nullable strings. This is explained in more detail here.






share|improve this answer


























  • I think that's not what is being asked: "I can not call this method for Array<String> because I get a type mismatch error then".

    – m0skit0
    Nov 20 '18 at 15:11











  • You can call it for both Array<String> and Array<String?>. I'll update my answer with more details..

    – Yoni Gibbs
    Nov 20 '18 at 15:12











  • Oh you're correct, I'm not very used to use out/in modifiers, very nice answer!

    – m0skit0
    Nov 20 '18 at 15:20











  • while being the correct answer to solve the problem, I can only suggest to eliminate the null values as soon as possible so that the code is not filled with tons of null-safe calls ;-)

    – Roland
    Nov 20 '18 at 15:33











  • Why doesn't this work with lists as well? If I have fun foo(c: MutableCollection<out String>) I can call foo(mutableListOf<String>()) but not foo(mutableListOf<String?>()). Any notes to that as well?

    – Cilenco
    Nov 27 '18 at 13:40





















2














Even though Yoni Gibbs answer is correct, I would rather take another route here, i.e. working with a non-nullable type, e.g.:



fun sumItems(values: Array<String>) = values.sumBy { it.length }


And as also m0skit0 mentioned in the comment: if you really have null values in your list, filter them out before making the sum, e.g.:



val arrayWithNulls = arrayOf("hello", null, "world")
arrayWithNulls.filterNotNull()
.let(::sumItems)


Or better yet: just skip that method and do:



arrayWithNulls.filterNotNull()
.sumBy { it.length } // or .sumBy(String::length)


Why introducing a new function, if applying existing ones already suffices?



Try to eliminate the null values from your array early. Otherwise your code just gets more complex (adding lots of null-safe things) and that makes the code just less readable. That way you could also skip that filterNotNull.






share|improve this answer


























  • Just be aware of my comment on the question itself: calling filterNotNull creates a new list in memory. Most probably the overhead here is negligible, but if you're working with large data sets you might want to avoid this. You could call asSequence on the original array to get round this though.

    – Yoni Gibbs
    Nov 20 '18 at 15:24













  • I see no real reason to keep null values within an array... that performance impact is negligible and probably completely avoidable ;-) Even if we would work with large data sets (then I assume even more that there are no null values), I would rather filter it once and use non-nullable types everywhere else...

    – Roland
    Nov 20 '18 at 15:32











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%2f53395798%2fmethod-call-with-nullable-and-not-nullable-generic-array%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









2














Try this:



fun sumItems(values: Array<out String?>) = values.sumBy { it?.length ?: 0 }


You might want to make it an extension method instead:



fun Array<out String?>.sumItems() = sumBy { it?.length ?: 0 }


This will work for both Array<String> and Array<String?> because of the out modifier on the generic type. What this states is that the values parameter (or the receiver of the extension method) must be an array that produces nullable strings. Obviously Array<String?> produces nullable strings so that's valid to pass in. But Array<String> also produces nullable strings, because strings can always be cast to nullable strings. This is explained in more detail here.






share|improve this answer


























  • I think that's not what is being asked: "I can not call this method for Array<String> because I get a type mismatch error then".

    – m0skit0
    Nov 20 '18 at 15:11











  • You can call it for both Array<String> and Array<String?>. I'll update my answer with more details..

    – Yoni Gibbs
    Nov 20 '18 at 15:12











  • Oh you're correct, I'm not very used to use out/in modifiers, very nice answer!

    – m0skit0
    Nov 20 '18 at 15:20











  • while being the correct answer to solve the problem, I can only suggest to eliminate the null values as soon as possible so that the code is not filled with tons of null-safe calls ;-)

    – Roland
    Nov 20 '18 at 15:33











  • Why doesn't this work with lists as well? If I have fun foo(c: MutableCollection<out String>) I can call foo(mutableListOf<String>()) but not foo(mutableListOf<String?>()). Any notes to that as well?

    – Cilenco
    Nov 27 '18 at 13:40


















2














Try this:



fun sumItems(values: Array<out String?>) = values.sumBy { it?.length ?: 0 }


You might want to make it an extension method instead:



fun Array<out String?>.sumItems() = sumBy { it?.length ?: 0 }


This will work for both Array<String> and Array<String?> because of the out modifier on the generic type. What this states is that the values parameter (or the receiver of the extension method) must be an array that produces nullable strings. Obviously Array<String?> produces nullable strings so that's valid to pass in. But Array<String> also produces nullable strings, because strings can always be cast to nullable strings. This is explained in more detail here.






share|improve this answer


























  • I think that's not what is being asked: "I can not call this method for Array<String> because I get a type mismatch error then".

    – m0skit0
    Nov 20 '18 at 15:11











  • You can call it for both Array<String> and Array<String?>. I'll update my answer with more details..

    – Yoni Gibbs
    Nov 20 '18 at 15:12











  • Oh you're correct, I'm not very used to use out/in modifiers, very nice answer!

    – m0skit0
    Nov 20 '18 at 15:20











  • while being the correct answer to solve the problem, I can only suggest to eliminate the null values as soon as possible so that the code is not filled with tons of null-safe calls ;-)

    – Roland
    Nov 20 '18 at 15:33











  • Why doesn't this work with lists as well? If I have fun foo(c: MutableCollection<out String>) I can call foo(mutableListOf<String>()) but not foo(mutableListOf<String?>()). Any notes to that as well?

    – Cilenco
    Nov 27 '18 at 13:40
















2












2








2







Try this:



fun sumItems(values: Array<out String?>) = values.sumBy { it?.length ?: 0 }


You might want to make it an extension method instead:



fun Array<out String?>.sumItems() = sumBy { it?.length ?: 0 }


This will work for both Array<String> and Array<String?> because of the out modifier on the generic type. What this states is that the values parameter (or the receiver of the extension method) must be an array that produces nullable strings. Obviously Array<String?> produces nullable strings so that's valid to pass in. But Array<String> also produces nullable strings, because strings can always be cast to nullable strings. This is explained in more detail here.






share|improve this answer















Try this:



fun sumItems(values: Array<out String?>) = values.sumBy { it?.length ?: 0 }


You might want to make it an extension method instead:



fun Array<out String?>.sumItems() = sumBy { it?.length ?: 0 }


This will work for both Array<String> and Array<String?> because of the out modifier on the generic type. What this states is that the values parameter (or the receiver of the extension method) must be an array that produces nullable strings. Obviously Array<String?> produces nullable strings so that's valid to pass in. But Array<String> also produces nullable strings, because strings can always be cast to nullable strings. This is explained in more detail here.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 15:16

























answered Nov 20 '18 at 15:09









Yoni GibbsYoni Gibbs

1,203113




1,203113













  • I think that's not what is being asked: "I can not call this method for Array<String> because I get a type mismatch error then".

    – m0skit0
    Nov 20 '18 at 15:11











  • You can call it for both Array<String> and Array<String?>. I'll update my answer with more details..

    – Yoni Gibbs
    Nov 20 '18 at 15:12











  • Oh you're correct, I'm not very used to use out/in modifiers, very nice answer!

    – m0skit0
    Nov 20 '18 at 15:20











  • while being the correct answer to solve the problem, I can only suggest to eliminate the null values as soon as possible so that the code is not filled with tons of null-safe calls ;-)

    – Roland
    Nov 20 '18 at 15:33











  • Why doesn't this work with lists as well? If I have fun foo(c: MutableCollection<out String>) I can call foo(mutableListOf<String>()) but not foo(mutableListOf<String?>()). Any notes to that as well?

    – Cilenco
    Nov 27 '18 at 13:40





















  • I think that's not what is being asked: "I can not call this method for Array<String> because I get a type mismatch error then".

    – m0skit0
    Nov 20 '18 at 15:11











  • You can call it for both Array<String> and Array<String?>. I'll update my answer with more details..

    – Yoni Gibbs
    Nov 20 '18 at 15:12











  • Oh you're correct, I'm not very used to use out/in modifiers, very nice answer!

    – m0skit0
    Nov 20 '18 at 15:20











  • while being the correct answer to solve the problem, I can only suggest to eliminate the null values as soon as possible so that the code is not filled with tons of null-safe calls ;-)

    – Roland
    Nov 20 '18 at 15:33











  • Why doesn't this work with lists as well? If I have fun foo(c: MutableCollection<out String>) I can call foo(mutableListOf<String>()) but not foo(mutableListOf<String?>()). Any notes to that as well?

    – Cilenco
    Nov 27 '18 at 13:40



















I think that's not what is being asked: "I can not call this method for Array<String> because I get a type mismatch error then".

– m0skit0
Nov 20 '18 at 15:11





I think that's not what is being asked: "I can not call this method for Array<String> because I get a type mismatch error then".

– m0skit0
Nov 20 '18 at 15:11













You can call it for both Array<String> and Array<String?>. I'll update my answer with more details..

– Yoni Gibbs
Nov 20 '18 at 15:12





You can call it for both Array<String> and Array<String?>. I'll update my answer with more details..

– Yoni Gibbs
Nov 20 '18 at 15:12













Oh you're correct, I'm not very used to use out/in modifiers, very nice answer!

– m0skit0
Nov 20 '18 at 15:20





Oh you're correct, I'm not very used to use out/in modifiers, very nice answer!

– m0skit0
Nov 20 '18 at 15:20













while being the correct answer to solve the problem, I can only suggest to eliminate the null values as soon as possible so that the code is not filled with tons of null-safe calls ;-)

– Roland
Nov 20 '18 at 15:33





while being the correct answer to solve the problem, I can only suggest to eliminate the null values as soon as possible so that the code is not filled with tons of null-safe calls ;-)

– Roland
Nov 20 '18 at 15:33













Why doesn't this work with lists as well? If I have fun foo(c: MutableCollection<out String>) I can call foo(mutableListOf<String>()) but not foo(mutableListOf<String?>()). Any notes to that as well?

– Cilenco
Nov 27 '18 at 13:40







Why doesn't this work with lists as well? If I have fun foo(c: MutableCollection<out String>) I can call foo(mutableListOf<String>()) but not foo(mutableListOf<String?>()). Any notes to that as well?

– Cilenco
Nov 27 '18 at 13:40















2














Even though Yoni Gibbs answer is correct, I would rather take another route here, i.e. working with a non-nullable type, e.g.:



fun sumItems(values: Array<String>) = values.sumBy { it.length }


And as also m0skit0 mentioned in the comment: if you really have null values in your list, filter them out before making the sum, e.g.:



val arrayWithNulls = arrayOf("hello", null, "world")
arrayWithNulls.filterNotNull()
.let(::sumItems)


Or better yet: just skip that method and do:



arrayWithNulls.filterNotNull()
.sumBy { it.length } // or .sumBy(String::length)


Why introducing a new function, if applying existing ones already suffices?



Try to eliminate the null values from your array early. Otherwise your code just gets more complex (adding lots of null-safe things) and that makes the code just less readable. That way you could also skip that filterNotNull.






share|improve this answer


























  • Just be aware of my comment on the question itself: calling filterNotNull creates a new list in memory. Most probably the overhead here is negligible, but if you're working with large data sets you might want to avoid this. You could call asSequence on the original array to get round this though.

    – Yoni Gibbs
    Nov 20 '18 at 15:24













  • I see no real reason to keep null values within an array... that performance impact is negligible and probably completely avoidable ;-) Even if we would work with large data sets (then I assume even more that there are no null values), I would rather filter it once and use non-nullable types everywhere else...

    – Roland
    Nov 20 '18 at 15:32
















2














Even though Yoni Gibbs answer is correct, I would rather take another route here, i.e. working with a non-nullable type, e.g.:



fun sumItems(values: Array<String>) = values.sumBy { it.length }


And as also m0skit0 mentioned in the comment: if you really have null values in your list, filter them out before making the sum, e.g.:



val arrayWithNulls = arrayOf("hello", null, "world")
arrayWithNulls.filterNotNull()
.let(::sumItems)


Or better yet: just skip that method and do:



arrayWithNulls.filterNotNull()
.sumBy { it.length } // or .sumBy(String::length)


Why introducing a new function, if applying existing ones already suffices?



Try to eliminate the null values from your array early. Otherwise your code just gets more complex (adding lots of null-safe things) and that makes the code just less readable. That way you could also skip that filterNotNull.






share|improve this answer


























  • Just be aware of my comment on the question itself: calling filterNotNull creates a new list in memory. Most probably the overhead here is negligible, but if you're working with large data sets you might want to avoid this. You could call asSequence on the original array to get round this though.

    – Yoni Gibbs
    Nov 20 '18 at 15:24













  • I see no real reason to keep null values within an array... that performance impact is negligible and probably completely avoidable ;-) Even if we would work with large data sets (then I assume even more that there are no null values), I would rather filter it once and use non-nullable types everywhere else...

    – Roland
    Nov 20 '18 at 15:32














2












2








2







Even though Yoni Gibbs answer is correct, I would rather take another route here, i.e. working with a non-nullable type, e.g.:



fun sumItems(values: Array<String>) = values.sumBy { it.length }


And as also m0skit0 mentioned in the comment: if you really have null values in your list, filter them out before making the sum, e.g.:



val arrayWithNulls = arrayOf("hello", null, "world")
arrayWithNulls.filterNotNull()
.let(::sumItems)


Or better yet: just skip that method and do:



arrayWithNulls.filterNotNull()
.sumBy { it.length } // or .sumBy(String::length)


Why introducing a new function, if applying existing ones already suffices?



Try to eliminate the null values from your array early. Otherwise your code just gets more complex (adding lots of null-safe things) and that makes the code just less readable. That way you could also skip that filterNotNull.






share|improve this answer















Even though Yoni Gibbs answer is correct, I would rather take another route here, i.e. working with a non-nullable type, e.g.:



fun sumItems(values: Array<String>) = values.sumBy { it.length }


And as also m0skit0 mentioned in the comment: if you really have null values in your list, filter them out before making the sum, e.g.:



val arrayWithNulls = arrayOf("hello", null, "world")
arrayWithNulls.filterNotNull()
.let(::sumItems)


Or better yet: just skip that method and do:



arrayWithNulls.filterNotNull()
.sumBy { it.length } // or .sumBy(String::length)


Why introducing a new function, if applying existing ones already suffices?



Try to eliminate the null values from your array early. Otherwise your code just gets more complex (adding lots of null-safe things) and that makes the code just less readable. That way you could also skip that filterNotNull.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 15:28

























answered Nov 20 '18 at 15:21









RolandRoland

9,74411241




9,74411241













  • Just be aware of my comment on the question itself: calling filterNotNull creates a new list in memory. Most probably the overhead here is negligible, but if you're working with large data sets you might want to avoid this. You could call asSequence on the original array to get round this though.

    – Yoni Gibbs
    Nov 20 '18 at 15:24













  • I see no real reason to keep null values within an array... that performance impact is negligible and probably completely avoidable ;-) Even if we would work with large data sets (then I assume even more that there are no null values), I would rather filter it once and use non-nullable types everywhere else...

    – Roland
    Nov 20 '18 at 15:32



















  • Just be aware of my comment on the question itself: calling filterNotNull creates a new list in memory. Most probably the overhead here is negligible, but if you're working with large data sets you might want to avoid this. You could call asSequence on the original array to get round this though.

    – Yoni Gibbs
    Nov 20 '18 at 15:24













  • I see no real reason to keep null values within an array... that performance impact is negligible and probably completely avoidable ;-) Even if we would work with large data sets (then I assume even more that there are no null values), I would rather filter it once and use non-nullable types everywhere else...

    – Roland
    Nov 20 '18 at 15:32

















Just be aware of my comment on the question itself: calling filterNotNull creates a new list in memory. Most probably the overhead here is negligible, but if you're working with large data sets you might want to avoid this. You could call asSequence on the original array to get round this though.

– Yoni Gibbs
Nov 20 '18 at 15:24







Just be aware of my comment on the question itself: calling filterNotNull creates a new list in memory. Most probably the overhead here is negligible, but if you're working with large data sets you might want to avoid this. You could call asSequence on the original array to get round this though.

– Yoni Gibbs
Nov 20 '18 at 15:24















I see no real reason to keep null values within an array... that performance impact is negligible and probably completely avoidable ;-) Even if we would work with large data sets (then I assume even more that there are no null values), I would rather filter it once and use non-nullable types everywhere else...

– Roland
Nov 20 '18 at 15:32





I see no real reason to keep null values within an array... that performance impact is negligible and probably completely avoidable ;-) Even if we would work with large data sets (then I assume even more that there are no null values), I would rather filter it once and use non-nullable types everywhere else...

– Roland
Nov 20 '18 at 15:32


















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%2f53395798%2fmethod-call-with-nullable-and-not-nullable-generic-array%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

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith