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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:37:59+00:00 2026-05-17T15:37:59+00:00

I have a question. First, I declared a map: Map<TwoIntClass, Set<Integer>> m = new

  • 0

I have a question. First, I declared a map:

Map<TwoIntClass, Set<Integer>> m = new HashMap<TwoIntClass, Set<Integer>>();

Now, I want to put stuff inside this map, something like

int num = 7;
m.put(new TwoIntClass(5, 3), ?? how to put num inside Set ??);

My question is, how do I put variable num inside Set.
Thanks.

  • 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-17T15:37:59+00:00Added an answer on May 17, 2026 at 3:37 pm

    Like Jack and others have suggested, you need to instantiate a concrete instance of the Set interface (like HashSet), add your int values in the Set and then put the Set into your Map. However, if you are using a custom class for your Map‘s key, I would suggest you implement the equals() and hashCode() method of your TwoIntClass class to be sure that you are not creating duplicate entries inside your Map. For example, consider this class :

    public class TwoIntClass {
    
        private int i1;
        private int i2;
    
        public TwoIntClass(int i1, int i2) {
            this.i1 = i1;
            this.i2 = i2;   
        }
    
        static public void main(String...args) {
    
            Map<TwoIntClass, Set<Integer>> map = new HashMap<TwoIntClass, Set<Integer>>();
    
            Set<Integer> dataset = new HashSet<Integer>();
            dataset.add(1);
            dataset.add(2);
    
            TwoIntClass i1 = new TwoIntClass(5, 3);
            TwoIntClass i2 = new TwoIntClass(5, 3);
    
            map.put(i1, dataset);
            map.put(i2, dataset);
    
            System.out.println( i1.hashCode() + " = " + i2.hashCode() + " == " + i2.equals(i2) + " > map count = " + map.size() );
    
            TwoIntClass i3 = new TwoIntClass(5, 3);
            System.out.println("Looking for TwoIntClass(5,3)... " + map.containsKey(i3) );
    
        }
    }
    

    The output for executing it is :

    1476323068 = 535746438 == false > map
    count = 2

    Looking for TwoIntClass(5,3)… false

    As you see, they both are “equal” (i.e. they both are constructed with the same integers), but they are distinct objects with different hash codes, therefore create two entries in the map. This could lead into possible data corruption in your application. Moreover, executing this line : map.get(new TwoIntClass(5,3)).add(3); will generate a NullPointerException because the key (it’s hash) does not exist in the map. So, you need to implement the equals() and hashCode() methods to fix this, so any TwoIntClass constructed with the same integers will be considered equal. Something like :

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof TwoIntClass)) {
            return false;
        }
        TwoIntClass other = (TwoIntClass) obj;
    
        return (other.i1 == this.i1) && (other.i2 == this.i2); 
    }
    
    @Override
    public int hashCode() {
        //return TwoIntClass.class.hashCode() | i1 | (i2 << 16);
    
        // better implementation based on the String class
        int hash = TwoIntClass.class.hashCode();
    
        hash = (hash * 31) + i1;
        hash = (hash * 31) + i2;
    
        return hash;
    }
    

    yields a more expected result of

    1476323071 = 1476323071 == true > map count = 1

    Looking for TwoIntClass(5,3)… true

    Of course, this hashCode() method is over simplistic and you may need to find a yet better construct, but the bottom line is that implementing them is what I would recommend.

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

Sidebar

Related Questions

Here's my first question at SO. I have a internal application for my company
I'm creating my first real binary parser (a tiff reader) and have a question
Greetings to all! This is my first question here on stackoverflow. I have a
I have a simple question related to one-line programming. First an example: function test(a)
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
First of all many thanks for clicking into my question. I have a few
I have a Question class: class Question { public int QuestionNumber { get; set;
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
We have the question is there a performance difference between i++ and ++i in
We have a question with regards to XML-sig and need detail about the optional

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.