无法理解:if (val == null ? it.next() == null : val.equals(it.next()))

Can't understand: if (val == null ? it.next() == null : val.equals(it.next()))

我在 Java Tutorial Oracle 上看到了这个代码片段,但是,无论我怎么努力,我都无法理解 if (val == null ? it.next() == null : val.equals(it.next()))

它的功能是什么,它是如何工作的?

public static <E> void replace(List<E> list, E val, E newVal) {
    for (ListIterator<E> it = list.listIterator(); it.hasNext(); )
        if (val == null ? it.next() == null : val.equals(it.next()))
            it.set(newVal);
}

valit.next()之间的相等检查。 null.equals() 将抛出 NullPointerException,因此使用条件来避免这种情况。

if ( // the if statement
    val == null ? // let me name this "condition A"
        it.next() == null : // this will be evaluated if condition A is true
        val.equals(it.next()) // this will be evaluated if condition A is false
) // the if statement

此代码尝试在您的列表中查找 val,然后将其替换为 newVal

for (ListIterator<E> it = list.listIterator(); it.hasNext();)
        if (val == null && it.next() == null)
        { }
        else if val.equals(it.next()))
            it.set(newVal);