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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:54:19+00:00 2026-05-25T19:54:19+00:00

I have this which should not compile. public boolean foo(final Map<String, String> map) {

  • 0

I have this which should not compile.

public boolean foo(final Map<String, String> map) {
     map.get(1);   // No error here?
}
  • 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-25T19:54:20+00:00Added an answer on May 25, 2026 at 7:54 pm

    The get method takes an Object, so the 1 gets autoboxed to an Integer and it compiles just fine. You’ll always get null back as a response though.

    This is done for backwards compatability.

    // Ideal Map:
    public V get(K key) {
     // impl here
    }
    
    // what we have
    public V get(Object key) {
     // impl here
    }
    

    Example for scorpian

    import java.util.*;
    class ScorpMain {
    
        /* This interface just has a single method on it returning an int */
        static interface SomeInterface {
            int foo();
        }
    
        /**
         * ExampleKey has an int and a string. It considers itself to be equal to
         * another object if that object implements SomeInterface and the two have
         * equal foo values. It believes this is sufficient, as its sole purpose is
         * to calculate this foo value.
         */
        static class ExampleKey implements SomeInterface {
            int i;
            String s;
            ExampleKey(int i, String s) { this.i = i; this.s = s; }
            public int foo() { return i * s.length(); }
            public int hashCode() { return i ^ s.hashCode(); }
            // notice - equals takes Object, not ExampleKey
            public boolean equals(Object o) {
                if(o instanceof SomeInterface) {
                    SomeInterface so = (SomeInterface)o;
                    System.out.println(so.foo() + " " + foo());
                    return so.foo() == foo();
                }
                return false;
            }
        }
    
        /**
         * The ImposterKey stores it's foo and hash value. It has no calculation 
         * involved. Note that its only relation to ExampleKey is that they happen
         * to have SomeInterface.
         */
        static class ImposterKey implements SomeInterface {
            int foo;
            int hash;
            ImposterKey(int foo, int hash) { this.foo = foo; this.hash = hash; }
            public int foo() { return foo; }
            public boolean equals(Object o) {
                    SomeInterface so = (SomeInterface)o;
                    return so.foo() == foo();
            }
            public int hashCode() { return hash; }
        }
    
        /**
         * In our main method, we make a map. We put a key into the map. We get the
         * data from the map to prove we can get it out. Then we make an imposter, 
         * and get the data based on that. 
         * The moral of the story is, Map.get isn't sacred: if you can trick it 
         * into thinking that the object inside is equal to the object you give it 
         * in both equality and hash, it will give you the resulting object. It 
         * doesn't have anything to do with the type except that a given type is 
         * unlikely to be equal to another object that isn't of the given type.
         * This may seem like a contrived example, and it is, but it is something 
         * to be conscious of when dealing with maps. It's entirely possible you 
         * could get the same data and not realize what you're trying to do. Note 
         * you cannot ADD the imposter, because that IS checked at compile time.
         */
        public static void main(String[] args) {
            Map<ExampleKey,String> map = new HashMap<ExampleKey,String>();
            ExampleKey key = new ExampleKey(1337,"Hi");
            map.put(key,"Awesome!");
            String get = map.get(key);
            System.out.println(get); // we got awesome
            ImposterKey imposter = new ImposterKey(2674,3096); // make an imposter
            String fake = map.get(imposter);
            System.out.println(fake); // we got awesome again!
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a few lines of code which I think should not compile.
I have this code which should create a splash image with either no animation
Consider this problem: I have a program which should fetch (let's say) 100 records
I'm using ExtJS on a registration page which should have no effect on this.
I don't know which title I should use for this question. I have a
I am trying to construct method which returns a boolean: public boolean isStringValid(String s){
I have an existing app(not released public yet just internal) based on c++ which
I have this program here: namespace TodoPlus { using System.Diagnostics; public class LameProg {
I have this code: $li = $(li, this) Which is selecting all of the
I have this ListBox which is bound to an ObservableCollection. Each object in the

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.