Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:07:47+00:00 2026-06-10T13:07:47+00:00

Is there a java collection that works like a LinkedHashMap but reflects the order

  • 0

Is there a java collection that works like a LinkedHashMap but reflects the order also in equals and hashCode?
-> two Maps with same elements but different order should not be equal and should have different hashCodes.

Solution based on Peter Lawrey’s answer (xAxis is the LinkedHashMap):

hashCode:

...
result = prime * result + ((xAxis == null) ? 0 : xAxis.hashCode() + xAxis.toString().hashCode());
...

equals:

...
if (xAxis == null) {
        if (other.xAxis != null) {
            return false;
        }
    } else if (!xAxis.equals(other.xAxis)) {
        return false;
    } else if (!xAxis.toString().equals(other.xAxis.toString())) {
        return false;
    }
...

(it based on code generated by eclipse)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:07:49+00:00Added an answer on June 10, 2026 at 1:07 pm

    You can override the hashCode and equals of HashMap.

    Since HashMap has no order, this is very dangerous but you can do it if you want. You might find that comparing the toString() of each is all you really need.

    BTW: To get an idea of how many different orders you can have for a keys of a HashMap, this blog post gives you an idea using HashSet (which uses the same code)

    http://vanillajava.blogspot.co.uk/2011/09/order-of-elements-in-hash-collection.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a standard Java (1.5+) implementation (i.e. no 3rd party) of a collection
Is there any Java lib that allows me to download web pages recursively given
Is there a Java web framework that allows the user to create custom fields,
In Java is there anyway to have one constructor that will accept an array
I would like to implement a variation on the Map of Sets collection that
I'm writing a Risk-like board game in java. A feature is that players can
We have a huge java application that entirely works based on JAXB serialization.The middleware
There are a huge feature collection in C++ programming language that supply a strict
First, I would like to mention that I also had this problem when 2.2
In java there is an optimal buffer size of 32 Kb which is based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.