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

  • Home
  • SEARCH
  • 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 595467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:03:54+00:00 2026-05-13T16:03:54+00:00

Looking at the source of Java 6, HashSet<E> is actually implemented using HashMap<E,Object> ,

  • 0

Looking at the source of Java 6, HashSet<E> is actually implemented using HashMap<E,Object>, using dummy object instance on every entry of the Set.

I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself.

But, why is it still used? Is there any reason to use it besides making it easier to maintain the code?

  • 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-05-13T16:03:55+00:00Added an answer on May 13, 2026 at 4:03 pm

    Actually, it’s not just HashSet. All implementations of the Set interface in Java 6 are based on an underlying Map. This is not a requirement; it’s just the way the implementation is. You can see for yourself by checking out the documentation for the various implementations of Set.

    Your main questions are

    But, why is it still used? Is there
    any reason to use it besides making it
    easier to maintain the codes?

    I assume that code maintenance is a big motivating factor. So is preventing duplication and bloat.

    Set and Map are similar interfaces, in that duplicate elements are not allowed. (I think the only Set not backed by a Map is CopyOnWriteArraySet, which is an unusual Collection, because it’s immutable.)

    Specifically:

    From the documentation of Set:

    A collection that contains no
    duplicate elements. More formally,
    sets contain no pair of elements e1
    and e2 such that e1.equals(e2), and at
    most one null element. As implied by
    its name, this interface models the
    mathematical set abstraction.

    The Set interface places additional
    stipulations, beyond those inherited
    from the Collection interface, on the
    contracts of all constructors and on
    the contracts of the add, equals and
    hashCode methods. Declarations for
    other inherited methods are also
    included here for convenience. (The
    specifications accompanying these
    declarations have been tailored to the
    Set interface, but they do not contain
    any additional stipulations.)

    The additional stipulation on
    constructors is, not surprisingly,
    that all constructors must create a
    set that contains no duplicate
    elements (as defined above).

    And from Map:

    An object that maps keys to values.
    A map cannot contain duplicate keys; each key can map to at most one value.

    If you can implement your Sets using existing code, any benefit (speed, for example) you can realize from existing code accrues to your Set as well.

    If you choose to implement a Set without a Map backing, you have to duplicate code designed to prevent duplicate elements. Ah, the delicious irony.

    That said, there’s nothing preventing you from implementing your Sets differently.

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

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's a wealth of material out there, and these two… May 13, 2026 at 9:23 pm
  • Editorial Team
    Editorial Team added an answer From my experience - I have a lot with Wordpress,… May 13, 2026 at 9:23 pm
  • Editorial Team
    Editorial Team added an answer you can update a set of columns (you still have… May 13, 2026 at 9:23 pm

Related Questions

I'm considering creating a screen reader for Android. My questions are as follows. One
I was looking at the source code of java.uti.concurrent.locks.AbstractQueuedSynchronizer and the acquire() method looks
I have a Java project in a SVN repository, with a bunch of .java
I'm trying to select a value in a select element. I'm using Selenium RC
Sometimes I seem to be quite inefficient. When I was nearly finished typing this,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.