This question already has answers here:
Lambda expression to convert array/List of String to array/List of Integers
(10 answers)
Closed 6 years ago.
I am converting my project to java8. How would I write this code in a better way using java8?
List<Bar> bars = new ArrayList<>();
for (Foo foo : obj.getFooList()) {
bars.add(Helper.fooToBar(foo));
}
return detailsVos;
Stream the list, mapping using a method reference, then collect to a list and return it:
return obj.getFooList().stream().map(Helper::fooToBar).collect(Collectors.toList());
Note that "better" has been interpreted as "neater" and "using the Java 8 style".
Also note that this may perform slightly worse than your original code, due to the overhead of using a stream.
Related
This question already has answers here:
What are the differences between C-like, constructor, and uniform initialization?
(2 answers)
What are the advantages of list initialization (using curly braces)?
(5 answers)
Closed 3 years ago.
I am new to C++ 11 & 14. In my new role I saw a code where string was initialized (below) and I do not know what is this new way called and where to read about it. Can some one tell what is it called and how does it work. Regards.
std::string mystring{""};
This is initialization with string literal, refer to :https://en.cppreference.com/w/cpp/string/basic_string/basic_string
see Notes.
Your example is same as std:string s{"",0};
This question already has answers here:
Explain the syntax of Collections.<String>emptyList()
(5 answers)
Closed 5 years ago.
Formal parameter type is allowed in Java 8 and, it is usually used when there are parameters with generic types AFAIK.
However, there are indeed some methods without parameters but formal parameter type anyway. For instance,
<T> Stream<T> java.util.stream.Stream.empty()
Anyone can explain on this?
The generic type argument is required here to specify the element type of the returned empty Stream. Otherwise this method would return a raw Stream type.
For example:
Stream<String> stream = Stream.empty();
This question already has answers here:
Chaining Optionals in Java 8
(10 answers)
Closed 6 years ago.
I have three java8 Optionals, and want to return whichever one is actually present in a preferred order. It seems like there should be an easy way to chain them like so:
return optionalA.orElseIfPresent(optionalB).orElseIfPresent(optionalC);
if all three are empty, then an Optional.empty() ought to be returned.
the existing orElse and orElseGet are not really up to the task - they must return an actual value, so it's not possible for the remaining fallbacks to be Optionals themselves.
In the worst case I can have a long list of ifPresent() checks, but there just seems like there's a better way to go about it?
return Stream.of(optional1, optional2, optional3)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
I like JB Nizet's answer (upvoted as well). Alternatively, using only Optional (for whatever reason):
Optional.ofNullable(
optionalA.orElse(
optionalB.orElse(
optionalC.orElse(null))
)
);
which falls into indentation/parenthesis madness and I personally do not like.
This question already has answers here:
F# Functions vs. Values
(4 answers)
Closed 7 years ago.
I have F# code something like this:
let ran = new System.Random()
let makeVal = ran.NextDouble()
Why when I use makeVal do I get the same random number on every call within one run of the app session (i.e. it's not a seed issue).
Values in F# are immutable by default. So makeVal will not change after the first binding. To get different random values you should call ran.NextDouble() again.
For example use the function:
let makeVal() = ran.NextDouble()
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A cycle was detected in a LINQ expression exception
I have a small problem. I have 2 IQueryable (A and B). I want to find the complement numbers. For this i use Except: A.Except(B)
This will give me all the number in A that ARE NOT in B.
This works. My problem is that i want to do this A = A.Except(B)
But this leaves me with an ERROR:
A cycle was detected in a LINQ expression exception
Anyone got a suggestion on how I could solve this.
I can't just create a new IQueryable C to hold the A.Except(B) result. Because I need A repediatly in later code.
Just create an intermediate list which you use to store your result.
var C = A.Except(B).ToArray();
C will hold your desired result, while A and B will remain unchanged.