java coalesce

Solutions on MaxInterview for java coalesce by the best coders in the world

showing results for - "java coalesce"
Nicolò
27 Nov 2019
1// Short answer: no
2
3// The best you can do is to create a static utility method (so that it can be 
4// imported using import static syntax)
5
6public static <T> T coalesce(T one, T two)
7{
8    return one != null ? one : two;
9}
10
11// The above is equivalent to Guava's method firstNonNull by @ColinD, but that 
12// can be extended more in general
13
14public static <T> T coalesce(T... params)
15{
16    for (T param : params)
17        if (param != null)
18            return param;
19    return null;
20}