Append object to list and return result in Java 8?
13,894
Solution 1
You could use
List<Foo> newList =
Stream.concat(list.stream(), Stream.of(fooToAdd))
.collect(Collectors.toList());
Bt I find this a little bit convoluted. Strive for readability rather than finding single-line, more obscure solutions. Also, never use raw types as you're doing in your question.
Solution 2
You can use var args
and create a stream
from it to be appended to the stream
of the actual list
, e.g:
public static <T> List<T> append(List<T> list, T... args){
return Stream.concat(list.stream(), Stream.of(args))
.collect(Collectors.toList());
}
Author by
mxscho
Updated on June 28, 2022Comments
-
mxscho almost 2 years
Is there a way of appending an object to a list and returning the result in one line in a functional non-imperative way? How would you do it if also the original list should not be mutated? Java 8 is allowed.
I already know how to concat two lists in one line. (Source)
List listAB = Stream.concat(listA.stream(), listB.stream()).collect(Collectors.toList());
I also know how to make a list out of objects in one line.
List listO1 = Collections.singletonList(objectA); List listO2 = Stream.of(objectA, objectB).collect(Collectors.toList()); List listOO = Arrays.asList(objectA, objectB);
Is there anything better than replacing
listB
in the first line with a part of the following lines?-
bracco23 over 7 yearshave you tried listA.clone().append(objectB) ?
-
mxscho over 7 years@bracco23 The
List<T>
interface does not expose anappend(...)
method, does it?. -
JB Nizet over 7 yearsIt doesn't expose a clone() either.
-
mxscho over 7 years@JBNizet I know, but I didn't mention it because it is not far from reality to assume that I could be working with the actual object and not a variable of type
List<T>
. -
bracco23 over 7 years@mxscho you're right it doesn't, but it expose add(...) and the JavaDoc says that the element added goes to the end of the list. Same behaviour, different name.
-
bracco23 over 7 yearsAnd it doesn't expose clone either, but Object does, and it is safe to assume it is overriden such as its behaviour is consistent.
-
-
mxscho over 7 yearsSure about the raw types. I didn't want to bloat the question even if I see that it might be confusing or unusual. I guess your answer is the way to go for me right now, but I thought there might be something better.
-
Andreas over 7 yearsThat's 3 lines. Sure it's only one statement, but that wasn't the question. ;-) 2 statement, 1 line answer here. Haven't decided yet, if my answer is a joke or not.
-
mxscho over 7 years@Andreas While it's somehow funny, it's not a joke. However, by continuing to complain about the other solutions you're forcing me to edit my question to match everyones thought process (one line = one statement = functional, not imperative). I don't want to do that because it would invalidate your answer (and I thought it's clear what I meant). Furthermore, this answer is still not something new to me, I also mentioned it half-way in the question (could be because there is no other good solution). I think I'll wait one more day before accepting it.
-
Andreas over 7 years@mxscho That wasn't a complaint. It's an observation, aka a comment to JB that although he answered the spirit of your question, he didn't answer the letter of your question. And he did answer, because the code in your question is for concatenating two lists, but your question is about how to add a single element to a list, and this answer shows how to adjust the code you showed, to actually do that.
-
mxscho over 7 years@Andreas I explicitely asked in the last sentence if there's another better way of doing it other than just putting my statements together hence this answer is not quite (even if taken literally) what I was looking for because I basically already knew that myself. I'm still not sure if there is something that would satisfy me at all of course. That's why I asked in the first place. Quibbling doesn't help me either, I'm sorry for the bad specification. Neither am I a native speaker nor did I mean to insult anyone by using the word complain instead of comment. Edited it to match my spirit.
-
GhostCat over 4 yearsNice idea, albeit ... the performance impacts might be significant.
-
vivek4348 almost 4 yearsCan you please elaborate on why would this cause a performance impact.