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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:52:36+00:00 2026-06-14T05:52:36+00:00

I have defined a simple private class named SetOb which contains an int and

  • 0

I have defined a simple private class named SetOb which contains an int and a Set data structure. I have a HashMap in the ‘main’ method with SetOb as Key and Integer as value. Now as you can see in the main method, when I feed the HashMap with a SetOb instance and then look for an instance with exactly the same value, it returns ‘null’. This has happened with me quite a few times before when I use my own defined data structures like SetOb as Key in HashMap. Can someone please point me what am I missing ?
Please note that in the constructor of SetOb class, I copy the Set passed as argument.

public class Solution {

    public static Solution sample = new Solution();
    private class SetOb {
        public int last;
        public Set<Integer> st;
        public SetOb(int l , Set<Integer> si ){
            last = l;
            st = new HashSet<Integer>(si);
        }
    }

    public static void main(String[] args) {
        Map<SetOb, Integer> m = new HashMap< SetOb, Integer>();
        Set<Integer> a = new HashSet<Integer>();

        for(int i =0; i<10; i++){
            a.add(i);
        }
        SetOb x = sample.new SetOb(100, a);
        SetOb y = sample.new SetOb(100, a);
        m.put(x,500);
        Integer val = m.get(y);
        if(val!= null) System.out.println("Success: " + val);
        else System.out.println("Failure");
    }

}
  • 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-14T05:52:37+00:00Added an answer on June 14, 2026 at 5:52 am

    Your x and y are not the same object instances hence contains is not able to match y against x, which ends up not finding the matching key/value in the Map.

    If you want the match to succeed, please implement(override) hasCode & equals method in SetOb which will compare the field values.

    Sample methods(Eclipse generated) as below:

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + last;
        result = prime * result + ((st == null) ? 0 : st.hashCode());
        return result;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SetOb other = (SetOb) obj;
        if (last != other.last)
            return false;
        if (st == null) {
            if (other.st != null)
                return false;
        } else if (!st.equals(other.st))
            return false;
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, the question is kinda simple. I have a object defined as: public class
I have a VERY simple 3D space defined in WPF, which defines a 3D
In my Hibernated system, I have a class Picture that has some simple data,
I have a simple entity structure defined in my project: Country > Region >
I have some slot function defined in my class which do some actions. I
Let's say we have a simple table defined as: <p:dataTable value=#{testBean.dummyStringData} var=data> <p:column> <p:commandLink
I have defined a fieldset in HTML, and applied the following (simple) CSS rules
I have a pretty simple UIScrollView defined inside Interface Builder. I've pasted in some
I have a very simple dialog defined as: import android.app.AlertDialog; import android.content.Context; import android.view.LayoutInflater;
It's a very simple program. I have a function defined on the top and

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.