Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use EnumMap in Java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use EnumMap in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use EnumMap in Java.

When it comes to the collection Map in Java, you may often use HashMap/ConcurrentHashMap, but when the key of Map is an enumerated type, have you ever thought of using EnumMap?

So what is the difference between key and HashMap when EnumMap is an enumerated type?

HashMap: use the Hash table to store, the underlying is the array, the array is stored in the entry object, the default length is 16. That is to say, every time an object is added to the Map, the hash value is first calculated according to the key value, and then it is placed in the appropriate position according to the length of the array. When there is a conflict, the "open chain method" is used, that is, the linked list is added at the conflict location; when Java8 solves the conflict, it adds a red-black tree solution. Generally speaking, HashMap is the way of exchanging space for time to improve the efficiency of access. But if we know all the values of key, is it necessary to calculate the hash value every time, then find the location, resolve the conflict, or even expand the capacity?

EnumMap: now that all the key are known, apply for an array of known size, locate to the specified location of the array according to the enumerated variable's value, and access it each time. This is not only efficient, but also does not waste the storage middle.

Next, let's explore the inner secrets of EnumMap.

Here the editor has built a front-end learning and exchange QQ group: 132667127, I have sorted out the latest front-end materials and advanced development tutorials, if you want, you can join the group to learn and communicate together.

Principal members:

Public class EnumMap extends AbstractMap

Implements java.io.Serializable, Cloneable {

/ / enumerated types of key values

Private final Class keyType

/ / all enumerated variables are pre-cached according to the enumeration type of the key value.

Private transient K [] keyUniverse

/ * *

* for all vlaue values, the subscript of the array is the serial number of the enumerated variable (ordinal)

* the content of the array is the vlaue corresponding to the enumerated variables in map

, /

Private transient Object [] vals

/ / current map size

Private transient int size = 0

}

Use detailed instructions:

Suppose such an Enum object is predetermined

Public enum WeekEnum {

SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY

}

Construction method:

/ / examples of the use of constructors

EnumMap weekEnumMap = new EnumMap (WeekEnum.class)

/ / all enumerated values are fetched in advance, and then according to the number of enumerated values, an array of corresponding size is created to store value.

Public EnumMap (Class keyType) {

This.keyType = keyType

KeyUniverse = getKeyUniverse (keyType)

Vals = new Object [keyUniverse.length]

}

Put method:

/ / check the enumerated value type, and then store the value in the array of the corresponding subscript according to the sequence number of the key

Public V put (K key, V value) {

TypeCheck (key)

Int index = key.ordinal ()

Object oldValue = vals [index]

Vals [index] = maskNull (value)

If (oldValue = = null)

Size++

Return unmaskNull (oldValue)

}

Get method:

/ / still check the key value type first, and then go to the location of the array according to the sequence number of key.

Public V get (Object key) {

Return (isValidKey (key)?

UnmaskNull (vals [(Enum) key) .ordinal ()]: null)

Thank you for reading. The above is the content of "how to use EnumMap in Java". After the study of this article, I believe you have a deeper understanding of how to use EnumMap in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report