Restricting the Object Type for the get method in java HashMap Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Does a finally block always get executed in Java?Fastest way to determine if an integer's square root is an integerA Java collection of value pairs? (tuples?)How to get an enum value from a string value in Java?Java Hashmap: How to get key from value?get string value from HashMap depending on key nameHow to update a value, given a key in a java hashmap?java.lang.OutOfMemoryError: GC overhead limit exceededConvert object array to hash map, indexed by an attribute value of the ObjectRestrict HashMap to accept a particular string key
What helicopter has the most rotor blades?
How to resize main filesystem
Why are current probes so expensive?
Vertical ranges of Column Plots in 12
What does Sonny Burch mean by, "S.H.I.E.L.D. and HYDRA don't even exist anymore"?
By what mechanism was the 2017 UK General Election called?
What should one know about term logic before studying propositional and predicate logic?
Did pre-Columbian Americans know the spherical shape of the Earth?
How does Billy Russo acquire his 'Jigsaw' mask?
Is there a canonical “inverse” of Abelianization?
Did John Wesley plagiarize Matthew Henry...?
Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?
Proving that any solution to the differential equation of an oscillator can be written as a sum of sinusoids.
Searching extreme points of polyhedron
Any stored/leased 737s that could substitute for grounded MAXs?
Keep at all times, the minus sign above aligned with minus sign below
Where did Ptolemy compare the Earth to the distance of fixed stars?
Improvising over quartal voicings
An isoperimetric-type inequality inside a cube
How to ask rejected full-time candidates to apply to teach individual courses?
malloc in main() or malloc in another function: allocating memory for a struct and its members
How do Java 8 default methods hеlp with lambdas?
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
Table formatting with tabularx?
Restricting the Object Type for the get method in java HashMap
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Does a finally block always get executed in Java?Fastest way to determine if an integer's square root is an integerA Java collection of value pairs? (tuples?)How to get an enum value from a string value in Java?Java Hashmap: How to get key from value?get string value from HashMap depending on key nameHow to update a value, given a key in a java hashmap?java.lang.OutOfMemoryError: GC overhead limit exceededConvert object array to hash map, indexed by an attribute value of the ObjectRestrict HashMap to accept a particular string key
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have instantiated my HashMap like this:
Map<String, Integer> myHashMap = new HashMap<String, Integer>();
The datatype of the Key
is String, so when I try to insert a new key-value pair in the map keeping the Key
as Integer, it throws an error.
myHashMap.put(1L, "value");
That means in the put method they have restricted the datatype of the Key
. But while fetching the value from the map using the get
method it is not checking for the datatype of the Key
. So if I write something like this, it doesn't give a compilation error.
myHashMap.get(1L);
I checked the get
method in the Java Map interface and its parameter type is Object
, so thats why it is allowing any Object as the put method argument.
V get(Object key)
My question is, is there any way I can restrict the datatype which I pass as an argument in the get
method? The argument that I pass should have the same datatype as the datatype of the Key
which I use while instantiating my hashmap.
java hashmap
add a comment |
I have instantiated my HashMap like this:
Map<String, Integer> myHashMap = new HashMap<String, Integer>();
The datatype of the Key
is String, so when I try to insert a new key-value pair in the map keeping the Key
as Integer, it throws an error.
myHashMap.put(1L, "value");
That means in the put method they have restricted the datatype of the Key
. But while fetching the value from the map using the get
method it is not checking for the datatype of the Key
. So if I write something like this, it doesn't give a compilation error.
myHashMap.get(1L);
I checked the get
method in the Java Map interface and its parameter type is Object
, so thats why it is allowing any Object as the put method argument.
V get(Object key)
My question is, is there any way I can restrict the datatype which I pass as an argument in the get
method? The argument that I pass should have the same datatype as the datatype of the Key
which I use while instantiating my hashmap.
java hashmap
1
No. But good IDEs will warn you when doing that. And you can encapsulate the map into your own class.
– JB Nizet
1 hour ago
In other languages you could create extension method, likegetTyped...
. In Java you can do this with static method, so your call will begetTyped(map, key)
. Ugly, but works.
– dyukha
1 hour ago
I have considered the option of creating a wrapper method over theget
method, something like this:Integer getMapValue(Map map, String Key) return map.get(key)
And call this method instead of the get method, but I wanted to know if Java provides any such restricting feature of not?
– Rito
1 hour ago
You can add an implementation of aget(K key)
method, but It seems like an unnecessary effort. The return value for a mismatching key (type-wise) will be null, as expected. So the Map will conform to its API.
– Rann Lifshitz
1 hour ago
add a comment |
I have instantiated my HashMap like this:
Map<String, Integer> myHashMap = new HashMap<String, Integer>();
The datatype of the Key
is String, so when I try to insert a new key-value pair in the map keeping the Key
as Integer, it throws an error.
myHashMap.put(1L, "value");
That means in the put method they have restricted the datatype of the Key
. But while fetching the value from the map using the get
method it is not checking for the datatype of the Key
. So if I write something like this, it doesn't give a compilation error.
myHashMap.get(1L);
I checked the get
method in the Java Map interface and its parameter type is Object
, so thats why it is allowing any Object as the put method argument.
V get(Object key)
My question is, is there any way I can restrict the datatype which I pass as an argument in the get
method? The argument that I pass should have the same datatype as the datatype of the Key
which I use while instantiating my hashmap.
java hashmap
I have instantiated my HashMap like this:
Map<String, Integer> myHashMap = new HashMap<String, Integer>();
The datatype of the Key
is String, so when I try to insert a new key-value pair in the map keeping the Key
as Integer, it throws an error.
myHashMap.put(1L, "value");
That means in the put method they have restricted the datatype of the Key
. But while fetching the value from the map using the get
method it is not checking for the datatype of the Key
. So if I write something like this, it doesn't give a compilation error.
myHashMap.get(1L);
I checked the get
method in the Java Map interface and its parameter type is Object
, so thats why it is allowing any Object as the put method argument.
V get(Object key)
My question is, is there any way I can restrict the datatype which I pass as an argument in the get
method? The argument that I pass should have the same datatype as the datatype of the Key
which I use while instantiating my hashmap.
java hashmap
java hashmap
edited 1 hour ago
Rann Lifshitz
3,17541435
3,17541435
asked 1 hour ago
RitoRito
1,086718
1,086718
1
No. But good IDEs will warn you when doing that. And you can encapsulate the map into your own class.
– JB Nizet
1 hour ago
In other languages you could create extension method, likegetTyped...
. In Java you can do this with static method, so your call will begetTyped(map, key)
. Ugly, but works.
– dyukha
1 hour ago
I have considered the option of creating a wrapper method over theget
method, something like this:Integer getMapValue(Map map, String Key) return map.get(key)
And call this method instead of the get method, but I wanted to know if Java provides any such restricting feature of not?
– Rito
1 hour ago
You can add an implementation of aget(K key)
method, but It seems like an unnecessary effort. The return value for a mismatching key (type-wise) will be null, as expected. So the Map will conform to its API.
– Rann Lifshitz
1 hour ago
add a comment |
1
No. But good IDEs will warn you when doing that. And you can encapsulate the map into your own class.
– JB Nizet
1 hour ago
In other languages you could create extension method, likegetTyped...
. In Java you can do this with static method, so your call will begetTyped(map, key)
. Ugly, but works.
– dyukha
1 hour ago
I have considered the option of creating a wrapper method over theget
method, something like this:Integer getMapValue(Map map, String Key) return map.get(key)
And call this method instead of the get method, but I wanted to know if Java provides any such restricting feature of not?
– Rito
1 hour ago
You can add an implementation of aget(K key)
method, but It seems like an unnecessary effort. The return value for a mismatching key (type-wise) will be null, as expected. So the Map will conform to its API.
– Rann Lifshitz
1 hour ago
1
1
No. But good IDEs will warn you when doing that. And you can encapsulate the map into your own class.
– JB Nizet
1 hour ago
No. But good IDEs will warn you when doing that. And you can encapsulate the map into your own class.
– JB Nizet
1 hour ago
In other languages you could create extension method, like
getTyped...
. In Java you can do this with static method, so your call will be getTyped(map, key)
. Ugly, but works.– dyukha
1 hour ago
In other languages you could create extension method, like
getTyped...
. In Java you can do this with static method, so your call will be getTyped(map, key)
. Ugly, but works.– dyukha
1 hour ago
I have considered the option of creating a wrapper method over the
get
method, something like this: Integer getMapValue(Map map, String Key) return map.get(key)
And call this method instead of the get method, but I wanted to know if Java provides any such restricting feature of not?– Rito
1 hour ago
I have considered the option of creating a wrapper method over the
get
method, something like this: Integer getMapValue(Map map, String Key) return map.get(key)
And call this method instead of the get method, but I wanted to know if Java provides any such restricting feature of not?– Rito
1 hour ago
You can add an implementation of a
get(K key)
method, but It seems like an unnecessary effort. The return value for a mismatching key (type-wise) will be null, as expected. So the Map will conform to its API.– Rann Lifshitz
1 hour ago
You can add an implementation of a
get(K key)
method, but It seems like an unnecessary effort. The return value for a mismatching key (type-wise) will be null, as expected. So the Map will conform to its API.– Rann Lifshitz
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
It is designed that way, since during the get operation only the equals
and hashCode
is used to determine the object to be returned. The implementation of the get
method does not check for the type of the Object used as the key.
In your example you are trying to get the value by passing a long like myHashMap.get(1L);
, firstly the hash code of the object Long
having the value 1L
will be used to determine the bucket from which to look for. Next the equals
method of the key is used to find out the exact entry of the map from which to return the value. And in a well-defined equals
method there is always a check for the type:
public boolean equals(Object obj)
if (obj instanceof Long) //here type is checked
return value == ((Long)obj).longValue();
return false;
So if the types are not equal, the equals
method returns false
and hence get
also will return null
.
In some cases such as when using List
as a key, it may happen that you put an item in the map using an instance of say an ArrayList
but you can successfully retrieve the same value with an instance of an LinkedList
. As both implement the List
interface.
Map<List<String>, String> myHashMap = new HashMap<>();
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
myHashMap.put(arrayList, "foo");
System.out.println(myHashMap.get(linkedList));
The above code will output in the console foo
.
Here although the implementations are different but if you examine the equals
method of ArrayList
, it is only checking if the type is a List
:
public boolean equals(Object o)
if (o == this)
return true;
if (!(o instanceof List)) //checking type of super interface
return false;
...
The same is true for LinkedList
.
1
Kudos. That's the type of answer I expect for such an (IMHO) interesting question.
– Rann Lifshitz
1 hour ago
@RannLifshitz Thank you!
– Amardeep Bhowmick
1 hour ago
1
This is a good example of how an answer should be written in StackOverflow - well formulated, informative, with to-the-point code examples. Well done!
– Rann Lifshitz
57 mins ago
add a comment |
I think if it is very important in a project that we control type in HashMap, we could extends HashMap and force using this class instead of HashMap like below code.
we have all HashMap capabilities, just we should use getValue method instead of get method.
import java.util.HashMap;
public class MyHashMap<K,V> extends HashMap<K,V>
public V getValue(K key)
return super.get(key);
test class :
public class Test
public static void main(String[] args)
MyHashMap<String,Integer> map = new MyHashMap();
1
A similar answer was posted maybe an hour ago and was then deleted by its poster...........
– Rann Lifshitz
1 hour ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55790456%2frestricting-the-object-type-for-the-get-method-in-java-hashmap%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is designed that way, since during the get operation only the equals
and hashCode
is used to determine the object to be returned. The implementation of the get
method does not check for the type of the Object used as the key.
In your example you are trying to get the value by passing a long like myHashMap.get(1L);
, firstly the hash code of the object Long
having the value 1L
will be used to determine the bucket from which to look for. Next the equals
method of the key is used to find out the exact entry of the map from which to return the value. And in a well-defined equals
method there is always a check for the type:
public boolean equals(Object obj)
if (obj instanceof Long) //here type is checked
return value == ((Long)obj).longValue();
return false;
So if the types are not equal, the equals
method returns false
and hence get
also will return null
.
In some cases such as when using List
as a key, it may happen that you put an item in the map using an instance of say an ArrayList
but you can successfully retrieve the same value with an instance of an LinkedList
. As both implement the List
interface.
Map<List<String>, String> myHashMap = new HashMap<>();
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
myHashMap.put(arrayList, "foo");
System.out.println(myHashMap.get(linkedList));
The above code will output in the console foo
.
Here although the implementations are different but if you examine the equals
method of ArrayList
, it is only checking if the type is a List
:
public boolean equals(Object o)
if (o == this)
return true;
if (!(o instanceof List)) //checking type of super interface
return false;
...
The same is true for LinkedList
.
1
Kudos. That's the type of answer I expect for such an (IMHO) interesting question.
– Rann Lifshitz
1 hour ago
@RannLifshitz Thank you!
– Amardeep Bhowmick
1 hour ago
1
This is a good example of how an answer should be written in StackOverflow - well formulated, informative, with to-the-point code examples. Well done!
– Rann Lifshitz
57 mins ago
add a comment |
It is designed that way, since during the get operation only the equals
and hashCode
is used to determine the object to be returned. The implementation of the get
method does not check for the type of the Object used as the key.
In your example you are trying to get the value by passing a long like myHashMap.get(1L);
, firstly the hash code of the object Long
having the value 1L
will be used to determine the bucket from which to look for. Next the equals
method of the key is used to find out the exact entry of the map from which to return the value. And in a well-defined equals
method there is always a check for the type:
public boolean equals(Object obj)
if (obj instanceof Long) //here type is checked
return value == ((Long)obj).longValue();
return false;
So if the types are not equal, the equals
method returns false
and hence get
also will return null
.
In some cases such as when using List
as a key, it may happen that you put an item in the map using an instance of say an ArrayList
but you can successfully retrieve the same value with an instance of an LinkedList
. As both implement the List
interface.
Map<List<String>, String> myHashMap = new HashMap<>();
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
myHashMap.put(arrayList, "foo");
System.out.println(myHashMap.get(linkedList));
The above code will output in the console foo
.
Here although the implementations are different but if you examine the equals
method of ArrayList
, it is only checking if the type is a List
:
public boolean equals(Object o)
if (o == this)
return true;
if (!(o instanceof List)) //checking type of super interface
return false;
...
The same is true for LinkedList
.
1
Kudos. That's the type of answer I expect for such an (IMHO) interesting question.
– Rann Lifshitz
1 hour ago
@RannLifshitz Thank you!
– Amardeep Bhowmick
1 hour ago
1
This is a good example of how an answer should be written in StackOverflow - well formulated, informative, with to-the-point code examples. Well done!
– Rann Lifshitz
57 mins ago
add a comment |
It is designed that way, since during the get operation only the equals
and hashCode
is used to determine the object to be returned. The implementation of the get
method does not check for the type of the Object used as the key.
In your example you are trying to get the value by passing a long like myHashMap.get(1L);
, firstly the hash code of the object Long
having the value 1L
will be used to determine the bucket from which to look for. Next the equals
method of the key is used to find out the exact entry of the map from which to return the value. And in a well-defined equals
method there is always a check for the type:
public boolean equals(Object obj)
if (obj instanceof Long) //here type is checked
return value == ((Long)obj).longValue();
return false;
So if the types are not equal, the equals
method returns false
and hence get
also will return null
.
In some cases such as when using List
as a key, it may happen that you put an item in the map using an instance of say an ArrayList
but you can successfully retrieve the same value with an instance of an LinkedList
. As both implement the List
interface.
Map<List<String>, String> myHashMap = new HashMap<>();
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
myHashMap.put(arrayList, "foo");
System.out.println(myHashMap.get(linkedList));
The above code will output in the console foo
.
Here although the implementations are different but if you examine the equals
method of ArrayList
, it is only checking if the type is a List
:
public boolean equals(Object o)
if (o == this)
return true;
if (!(o instanceof List)) //checking type of super interface
return false;
...
The same is true for LinkedList
.
It is designed that way, since during the get operation only the equals
and hashCode
is used to determine the object to be returned. The implementation of the get
method does not check for the type of the Object used as the key.
In your example you are trying to get the value by passing a long like myHashMap.get(1L);
, firstly the hash code of the object Long
having the value 1L
will be used to determine the bucket from which to look for. Next the equals
method of the key is used to find out the exact entry of the map from which to return the value. And in a well-defined equals
method there is always a check for the type:
public boolean equals(Object obj)
if (obj instanceof Long) //here type is checked
return value == ((Long)obj).longValue();
return false;
So if the types are not equal, the equals
method returns false
and hence get
also will return null
.
In some cases such as when using List
as a key, it may happen that you put an item in the map using an instance of say an ArrayList
but you can successfully retrieve the same value with an instance of an LinkedList
. As both implement the List
interface.
Map<List<String>, String> myHashMap = new HashMap<>();
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
myHashMap.put(arrayList, "foo");
System.out.println(myHashMap.get(linkedList));
The above code will output in the console foo
.
Here although the implementations are different but if you examine the equals
method of ArrayList
, it is only checking if the type is a List
:
public boolean equals(Object o)
if (o == this)
return true;
if (!(o instanceof List)) //checking type of super interface
return false;
...
The same is true for LinkedList
.
edited 1 hour ago
answered 1 hour ago
Amardeep BhowmickAmardeep Bhowmick
6,30121230
6,30121230
1
Kudos. That's the type of answer I expect for such an (IMHO) interesting question.
– Rann Lifshitz
1 hour ago
@RannLifshitz Thank you!
– Amardeep Bhowmick
1 hour ago
1
This is a good example of how an answer should be written in StackOverflow - well formulated, informative, with to-the-point code examples. Well done!
– Rann Lifshitz
57 mins ago
add a comment |
1
Kudos. That's the type of answer I expect for such an (IMHO) interesting question.
– Rann Lifshitz
1 hour ago
@RannLifshitz Thank you!
– Amardeep Bhowmick
1 hour ago
1
This is a good example of how an answer should be written in StackOverflow - well formulated, informative, with to-the-point code examples. Well done!
– Rann Lifshitz
57 mins ago
1
1
Kudos. That's the type of answer I expect for such an (IMHO) interesting question.
– Rann Lifshitz
1 hour ago
Kudos. That's the type of answer I expect for such an (IMHO) interesting question.
– Rann Lifshitz
1 hour ago
@RannLifshitz Thank you!
– Amardeep Bhowmick
1 hour ago
@RannLifshitz Thank you!
– Amardeep Bhowmick
1 hour ago
1
1
This is a good example of how an answer should be written in StackOverflow - well formulated, informative, with to-the-point code examples. Well done!
– Rann Lifshitz
57 mins ago
This is a good example of how an answer should be written in StackOverflow - well formulated, informative, with to-the-point code examples. Well done!
– Rann Lifshitz
57 mins ago
add a comment |
I think if it is very important in a project that we control type in HashMap, we could extends HashMap and force using this class instead of HashMap like below code.
we have all HashMap capabilities, just we should use getValue method instead of get method.
import java.util.HashMap;
public class MyHashMap<K,V> extends HashMap<K,V>
public V getValue(K key)
return super.get(key);
test class :
public class Test
public static void main(String[] args)
MyHashMap<String,Integer> map = new MyHashMap();
1
A similar answer was posted maybe an hour ago and was then deleted by its poster...........
– Rann Lifshitz
1 hour ago
add a comment |
I think if it is very important in a project that we control type in HashMap, we could extends HashMap and force using this class instead of HashMap like below code.
we have all HashMap capabilities, just we should use getValue method instead of get method.
import java.util.HashMap;
public class MyHashMap<K,V> extends HashMap<K,V>
public V getValue(K key)
return super.get(key);
test class :
public class Test
public static void main(String[] args)
MyHashMap<String,Integer> map = new MyHashMap();
1
A similar answer was posted maybe an hour ago and was then deleted by its poster...........
– Rann Lifshitz
1 hour ago
add a comment |
I think if it is very important in a project that we control type in HashMap, we could extends HashMap and force using this class instead of HashMap like below code.
we have all HashMap capabilities, just we should use getValue method instead of get method.
import java.util.HashMap;
public class MyHashMap<K,V> extends HashMap<K,V>
public V getValue(K key)
return super.get(key);
test class :
public class Test
public static void main(String[] args)
MyHashMap<String,Integer> map = new MyHashMap();
I think if it is very important in a project that we control type in HashMap, we could extends HashMap and force using this class instead of HashMap like below code.
we have all HashMap capabilities, just we should use getValue method instead of get method.
import java.util.HashMap;
public class MyHashMap<K,V> extends HashMap<K,V>
public V getValue(K key)
return super.get(key);
test class :
public class Test
public static void main(String[] args)
MyHashMap<String,Integer> map = new MyHashMap();
answered 1 hour ago
hamid rostamihamid rostami
467
467
1
A similar answer was posted maybe an hour ago and was then deleted by its poster...........
– Rann Lifshitz
1 hour ago
add a comment |
1
A similar answer was posted maybe an hour ago and was then deleted by its poster...........
– Rann Lifshitz
1 hour ago
1
1
A similar answer was posted maybe an hour ago and was then deleted by its poster...........
– Rann Lifshitz
1 hour ago
A similar answer was posted maybe an hour ago and was then deleted by its poster...........
– Rann Lifshitz
1 hour ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55790456%2frestricting-the-object-type-for-the-get-method-in-java-hashmap%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
No. But good IDEs will warn you when doing that. And you can encapsulate the map into your own class.
– JB Nizet
1 hour ago
In other languages you could create extension method, like
getTyped...
. In Java you can do this with static method, so your call will begetTyped(map, key)
. Ugly, but works.– dyukha
1 hour ago
I have considered the option of creating a wrapper method over the
get
method, something like this:Integer getMapValue(Map map, String Key) return map.get(key)
And call this method instead of the get method, but I wanted to know if Java provides any such restricting feature of not?– Rito
1 hour ago
You can add an implementation of a
get(K key)
method, but It seems like an unnecessary effort. The return value for a mismatching key (type-wise) will be null, as expected. So the Map will conform to its API.– Rann Lifshitz
1 hour ago