Why use EnumMap instead of HashMap
As we already have HashMap, why would we use EnumMap?
java hashmap enum-map
add a comment |
As we already have HashMap, why would we use EnumMap?
java hashmap enum-map
6
We already haveboolean, and digital computer programs are composed of ones and zeros... why do we need any other types?
– Elliott Frisch
Nov 21 '18 at 6:51
6
did you read the docs?
– richflow
Nov 21 '18 at 6:52
5
As the doc says This representation is extremely compact and efficient.
– Evgeni Enchev
Nov 21 '18 at 6:53
add a comment |
As we already have HashMap, why would we use EnumMap?
java hashmap enum-map
As we already have HashMap, why would we use EnumMap?
java hashmap enum-map
java hashmap enum-map
edited Nov 21 '18 at 7:01
Raedwald
26.5k2396157
26.5k2396157
asked Nov 21 '18 at 6:49
Ashish ChoudharyAshish Choudhary
175
175
6
We already haveboolean, and digital computer programs are composed of ones and zeros... why do we need any other types?
– Elliott Frisch
Nov 21 '18 at 6:51
6
did you read the docs?
– richflow
Nov 21 '18 at 6:52
5
As the doc says This representation is extremely compact and efficient.
– Evgeni Enchev
Nov 21 '18 at 6:53
add a comment |
6
We already haveboolean, and digital computer programs are composed of ones and zeros... why do we need any other types?
– Elliott Frisch
Nov 21 '18 at 6:51
6
did you read the docs?
– richflow
Nov 21 '18 at 6:52
5
As the doc says This representation is extremely compact and efficient.
– Evgeni Enchev
Nov 21 '18 at 6:53
6
6
We already have
boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?– Elliott Frisch
Nov 21 '18 at 6:51
We already have
boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?– Elliott Frisch
Nov 21 '18 at 6:51
6
6
did you read the docs?
– richflow
Nov 21 '18 at 6:52
did you read the docs?
– richflow
Nov 21 '18 at 6:52
5
5
As the doc says This representation is extremely compact and efficient.
– Evgeni Enchev
Nov 21 '18 at 6:53
As the doc says This representation is extremely compact and efficient.
– Evgeni Enchev
Nov 21 '18 at 6:53
add a comment |
2 Answers
2
active
oldest
votes
The Javadoc makes a pretty good argument:
Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.
Pretty much what I was copying :) I suggest OP looks into the source forEnumMapin order to see the shortcuts that can be taken when the entire "usage space" is known in advance (sinceenums are constant.)
– jensgram
Nov 21 '18 at 6:55
There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such asint, all for the purpose of efficiency.
– Thilo
Nov 21 '18 at 6:57
add a comment |
The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.
Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe
1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.
2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.
3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap
int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
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');
});
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%2f53406636%2fwhy-use-enummap-instead-of-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
The Javadoc makes a pretty good argument:
Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.
Pretty much what I was copying :) I suggest OP looks into the source forEnumMapin order to see the shortcuts that can be taken when the entire "usage space" is known in advance (sinceenums are constant.)
– jensgram
Nov 21 '18 at 6:55
There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such asint, all for the purpose of efficiency.
– Thilo
Nov 21 '18 at 6:57
add a comment |
The Javadoc makes a pretty good argument:
Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.
Pretty much what I was copying :) I suggest OP looks into the source forEnumMapin order to see the shortcuts that can be taken when the entire "usage space" is known in advance (sinceenums are constant.)
– jensgram
Nov 21 '18 at 6:55
There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such asint, all for the purpose of efficiency.
– Thilo
Nov 21 '18 at 6:57
add a comment |
The Javadoc makes a pretty good argument:
Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.
The Javadoc makes a pretty good argument:
Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.
answered Nov 21 '18 at 6:53
ThiloThilo
196k78422575
196k78422575
Pretty much what I was copying :) I suggest OP looks into the source forEnumMapin order to see the shortcuts that can be taken when the entire "usage space" is known in advance (sinceenums are constant.)
– jensgram
Nov 21 '18 at 6:55
There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such asint, all for the purpose of efficiency.
– Thilo
Nov 21 '18 at 6:57
add a comment |
Pretty much what I was copying :) I suggest OP looks into the source forEnumMapin order to see the shortcuts that can be taken when the entire "usage space" is known in advance (sinceenums are constant.)
– jensgram
Nov 21 '18 at 6:55
There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such asint, all for the purpose of efficiency.
– Thilo
Nov 21 '18 at 6:57
Pretty much what I was copying :) I suggest OP looks into the source for
EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)– jensgram
Nov 21 '18 at 6:55
Pretty much what I was copying :) I suggest OP looks into the source for
EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)– jensgram
Nov 21 '18 at 6:55
There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as
int, all for the purpose of efficiency.– Thilo
Nov 21 '18 at 6:57
There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as
int, all for the purpose of efficiency.– Thilo
Nov 21 '18 at 6:57
add a comment |
The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.
Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe
1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.
2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.
3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap
int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
add a comment |
The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.
Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe
1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.
2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.
3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap
int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
add a comment |
The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.
Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe
1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.
2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.
3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap
int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.
Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe
1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.
2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.
3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap
int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
answered Nov 21 '18 at 6:53
Pooja AggarwalPooja Aggarwal
870211
870211
add a comment |
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');
});
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%2f53406636%2fwhy-use-enummap-instead-of-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');
});
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');
});
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');
});
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
6
We already have
boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?– Elliott Frisch
Nov 21 '18 at 6:51
6
did you read the docs?
– richflow
Nov 21 '18 at 6:52
5
As the doc says This representation is extremely compact and efficient.
– Evgeni Enchev
Nov 21 '18 at 6:53