我有一个 LinkedList
作为 -
List<String> tables = new LinkedList<String>();
有时,tables
列表看起来像,这意味着它里面会有所有空字符串值 -
[null, null]
是否有任何直接的方法来识别tables
列表中的所有元素是否为null
字符串,然后返回true,否则返回false。
我能想到的一种方法是继续迭代它,看看它是否有空字符串,然后相应地返回 true 或 false。
更新:-
public static void main(String[] args) {
String table_1 = null;
String table_2 = "hello";
List<String> tables = new LinkedList<String>();
tables.add(table_1);
tables.add(table_2);
boolean ss = isAllNull(tables);
System.out.println(ss);
}
public static boolean isAllNull(Iterable<?> list) {
for (Object obj : list) {
if (obj != null)
return false;
}
return true;
}
最佳答案
如果你可以使用 Guava 库:
Iterables.all(input, Predicates.isNull());
使用 static import
它将变得更具可读性:
import static com.google.common.base.Predicates.isNull;
import static com.google.common.collect.Iterables.all;
Iterable<?> input = ...
all(input, isNull())
关于java - 如何检查一个List是否包含所有元素为NULL字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21592835/