Map and FindFirst - java

Is it valid to use findFirst() and map() in pipeline.findFirst is shortcircuit method whereas map is intermediate operation.
this.list.stream().filter(t -> t.name.equals("pavan")).findFirst().map(toUpperCase()).orElse(null);
Is it valid to use map in pipeline like above??

Yes, you can use map after findFirst. The Key thing to know here is that findFirst() returns an Optional and hence, you can't simply use the return value without first checking whether the optional has got a value or not. The snippet below assumes that you were working with a list of objects of Person class.
Optional<String> result = this.list.stream()
.filter(t -> t.name.equals("pavan")) // Returns a stream consisting of the elements of this stream that match the given predicate.
.findFirst() // Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
.map(p -> p.name.toUpperCase()); // If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
// This check is required!
if (result.isPresent()) {
String name = result.get(); // If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
System.out.println(name);
} else {
System.out.println("pavan not found!");
}
One more error with your snippet was where you were using toUpperCase. It needs a String, whereas the implicit argument that was getting passed in your snippet was an object of Person class.

Related

Printing max element in a Deque<Integer>

I have a Deque from Integers, which I'm using as a Que. I have to add and remove elements from it and in some cases I need to print the max value in the Que.
The thing that I've tried is:
System.out.println(deque.stream().max(Integer::compareTo));
But it prints - Optional[the current max int].
Thank you!
That is because the max method of java.util.Stream returns your value wrapped in Optional. This is the signature of the max method Optional<T> max(Comparator<? super T> comparator);
The example below would provide you the expected behaviour:
Optional<Integer> optionalOfMax = deque.stream().max(Integer::compareTo);
System.out.println(optionalOfMax.orElseThrow());
You can do it as follows:
deque.stream().max(Integer::compareTo).ifPresent(System.out::println);
Note that deque.stream().max(Integer::compareTo) returns Optional<Integer>.
Alternatively,
deque.stream().flatMapToInt(x -> IntStream.of(x)).max().ifPresent(System.out::println);
Stream#flatMapToInt returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
You can just use the code you have written and extend it by .get().
The following code
public static void main(String[] args) {
// create a Deque that holds Integers
Deque<Integer> myDQ = new ArrayDeque<Integer>();
// add some of them
myDQ.add(12);
myDQ.add(13);
myDQ.add(14);
myDQ.add(15);
myDQ.add(16);
myDQ.add(20);
myDQ.add(17);
myDQ.add(18);
myDQ.add(19);
// and print
System.out.println(
myDQ.stream()
.max(Integer::compareTo) // the largest one
.get() // not as Optional but only as Integer
);
}
just prints 20.
The max-Method returns an java.util.Optional. If you just want to return a int-Value you can use the orElse-Method from java.util.Optional to return the given value or, if not present, another default.
System.out.println(deque.stream().max(Integer::compareTo).orElse(0));
You can unbox the Integer wrappers in your queue and use IntStream.max(), which returns an OptionalInt:
deque.stream().mapToInt(Integer::intValue)
.max()
.ifPresent(System.out::println);
This will do nothing if max() returns OptionalInt.empty(), which happens when the deque is empty. If you want to check for emptiness, you can do, for example:
deque.stream().mapToInt(Integer::intValue)
.max()
.ifPresentOrElse(System.out::println,
() -> throw new RuntimeException("Attempt to get max of empty collection"));

How to map value at index 0 for a list in an Optional Stream

I have the following method which is working fine. I am trying to accomplish everything and get the value inside that Optional stream
without having to do the additional if check. Is it possible to map and get the Result object at index 0? Please advice thanks.
public String getData(HttpEntity<Request> request, String endPoint){
ResponseEntity<Reponse> response =
template.exchange(endPoint, HttpMethod.POST, request, Reponse.class);
List<Result> results = Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
// getResults is an ArrayList of Result Objects. Could I get the Result Object at index 0 here?
// following that I plan to go .map(Result::getValue) here.
.orElse(null);
if(CollectionUtils.isNotEmpty(results)){
return results.get(0).getValue();
}
return null;
}
return Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(list -> list.get(0)) // hate this part :)
.map(Result::getValue)
.orElse(null);
If you are a fan of method references, and you find lambdas boring
return Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(List::iterator)
.map(Iterator::next)
.map(Result::getValue)
.orElse(null);
I showed it for educational reasons, I don't advocate it even though I like it.
Assuming that the ArrayList is never null:
.flatMap(r -> r.stream().findFirst())
This takes the list, streams it, gets an Optional with the first element (or an empty Optional if the list is empty. Lastly, since an Optional<Optional<Result>> isn't that useful, we use flatMap instead of map to collapse it into an Optional<Result>.
change that orElse to return an empty List and stream from there. In such a way you can invoke findFirst safely - as for an empty List it will return Optional::empty and from there - you either map it (in case you do have it) to Result::getValue or, in case such a List is not present - null, so it is exactly as your program flow.
...
.map(QueryResult::getResults)
.orElse(Collections.emptyList())
.stream()
.findFirst()
.map(Result::getValue)
.orElse(null);

type of map() and filter() operations of Optional

Are the map() and filter() of Optional are lazy like Stream?
How can I confirm their type?
There is a fundamental difference between a Stream and an Optional.
A Stream encapsulates an entire processing pipeline, gathering all operations before doing anything. This allows the implementation to pick up different processing strategies, depending on what result is actually requested. This also allows to insert modifiers like unordered() or parallel() into the chain, as at this point, nothing has been done so far, so we can alter the behavior of the subsequent actual processing.
An extreme example is Stream.of(1, 2, 3).map(function).count(), which will not process function at all in Java 9, as the invariant result of 3 can be determined without.
In contrast, an Optional is just a wrapper around a value (if not empty). Each operation will be performed immediately, to return either, a new Optional encapsulating the new value or an empty Optional. In Java 8, all methods returning an Optional, i.e.map, flatMap or filter, will just return an empty optional when being applied to an empty optional, so when chaining them, the empty optional becomes a kind of dead-end.
But Java 9 will introduce Optional<T> or​(Supplier<? extends Optional<? extends T>>), which may return a non-empty optional from the supplier when being applied to an empty optional.
Since an Optional represents a (possibly absent) value, rather than a processing pipeline, you can query the same Optional as many times you want, whether the query returns a new Optional or a final value.
It’s easy to verify. The following code
Optional<String> first=Optional.of("abc");
Optional<String> second=first.map(s -> {
System.out.println("Running map");
return s + "def";
});
System.out.println("starting queries");
System.out.println("first: "+(first.isPresent()? "has value": "is empty"));
System.out.println("second: "+(second.isPresent()? "has value": "is empty"));
second.map("second's value: "::concat).ifPresent(System.out::println);
will print
Running map
starting queries
first: has value
second: has value
second's value: abcdef
demonstrating that the mapping function is evaluated immediately, before any other query, and that we still can query the first optional after we created a second via map and query optionals multiple times.
In fact, it is strongly recommended to check via isPresent() first, before calling get().
There is no equivalent stream code, as re-using Stream instances this way is invalid. But we can show that intermediate operations are not performed before the terminal operation has been commenced:
Stream<String> stream=Stream.of("abc").map(s -> {
System.out.println("Running map");
return s + "def";
});
System.out.println("starting query");
Optional<String> result = stream.findAny();
System.out.println("result "+(result.isPresent()? "has value": "is empty"));
result.map("result value: "::concat).ifPresent(System.out::println);
will print
starting query
Running map
result has value
result value: abcdef
showing that the mapping function is not evaluated before the terminal operation findAny() starts. Since we can’t query a stream multiple times, findAny() even uses Optional as return value, which allows us to do that with the final result.
There are other semantic differences between the operations of the same name, e.g. Optional.map will return an empty Optional if the mapping function evaluates to null. For a stream, it makes no difference whether the function passed to map returns null or a non-null value (that’s why we can count the elements without knowing whether it does).
String r = Optional.of("abc")
.map(s -> {
System.out.println("Running map");
return s + "def";
})
.filter(s -> {
System.out.println("First Filter");
return s.equals("abcdef");
})
.map(s -> {
System.out.println("mapping");
return s + "jkl";
})
.orElse("done");
System.out.println(r);
Running this will produce:
Running map, First Filter, mapping abcdefjkl
On the other hand running this:
String r = Optional.of("mnt") //changed
.map(s -> {
System.out.println("Running map");
return s + "def";
})
.filter(s -> {
System.out.println("First Filter");
return s.equals("abcdef");
})
.map(s -> {
System.out.println("mapping");
return s + "jkl";
})
.orElse("done");
Running map, First Filter, done
I always thought that since the map is only execute based on the previous filter, this would be considered lazy. It turns out this is not true:
Optional.of("s").map(String::toUpperCase)
Stream.of("test").map(String::toUpperCase)
The map from Optional will get executed; while the one from Stream will not.
EDIT
go and up-vote the other answer here. This is edited because of the other one.

How to use Stream instead for each loop

I have a loop which update an String object:
String result = "";
for (SomeObject obj: someObjectList) {
result = someMetohd(obj, result);
}
An implementation of someMethod is irrelevant:
private String someMethod(SomeObject obj, String result) {
result = result.concat(obj.toString());
return result;
}
And I want to use Stream instead a loop. How to implement it with Stream?
#SuppressWarnings("OptionalGetWithoutIsPresent")
String result = Stream.concat(Stream.of(""), someObjectList.stream())
.reduce(this::someMethod)
.get();
Your someMethod should be associative as specified in the documentation, however this is only important for parallel streams, while your code is explicitly sequential
As you always add to the result, you can consider it a first element of the stream and then use reduce method which will always merge first two elements - current result and next element
result has to be the first parameter of your someMethod
Because all elements in the stream have to be of the same type, while you have String result and SomeObject elements, you need to change the signature of someMethod to accept two Objects (and do the casts inside the method): private String someMethod(Object result, Object obj). This is the most ugly part of this solution.
You can inline the initial value of the result - no need to define result upfront
You might want to change this::someMethod depending on where this method is declared
Finally, you don't need to worry about handling Optional result, because the stream always has at least one element so it's safe to just call get()
final StringBuilder resultBuilder = new StringBuilder();
someObjectList.stream().map(SomeObject::toString).forEach(resultBuilder::append);
final String result = resultBuilder.toString();
To know more about Streams, you can check this page: http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/, I think it's very helpful.
Although the functional equivalent of what you're trying to achieve here is possible with streams, it's worth reminding you that functional and iterative ways of thinking are not necessarily compatible.
Generally you think of each element on its own, and you don't have visibility over other elements, unless you're using a special function like reduce.
Here's something that does what you've asked for:
final List<Object> objectList = Arrays.asList("a", "b", "c", "d");
String concatString = objectList.stream()
.map(e -> e.toString())
.reduce((result, element) -> result.concat(e))
.get();
Map turns the entire stream into a list, but with the toString function called separately on every element. Reduce is more complex. It can be described as an accumulation. It executes a function between the result, and the current element. In this case, it takes the first element, and concatenates it to the second. It then takes the first/second concatenation, and applies the same function to the third. And so on.
Instead of dealing with lambdas, you can also pass in methods directly, to tighten up your code a bit:
String result = objectList.stream()
.map(Object::toString)
.reduce(String::concat)
.get();

Java stream of optionals to optional stream

I need to convert Stream<Optional<Integer>> to Optional<Stream<Integer>>.
The output Optional<Stream<Integer>> should be an empty value when at least one value ofStream<Optional<Integer>> is empty.
Do you know any functional way to solve the problem? I tried to use collect method, but without success.
Well, the tricky thing here is that if you're just given a Stream, you can only use it once.
To be stateless and avoid redundant copying, one way is to just catch NoSuchElementException:
static <T> Optional<Stream<T>> invert(Stream<Optional<T>> stream) {
try {
return Optional.of(
stream.map(Optional::get)
.collect(Collectors.toList())
.stream());
} catch (NoSuchElementException e) {
return Optional.empty();
}
}
A simple inversion would be:
static <T> Optional<Stream<T>> invert(Stream<Optional<T>> stream) {
return Optional.of(stream.map(Optional::get));
}
But to find out if it contains an empty element, you need to actually traverse it which also consumes it.
If you're given the source of the stream, you can traverse it without collecting it:
static <T> Optional<Stream<T>> invert(
Supplier<Stream<Optional<T>>> supplier) {
// taking advantage of short-circuiting here
// instead of allMatch(Optional::isPresent)
return supplier.get().anyMatch(o -> !o.isPresent()) ?
Optional.empty() : Optional.of(supplier.get().map(Optional::get));
}
List<Optional<Integer>> myInts =
Arrays.asList(Optional.of(1), Optional.of(2), Optional.of(3));
Optional<Stream<Integer>> inverted = invert(myInts::stream);
That's probably a more interesting approach. (But it's prone to a race condition because the stream() is taken twice. If some other thread adds an empty element in between and gets away with it, we have a problem.)
Though this has already been answered yet to add to the list, with Java-9 introducing Optional.stream, this should be achievable as:
// initialized stream of optional
Stream<Optional<Integer>> so = Stream.empty();
// mapped stream of T
Stream<Integer> s = so.flatMap(Optional::stream);
// constructing optional from the stream
Optional<Stream<Integer>> os = Optional.of(s);
Similar to Radiodef's answer, though this one avoids the exception handling and the intermediate list.
private static <T> Optional<Stream<T>> invertOptional(Stream<Optional<T>> input) {
return input.map(integer -> integer.map(Stream::of))
.collect(Collectors.reducing((l, r) -> l.flatMap(lv -> r.map(rv -> Stream.concat(lv, rv)))))
.orElse(Optional.empty());
}
The way this works is it maps to a Stream of Optional Streams of T. The Optional.map is used in this case, so each one of the Optional<Stream<T>> items in the resultant stream is a either a Stream of 1, or an empty Optional.
Then it collects these streams by reducing them together. the l.flatMap will return an empty Optional if l is empty or the r.map returns an empty. if r.map isn't empty, it calls the Stream.concat, which combines the left and right stream values.
The whole collect reduction produces an Optional<Optional<Stream<T>>>, so we narrow that down with the .orElse(Optional.empty)
Note: Code is tested, and appears to work. The unspecified "edge case" of an empty input Stream is treated as an an empty Optional, but can be easily changed.
final Stream<Optional<Integer>> streamOfInts = Stream.of(Optional.of(1), Optional.of(2), Optional.of(3), Optional.of(4), Optional.of(5));
// false - list of Optional.empty(); true -> list of Optional.of(Integer)
final Map<Boolean, List<Optional<Integer>>> collect = streamOfInts.collect(Collectors.partitioningBy(Optional::isPresent));
final Function<List<Optional<Integer>>, Stream<Integer>> mapToStream = List->List.stream().filter(o->o.isPresent()).map(o->o.get());
Optional<Stream<Integer>> result = Optional
.of(Optional.of(collect.get(false)).filter(list->list.size()>0).orElse(collect.get(true)))
.filter(list->list.size()>0)
.filter(list->list.get(0).isPresent())
.map(mapToStream)
.map(Optional::of)
.orElse(Optional.empty());

Categories

Resources