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
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
  1. DZone
  2. Coding
  3. Java
  4. HE-Collections - Java Collections independent from hashCode and equals

HE-Collections - Java Collections independent from hashCode and equals

Frank Lemke user avatar by
Frank Lemke
·
Apr. 16, 12 · News
Like (0)
Save
Tweet
Share
600 Views

Join the DZone community and get the full member experience.

Join For Free

HE-Collection

Independence from hashCode() and equals(Object)

 

Motivation

Did you ever see a project where classes break the contract betweenthe methods hashCode() and equals(Object) defined injava.lang.Object? This contract says, that equal objects must provide the same hash-code. The criteria can be changed in any custom class by overriding the default implementations in java.lang.Object.

Legacy Projects may need "standard" Corrections

Objectsbreaking the contract can not be used in a Java-Standard-Collection (package java.util), because the collection will behave unpredictably.You may find this problem in a legacy project, where you are unable (because of risks or workload) to change any implementation of the hashCode() and equals(Object) methods.

Special Use Case may need "special" corrections

Another problem with the contract and Java-Standard-Collections can occur if you need - in a specal use case for example - other criteria for equality than implemented. In such a case, you would need to extend or wrap the values, so that the Java-Standard-Collection will use your use case specific implementations of hashCode() and equals(Object).

HE-Collection

"HE-Collection" is a new project on sourceforge. It's based on the Java-Standard-Collection and provides a kind of transparent wrapping- and switch-mechanism in order to achieve the independence mentioned above: Values that are stored in a HE-Collection don't need to implement the contract correctly. Calls to hashCode() and equals() from the Java-Standard-Collection to the value will be delegated to any other method.

The project HE-Collection can be found here.

Use case Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Beginners’ Guide to Run a Linux Server Securely
  • Load Balancing Pattern
  • Why Open Source Is Much More Than Just a Free Tier
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future

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: