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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:57:50+00:00 2026-06-12T12:57:50+00:00

I have a Color class that I’m putting in the hashmap. I’d like to

  • 0

I have a Color class that I’m putting in the hashmap. I’d like to call containsKey on the hashmap to ensure whether the object is already present in the hashmap

Color class

public class Color {
  public String name;
  Color (String name) {this.name = name;}
  //getters setters for name 
}

HashMap

HashMap<Color, List<String>> m = new HashMap<Color, List<String>>();
Color c = new Color("red");
m.put(c, new ArrayList<String>());
Color c1 = new Color("red");
System.out.println(m.containsKey(c1)); //I'd like to return this as true

Since c1 has name red. I’d like the System.out to return true because the key already present in the map, c, has name red

How can this be achieved?

  • 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-12T12:57:52+00:00Added an answer on June 12, 2026 at 12:57 pm

    Your custom class Color should override equals() and hashcode() methods to achieve what you want.

    When you are using custom objects as keys for collections and would like to do lookup using object, then you should properly override equals() and hashcode() methods.

    Also read:

    Overriding equals and hashCode in Java

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

Sidebar

Related Questions

I have a base Color class that looks something like this. The class is
I have an IValueConverter class that is used to change the background color of
Possible Duplicate: Difference between Color.red and Color.RED I have seen that the Java class
I have a LinearLayout that I would like to change the background color of
I have a class that creates a matrix of Color values by reading a
I have a CSS class that makes my links look like nice looking buttons.
I have a css class that is called button.blue: button.blue { background-color: blue; ...
I have found this class that draws circles with different colors. The color of
I have a div in CSS that works like this: SomeDiv has another class,
I have that Class : public Class Color { public virtual int Id {

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.