📜  java coalesce - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:32.615000             🧑  作者: Mango

代码示例1
// Short answer: no

// The best you can do is to create a static utility method (so that it can be 
// imported using import static syntax)

public static  T coalesce(T one, T two)
{
    return one != null ? one : two;
}

// The above is equivalent to Guava's method firstNonNull by @ColinD, but that 
// can be extended more in general

public static  T coalesce(T... params)
{
    for (T param : params)
        if (param != null)
            return param;
    return null;
}