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 Video Library
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
View Events Video Library
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
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Spice Up Your 'CI/CD Process' With Automation Using Cucumber, Selenium, and Kotlin
  • Playwright: Test Automation You Must Know
  • How to Test Your Website’s Accessibility With JAWS
  • XPath: Axis Steps, Node Tests, Unabbreviated Syntax

Trending

  • CI/CD Docker: How To Create a CI/CD Pipeline With Jenkins, Containers, and Amazon ECS
  • AWS vs. Azure vs. Google Cloud: Comparing the Top Cloud Providers
  • Reflections From a DBA
  • Spring Authentication With MetaMask

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.37K 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.

Related

  • Spice Up Your 'CI/CD Process' With Automation Using Cucumber, Selenium, and Kotlin
  • Playwright: Test Automation You Must Know
  • How to Test Your Website’s Accessibility With JAWS
  • XPath: Axis Steps, Node Tests, Unabbreviated Syntax

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: