我正在编写一个实用程序方法,可以检查空字符串,空字符串,集合或对象或任何常规类型 -
public static boolean isEmpty(Object obj) {
if (obj == null)
return true;
if (obj instanceof Collection)
return ((Collection<?>) obj).size() == 0;
// is below line expensive?
final String s = String.valueOf(obj).trim();
return s.length() == 0 || s.equalsIgnoreCase("null");
}
如何使我的上述方法高效,因为上述isEmpty
方法将从应用程序中多次调用,这对性能至关重要?
我怀疑下面的行会因昂贵的toString方法而变得昂贵,并且会产生临时垃圾,这可能会导致GC并降低性能?
final String s = String.valueOf(obj).trim();
更新: -
我现在为每种类型分离了isEmpty方法。以下是我在简化上述isEmpty方法后得到的结果。
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
return false;
}
public static boolean isEmpty(Collection<?> value) {
if (value == null || value.isEmpty()) {
return true;
}
return false;
}
public static boolean isEmpty(String value) {
if (value == null || value.isEmpty()) {
return true;
}
return false;
}
更新2: -
如果我需要检查map null或empty,我应该同时保留两个集合isEmpty和Map isEmpty方法吗?或者Collection isEmpty方法可以正常吗?
public static void main(String[] args) {
Map<String, String> hello = new HashMap<String, String>();
System.out.println(isEmpty(hello));
Map<String, HashMap<Integer, String>> primary = new HashMap<String, HashMap<Integer, String>>();
System.out.println(isEmpty(primary));
}
public static boolean isEmpty(Collection<?> value) {
return value == null || value.isEmpty();
}
public static boolean isEmpty(Map<?, ?> value) {
return value == null || value.isEmpty();
}
答案 0 :(得分:13)
这听起来像是一个糟糕的设计。 Null为null,empty为空,如果它是一个字符串,则为字符串,依此类推。不要试图用一种方法堵塞一切。它的可维护性和可读性都很糟糕。
if (str == null || str.isEmpty())
...
和
if (coll == null || coll.isEmpty())
都非常好。
然而,我个人试图永远不要将null
与空字符串或空集合等同起来。我认为这是一种不好的做法。 null
集合根本不是集合,空集合实际上仍然是集合。您可以通过保持集合非空来避免许多if (coll == null)
检查。如果您担心内存消耗,请使用Collections.emptySet
等等。
话虽如此,如果你仍然想要朝着这个方向前进,我建议你使用普通method overloading并创建一个isEmpty(Collection<?> coll)
和一个isEmpty(String str)
来避免instanceof和铸造。
关于您的修改:
不要这样做
if (value == null || value.isEmpty()) {
return true;
}
return false;
只是做
return value == null || value.isEmpty();
答案 1 :(得分:2)
对于馆藏,您希望使用isEmpty()
代替size()
。对于某些集合类型(例如LinkedList),size()比isEmpty()更昂贵。
答案 2 :(得分:2)
我喜欢在处理此问题的公共库中使用实用程序类。注意,如果对象有一个(在确定对象不为null之后),我们使用对象自己的isEmpty,length或size方法(按此顺序)。通过拨打这个电话,人们不再需要担心NPE了 - 你打电话给这个并且你很有兴趣 - 如果它是真的,那么你的集合/ map / etc不是null有一些东西;如果它是假的,则跳过该项目(它由null或自己的帐户为空)。第二种方法检查数组以查看它是空还是空,但不检查内容。迭代数组时,只需进行检查,然后迭代,并在迭代时检查每个元素。
/**
* Provides methods to perform input validation and boundary validation.
*/
public final class ValidationUtils {
/**
* Check to see if Object is empty or null.
*
* @param object
* The object to check
* @return boolean {@code true} iff the Object is null or determined to be empty (using methods that it provides --
* if it doesn't provide such methods, it's only empty if it's null)
*/
public static boolean isEmpty(@Nullable final Object object) {
if (object == null)
return true;
try {
// Try to use the object class's isEmpty method to check if we're empty, now that we know we're
// not NULL
final Method method = object.getClass().getMethod("isEmpty");
final Object result = method.invoke(object);
if (result instanceof Boolean)
return Boolean.class.cast(result).booleanValue();
} catch (@NotNull final NoSuchMethodException | InvocationTargetException | IllegalArgumentException
| IllegalAccessException | SecurityException ignored) {
// We couldn't invoke... let's go to the next common method
}
try {
// Try to use the object class's length method to check if we're empty, now that we know we're
// not NULL
final Method method = object.getClass().getMethod("length");
final Object result = method.invoke(object);
if (result instanceof Integer)
return Integer.class.cast(result).intValue() <= 0;
if (result instanceof Long)
return Long.class.cast(result).longValue() <= 0L;
} catch (@NotNull final NoSuchMethodException | InvocationTargetException | IllegalArgumentException
| IllegalAccessException | SecurityException ignored) {
// We couldn't invoke... let's go to the next common method
}
try {
// Try to use the object class's size method to check if we're empty, now that we know we're
// not NULL
final Method method = object.getClass().getMethod("size");
final Object result = method.invoke(object);
if (result instanceof Integer)
return Integer.class.cast(result).intValue() <= 0;
if (result instanceof Long)
return Long.class.cast(result).longValue() <= 0L;
} catch (@NotNull final NoSuchMethodException | InvocationTargetException | IllegalArgumentException
| IllegalAccessException | SecurityException ignored) {
// We couldn't invoke... but we're not null... treat it like an Object
}
// Let's treat it like an Object... we're not null, so we're not empty
return false;
}
/**
* Check to see if the array of Objects is empty or null.
*
* @param obj
* Object Array to check
* @return boolean true if empty
*/
public static boolean isEmpty(@Nullable final Object... obj) {
return ((obj == null) || (obj.length == 0));
}
}
示例用途:
final Map<String, String[]> postData = ServletActionContext.getRequest().getParameterMap();
// We're testing if the map is null or empty... we could just do a null check here because of how we're using the map after, but...
if (!ValidationUtils.isEmpty(postData)) {
for (final Map.Entry<String, String[]> reqKey : postData.entrySet()) {
// We're checking if the array is null or doesn't have any length; again, the foreach does the latter, but this is perfectly fine
if (!ValidationUtils.isEmpty(reqKey.getValue())) {
for (final String value : reqKey.getValue()) {
// Checking the value
if (ValidationUtils.isEmpty(value)) {
continue;
}
...
}
}
}
}
答案 3 :(得分:1)
正如我刚刚在my answer中写到你在另一个问题发布前30分钟发布的问题时,每次查看所有内容都是浪费。
但是,在某些情况下,这些类型的功能仍然有用。而不是使用&#34; is-valid&#34;但是,我会把它实现为一个&#34;崩溃 - 如果不好&#34;功能。另请注意,此功能仅适用于集合。
使用示例是
CrashIfCollection.badNullLength(coll, "coll", Null.BAD, 1);
代码:
import java.util.Arrays;
import java.util.Collection;
enum Null {OK, BAD};
public class CrashIfCollection {
public static final void main(String[] ignored) {
test(null);
test(Arrays.asList(new String[] {}));
test(Arrays.asList(new String[] {"one element"}));
}
private static final void test(Collection<?> to_test) {
System.out.println("Testing " + ((to_test == null) ? "null"
: Arrays.toString(to_test.toArray())));
try {
CrashIfCollection.badNullLength(to_test, "to_test", Null.BAD, 1);
} catch(Exception x) {
System.out.println(x);
}
}
public static final void badNullLength(Collection<?> coll, String coll_name, Null nullness, int min_len) {
try {
if(nullness == Null.OK) {
if(coll == null) {
return;
}
if(coll.size() < min_len) {
throw new IllegalArgumentException(coll_name + ".size() (" + coll.size() + ") is less than min_len (" + min_len + ")");
}
}
} catch(NullPointerException npx) {
if(nullness == null) {
throw new NullPointerException("nullness");
}
throw npx;
}
//Null.BAD
try {
if(coll.size() < min_len) {
throw new IllegalArgumentException(coll_name + ".size() (" + coll.size() + ") is less than min_len (" + min_len + ")");
}
} catch(NullPointerException npx) {
throw new NullPointerException(coll_name);
}
}
}
输出:
Testing null
java.lang.NullPointerException: to_test
Testing []
java.lang.IllegalArgumentException: to_test.size() (0) is less than min_len (1)
Testing [one element]
答案 4 :(得分:0)
您可以尝试这样做。大小是否为私有整数。您可以执行以下操作:
public boolean isEmpty()
{
if(size == 0)
{
return true;
}
else
{
return false;
}
}
这对我有用。