DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Reasons to call .isEmpty on collections

Attila-Mihaly Balazs user avatar by
Attila-Mihaly Balazs
·
Oct. 18, 11 · Interview
Like (3)
Save
Tweet
Share
15.26K Views

Join the DZone community and get the full member experience.

Join For Free

i’ve seen many times code like the one below:

if (collection.size() > 0) { ... }

there is just something which inherently “clicks” with most programmers minds when they think “non-empty”. there is however a method which is more appropriate in most of the cases: isempty :

if (!collection.isempty()) { ... }

the top reasons for using isempty rather than size would be:

  • it is more expressive (the code is easier to read and to maintain)
  • it is faster, in some cases by orders of magnitude. two examples from the jdk where this is extremely visible would be the concurrentlinkedqueue and navigablemap / navigableset . all of these implement the “size” method by iterating trough the collection and because of this, calling size gets increasingly slower as the number of elements increase

todays source code performs a comparative benchmark between small (10 element) and large (1 000 000 elements) collections by timing the calls to the size and is empty methods. while benchmarks and especially micro-benchmarks needs be taken with a large grain of salt :-) , these tests show that there is at least a factor of 10x difference in time when calling the size method and there is very little difference when calling the isempty method.

from http://www.transylvania-jug.org/archives/140

Element Testing Programmer (hardware) Magnitude (astronomy) Factor (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Tackling the Top 5 Kubernetes Debugging Challenges
  • Getting a Private SSL Certificate Free of Cost
  • Building a Real-Time App With Spring Boot, Cassandra, Pulsar, React, and Hilla
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: