Use Java object and primitive Streams, including lambda expressions implementing functional interfaces, to create, filter, transform, process, and sort data.
Perform decomposition, concatenation, and reduction, and grouping and partitioning on sequential and parallel streams.
1. The correct answer is C.
Explanation:
Optional<String> optional = new Optional<>(value);
Optional
does not have a public constructor. Instead, static factory methods like of
and ofNullable
should be used.Optional<String> optional = Optional.of(value);
Optional.of(value)
throws a NullPointerException
if value
is null
. In this scenario, since getValue()
can return null
, this line could lead to an exception.Optional<String> optional = Optional.ofNullable(value);
Optional.ofNullable(value)
will return an Optional
describing the specified value if non-null, or an empty Optional
if the value is null
. This is the appropriate way to handle a potentially null
value.Optional<String> optional = Optional.empty(value);
Optional.empty()
does not accept any arguments. It simply returns an empty Optional
.Optional<String> optional = Optional.nullable(value);
nullable
in the Optional
class. The correct method for this purpose is ofNullable
.2. The correct answer is E.
Explanation:
stream.filter(s -> s.contains("A"));
filter
is an intermediate operation. It returns a new stream with elements that match the given predicate.stream.map(String::toLowerCase);
map
is an intermediate operation. It returns a new stream with elements that are the results of applying the given function.stream.distinct();
distinct
is an intermediate operation. It returns a new stream with distinct elements.stream.limit(2);
limit
is an intermediate operation. It returns a new stream that is truncated to be no longer than the given size.stream.collect(Collectors.toList());
collect
is a terminal operation. It triggers the processing of the stream and collects the elements into a List
.3. The correct answer is D.
Explanation:
int sum = numbers.stream().sum();
stream
method directly on them. You need to use a method from a utility class like IntStream
to create a stream.int sum = IntStream.range(0, numbers.length).sum();
IntStream.range(0, numbers.length)
generates a stream of integers from 0 to the length of the array, not the elements of the array itself.int sum = IntStream.from(numbers).sum();
IntStream
does not have a from
method. The correct method is of
.int sum = IntStream.of(numbers).sum();
IntStream.of(numbers).sum()
correctly creates an IntStream
from the array and calculates the sum of its elements.int sum = IntStream.range(numbers).sum();
IntStream.range
requires two arguments (a start and an end index) and is used to generate a stream of numbers within a range, not to sum an array.4. The correct answer is A.
Explanation:
Stream<String> filteredStream = stream.filter(s -> s.length() > 3);
filter
is the correct intermediate operation to apply a predicate to each element of the stream and return a new stream containing only elements that match the predicate.Stream<String> filteredStream = stream.map(s -> s.length() > 3);
map
is used to transform elements of the stream and does not filter them. The result would be a stream of Boolean
values instead of the original strings.Stream<String> filteredStream = stream.collect(Collectors.filtering(s -> s.length() > 3));
Collectors.filtering
is not a valid method. Filtering is done through the filter
method on the stream itself, not via collectors.Stream<String> filteredStream = stream.filtering(s -> s.length() > 3);
filtering
method on the stream. The correct method is filter
.Stream<String> filteredStream = stream.filterByLength(3);
filterByLength
method on the stream. The correct method to use is filter
.5. The correct answer is C.
Explanation:
Stream<String> lengthStream = stream.map(s -> s.length());
map
method will transform the elements to Integer
, not String
. The correct type for the resulting stream should be Stream<Integer>
.Stream<String> lengthStream = stream.mapToInt(s -> s.length());
mapToInt
produces an IntStream
, not a Stream<String>
. Additionally, the resulting stream type would not be Stream<String>
.Stream<Integer> lengthStream = stream.map(s -> s.length());
map
transforms each string in the stream to its length, resulting in a Stream<Integer>
.IntStream lengthStream = stream.map(s -> s.length());
map
produces a Stream<R>
, not an IntStream
. The correct method for producing an IntStream
would be mapToInt
.Stream<String> lengthStream = stream.flatMap(s -> Stream.of(s.length()));
flatMap
is used to flatten nested streams and not simply map to another type. Additionally, the resulting stream type would not be Stream<String>
.6. The correct answer is A.
Explanation:
Stream<String> resultStream = stream.skip(2).limit(3);
skip(2)
skips the first 2 elements of the stream, and limit(3)
limits the stream to the next 3 elements. Therefore, the resulting stream will contain the 3rd, 4th, and 5th elements of the original list.Stream<String> resultStream = stream.limit(3).skip(2);
limit(3)
first limits the stream to the first 3 elements, and then skip(2)
skips 2 of those elements, resulting in a stream with only the 3rd element.Stream<String> resultStream = stream.skip(3).limit(2);
skip(3)
skips the first 3 elements, and limit(2)
then limits the stream to the next 2 elements, resulting in a stream with the 4th and 5th elements.Stream<String> resultStream = stream.limit(2).skip(3);
limit(2)
first limits the stream to the first 2 elements, and then skip(3)
would attempt to skip more elements than are available, resulting in an empty stream.Stream<String> resultStream = stream.slice(2, 5);
slice
method in the Stream API. The correct methods to achieve the desired result are skip
and limit
.7. The correct answer is B.
Explanation:
Stream<String> resultStream = Stream.concat(stream1, stream2.collect(Collectors.toList()));
Stream.concat
expects two streams as arguments. stream2.collect(Collectors.toList())
converts stream2
into a List
, not a Stream
.Stream<String> resultStream = Stream.concat(stream1, stream2);
Stream.concat(stream1, stream2)
correctly concatenates the two streams into a single stream containing all elements from both streams.Stream<String> resultStream = stream1.concat(stream2);
Stream
does not have an instance method concat
. The concat
method is a static method of the Stream
class.Stream<String> resultStream = stream1.merge(stream2);
merge
method in the Stream
API. The correct method for concatenating streams is Stream.concat
.Stream<String> resultStream = Stream.of(stream1, stream2);
Stream.of(stream1, stream2)
creates a stream of streams, resulting in Stream<Stream<String>>
rather than a single concatenated Stream<String>
.8. The correct answer is E.
Explanation:
int product = stream.reduce(1, (a, b) -> a + b);
(a, b) -> a * b
.int product = stream.reduce((a, b) -> a * b);
Optional<Integer>
rather than an int
.int product = stream.reduce(0, (a, b) -> a * b);
1
, not 0
. Using 0
as the identity value would result in a product of 0
regardless of the stream elements.Optional<Integer> product = stream.reduce(1, (a, b) -> a * b);
reduce
method with an identity value does not return an Optional
. It should return the result directly as int
.int product = stream.reduce(1, (a, b) -> a * b, (a, b) -> a * b);
reduce
method with an identity value of 1
and a combiner function that multiplies the results. This form of reduce
is suitable for parallel processing as well, ensuring the product is correctly calculated across multiple segments of the stream.9. The correct answer is B.
Explanation:
Set<String> resultSet = stream.collect(Collectors.toSet());
Collectors.toSet()
does not guarantee the order of the elements. The implementation returned by this collector does not preserve the order of insertion.Set<String> resultSet = stream.collect(Collectors.toCollection(LinkedHashSet::new));
Collectors.toCollection(LinkedHashSet::new)
collects the elements into a LinkedHashSet
, which maintains the order of insertion.Set<String> resultSet = stream.collect(Collectors.toCollection(TreeSet::new));
TreeSet
sorts the elements according to their natural ordering (or by a comparator, if provided). This does not necessarily preserve the original order of the stream elements.Set<String> resultSet = stream.collect(Collectors.toList());
Collectors.toList()
collects the elements into a List
, not a Set
.Set<String> resultSet = stream.collect(Collectors.toMap());
Collectors.toMap()
is used to collect the elements into a Map
, not a Set
.Do you like what you read? Would you consider?
Do you have a problem or something to say?