Return from lambda forEach() in java

The return there is returning from the lambda expression rather than from the containing method. Instead of forEach you need to filter the stream: players.stream().filter(player -> player.getName().contains(name)) .findFirst().orElse(null); Here filter restricts the stream to those items that match the predicate, and findFirst then returns an Optional with the first matching entry. This looks less efficient … Read more

What’s the difference between returning void and returning a Task?

SLaks and Killercam’s answers are good; I thought I’d just add a bit more context. Your first question is essentially about what methods can be marked async. A method marked as async can return void, Task or Task<T>. What are the differences between them? A Task<T> returning async method can be awaited, and when the … Read more

Why doesn’t C# support the return of references?

This question was the subject of my blog on June 23rd 2011. Thanks for the great question! The C# team is considering this for C# 7. See https://github.com/dotnet/roslyn/issues/5233 for details. UPDATE: The feature made it in to C# 7! You are correct; .NET does support methods that return managed references to variables. .NET also supports … Read more

How to return result of a SELECT inside a function in PostgreSQL?

Use RETURN QUERY: CREATE OR REPLACE FUNCTION word_frequency(_max_tokens int) RETURNS TABLE (txt text — also visible as OUT param in function body , cnt bigint , ratio bigint) LANGUAGE plpgsql AS $func$ BEGIN RETURN QUERY SELECT t.txt , count(*) AS cnt — column alias only visible in this query , (count(*) * 100) / _max_tokens … Read more

How to specify multiple return types using type-hints

From the documentation class typing.Union Union type; Union[X, Y] means either X or Y. Hence the proper way to represent more than one return data type is from typing import Union def foo(client_id: str) -> Union[list,bool] But do note that typing is not enforced. Python continues to remain a dynamically-typed language. The annotation syntax has … Read more