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 8009529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:28:38+00:00 2026-06-04T18:28:38+00:00

I want to create a map which has a certain class as a key.

  • 0

I want to create a map which has a certain class as a key. The problem I ran into is that since this class contains pointers, this address is used when hashing if I use a HashMap (See my code below). How can I get it to compare the actual values rather than the address, or is there some other container that I can use that’ll achieve the same result?

import java.util.*;
public class Main {
    public static void main(String args[]) {
        class Foo {
            public Foo(String a) {s = a;}
            public String s;
        }

        HashMap<Foo,Integer> a = new HashMap<Foo,Integer>();
        a.put(new Foo("test"), 1);
        System.out.println(a.get(new Foo("test")));
    }
}

This outputs null

  • 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-04T18:28:39+00:00Added an answer on June 4, 2026 at 6:28 pm

    In order to use instances of a class as keys in a HashMap you need to override its hashCode and equals methods. Once you do, everything should work fine.

    class Foo {
        public Foo(String a) {s = a;}
        public String s;
        int hashCode() {return s.hashCode();}
        boolean equals(Object other) {
            if (other == this) return true;
            if (!(other instanceof Foo)) return false;
            return ((Foo)other).s.equals(s);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create application which would display spatial data on Map. I'm thinking
I want to create a Map object that can lose value only if we
Let's assume I want to create a trait that I can mix in into
We want to use MapKit / MKOverlayView to create a map that looks totally
I want to create a territories map on Google Maps that follows zip code
I am using oozie to run my map-reduce job. I want to create the
I want create module which update list of usb devices automatically (not only mass
I want to create a class with a nested enum. public class Foo {
I want to create a stored procedure (on SQL Server 2005) that fetches a
I want to determine if a user has scrolled more than a certain percentage

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.