Home

isempty

IsEmpty is a common predicate in programming used to determine whether a container-like object has no elements. It is implemented as a method or function on many data types, including strings, arrays, lists, maps, and sets. When isEmpty returns true, the object contains zero elements; when false, it contains at least one element. For strings, emptiness typically means the string has zero characters; for collections, it means the number of elements is zero.

Language-specific notes:

In Java, String has isEmpty(), which returns true if length() is 0. Subtypes of Collection also provide

Edge cases and performance:

Nullability matters: calling isEmpty on a null reference usually results in an error; frameworks sometimes offer

See also: isNotEmpty, isBlank, and size or length checks.

isEmpty();
for
arrays,
there
is
no
isEmpty
method
and
you
usually
check
array.length
==
0.
In
Python,
there
is
no
isEmpty
method;
emptiness
is
tested
with
len(x)
==
0
for
sequences
and
mappings.
In
JavaScript,
emptiness
is
checked
by
inspecting
length
for
strings
and
arrays;
for
plain
objects
you
commonly
use
Object.keys(obj).length
===
0;
Map
and
Set
expose
a
size
property,
so
map.size
===
0
or
set.size
===
0
checks
emptiness.
In
Ruby,
empty?
is
a
common
method
on
Strings,
Arrays,
and
Hashes,
returning
true
when
there
are
no
characters
or
elements.
In
C++,
containers
provide
empty()
as
a
member
function,
typically
mirroring
size()
==
0;
arrays
do
not
have
an
empty()
member,
so
size
checks
are
used
instead.
safe
checks
or
optional
types
to
handle
nulls
gracefully.
In
well-designed
containers,
isEmpty
is
typically
an
O(1)
operation,
since
it
usually
consults
a
stored
size
or
length
rather
than
counting
elements.