In Java, the code for replacing the ordinal index with EnumMap is described in detail.

Source: Internet
Author: User

In Java, the code for replacing the ordinal index with EnumMap is described in detail.

This article focuses on the Content of replacing the ordinal index with EnumMap in Java.

Study NotesTive Java Chinese Version 2nd

The Enum ordinal method is often used to index enumeration types.

public class Herb {  public enum Type { ANNUAL, PERENNIAL, BIENNIAL };  private final String name;  private final Type type;  Herb(String name, Type type) {    this.name = name;    this.type = type;  }  @Override public String toString() {    return name;  }}

Now suppose there is an array of herbs that represents plants in a garden. You want to organize the plants by type (annual, perennial, or two-year-old) before listing them. To do this, you need to build three sets, one for each type, traverse the entire garden, and place each vanilla in the corresponding collection. Some programmers put these sets into an array indexed by the ordinal number of types to achieve this.

//Using ordinal() to index an array - DON'T DO THISHerb[] garden = ... ;//Indexed by Herb.Type.ordinal()Set<Herb>[] herbsByType = (Set<Herb>[])new Set[Herb.Type.values().length]; for(int i=0; i

This method is indeed feasible, but it hides many problems. Because arrays cannot be compatible with generics. The program must undergo unchecked conversions and cannot be compiled correctly. Because the array does not know what its index represents, you must manually mark the output of these indexes. However, the most serious problem with this method is that when you access an array indexed by the enumerated ordinal number, using the correct int value is your responsibility; int cannot provide Enumeration type security. If you use the wrong value, the program will quietly complete the wrong work, or if you are lucky, it will throwArrayIndexOutOfBoundExceptionException.

Java. util. EnumMap is a very fast Map implementation dedicated to enumeration keys.

//Using an EnumMap to associate data with an enumMap<Herb.Type, Set<Herb>> herbsByType = new EnumMap<Herb.Type, Set<Herb>>(Herb.Type.class);for(Herb.Type t : Herb.Type.values)  herbsByType.put(t, new HashSet<Herb>());for(Herb h : garden)  herbsByType.get(h.type).add(h);System.out.println(herbsByType);

This program is shorter, clearer, and safer. The running speed can be comparable to the number of programs used. It does not have unsafe conversions; you do not need to manually mark the output of these indexes, because the ing key knows how to translate itself into an enumeration of printable strings; it is impossible to make an error when calculating the Array Index. In terms of Running Speed, EnumMap can rival arrays indexed by ordinal numbers because EnumMap uses such arrays internally.However, it hides the details of this idea from programmers and combines the rich functions and types of Map security with arrays quickly. Note that the EnumMap constructor uses a key-type Class Object: This is a restricted type token (bounded type token), which provides runtime generic information.

Summary

The above is all the details about the code for replacing the ordinal index with EnumMap in Java. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.