This sounds like a bad design to me. Null is null, empty is empty, if it's a string it's a string, and so on. Don't try to jam everything up in one method. It's bad for maintainability and readability.
if (str == null || str.isEmpty())
...
and
if (coll == null || coll.isEmpty())
are both perfectly fine.
Personally however, I try to never ever equate null with an empty string or empty collection. I think it's a bad practice. A null collection is no collection at all, an empty collection is in fact still a collection. You can avoid many if (coll == null) checks by keeping a collection non-null. If you're worried about memory consumption, use use Collections.emptySet et al.
That being said, if you still want to go in this direction, I'd suggest you use plain method overloading and create one isEmpty(Collection<?> coll) and one isEmpty(String str) to avoid instanceof and casting.
Regarding your edit:
Don't do for instance
if (value == null || value.isEmpty()) {
return true;
}
return false;
just do
return value == null || value.isEmpty();
Answer from aioobe on Stack OverflowVideos
SORRY
I JUST REALISED I MADE A MISTAKE I WANTED TO ASK ABOUT EQUALS("") BUT AT THE SAME TIME I WAS FIGHTING WITH ANOTHER EXCERCISE IN WHICH I HAD TO USE CONTAINS() METHOD AND THIS MESSED UP MY THOUGHTS SORRY FOR WASTING YOUR TIME :/
but still i was able to get some usefull info about contains method so it wasn't as much wasted effort but still sorry
Hi i'm doing(or atleast trying to do) mooc.fi java part 1 course and there in some exercises is required to check for empty input.
So there is my question what is difference between .isEmpty() and .Equals("") because i like to use the first one but in suggested solutions always is used ".Equals("") and i'm wondering is there any rule for using .isEmpty(), maybe it's the matter of optimalization etc. or maybe both are as good
I know it can be stupid question but it's better to ask that and get rid of bad habits in writing code as soon as possible :D
In advance thanks for answers
P.S i hope everything is easy to understand unfortunately my english is still far from good :D
This sounds like a bad design to me. Null is null, empty is empty, if it's a string it's a string, and so on. Don't try to jam everything up in one method. It's bad for maintainability and readability.
if (str == null || str.isEmpty())
...
and
if (coll == null || coll.isEmpty())
are both perfectly fine.
Personally however, I try to never ever equate null with an empty string or empty collection. I think it's a bad practice. A null collection is no collection at all, an empty collection is in fact still a collection. You can avoid many if (coll == null) checks by keeping a collection non-null. If you're worried about memory consumption, use use Collections.emptySet et al.
That being said, if you still want to go in this direction, I'd suggest you use plain method overloading and create one isEmpty(Collection<?> coll) and one isEmpty(String str) to avoid instanceof and casting.
Regarding your edit:
Don't do for instance
if (value == null || value.isEmpty()) {
return true;
}
return false;
just do
return value == null || value.isEmpty();
For collections, you'll want to use isEmpty() instead of size(). For some collection types (such as LinkedList), size() is more expensive than isEmpty().