33rd: Replace ordinal index with Enummap

Source: Internet
Author: User

Sometimes, the code that uses the ordinal method to index an array is met. For example, here is a simplified class that represents a cooking herb:

 Public classHerb { Public enumType {Annual, perennial, biennial}Private FinalString name; Private Finaltype type; Herb (String name, type type) { This. Name =name;  This. Type =type; } @Override PublicString toString () {returnname; }    }

Suppose there is an array of vanilla that represents a plant in a garden that is intended to be organized by type (annual, perennial, or biennial) to list the plants.

1. The collection is implemented in an array indexed by the ordinal of the type:

 Public Static voidMain (string[] args) {herb[] garden= {NewHerb ("A", Herb.Type.ANNUAL),NewHerb ("B", Herb.Type.BIENNIAL)}; SetNewset[herb.type.values (). length];  for(inti=0; i < herbsbytype.length; i++) Herbsbytype[i]=NewHashset();  for(Herb H:garden) herbsbytype[h.type.ordinal ()].add (h);  for(inti=0; i < herbsbytype.length; i++) System.out.printf ("%s:%s%n", Herb.Type.values () [i], herbsbytype[i]);}

This method works, but because the array is incompatible with generics, a non-inspected conversion is required.

2. Better method, the array actually acts as a mapping from enumeration to value, ENUMMAP provides such an implementation:

 Public Static voidMain (string[] args) {herb[] garden= {NewHerb ("A", Herb.Type.ANNUAL),NewHerb ("B", Herb.Type.BIENNIAL)}; MapNewEnummapclass);  for(Herb.type t:herb.type.values ()) Herbbytype.put (T,NewHashset());  for(Herb H:garden) herbbytype.get (H.type). Add (h); System.out.println (Herbbytype);}

There is no insecure type conversion.

You may also encounter an array of arrays that are indexed two times by ordinal

 Public enumPhase {SOLID, LIQUID, gas;  Public enumTransition {MELT, FREEZE, Botl, condense, SUBLIME, DEPOSIT; Private Static Finaltransition[][] Transitions = {            {NULL, MELT, SUBLIME}, {FREEZE,NULL, Botl}, {DEPOSIT, condense,NULL}        };  Public StaticTransition from (Phase src, Phase DST) {returntransitions[src.ordinal ()][dst.ordinal ()]; }    }}

You can do better with Enummap:

 Public enumPhase {SOLID, LIQUID, gas;  Public enumTransition {MELT (solid,liquid), FREEZE (LIQUID, SOLID), boil (LIQUID, gas), condense (gas, LIQUID),                SUBLIME (solid, gas), DEPOSIT (gas, solid); Private FinalPhase src; Private FinalPhase DST; Transition (Phase src, Phase DST) { This. src =src;  This. DST =DST; }                Private Static FinalMap<phase, map<phase, transition>> m =NewEnummap<phase, Map<phase, transition>> (Phase.class); Static {             for(Phase p:phase.values ()) M.put (p,NewEnummap<phase, Transition> (Phase.class));  for(Transition t:transition.values ()) M.get (T.SRC). Put (T.DST, t); }                 Public StaticTransition from (Phase src, Phase DST) {returnm.get (SRC). get (DST); }    }}

An array of square-sized sizes is not required.

If a new phase of plasma (ions) is added, only two transitions are associated with this phase, ionization, turning the gas into ions, eliminating ionization, and turning the ions into gases.

An array-based program, you must add a plasma constant to the phase, add two constants to Phase.transition, and replace the array of the original 9 elements with the new 16-element version, which is prone to error.

Based on the Enummap version, all you have to do is add PLASMA to the phase list and add ionize (gas, PLASMA), Deionize (PLASMA, gas) to the Phase.transition list, with little chance of error.

33rd: Replace ordinal index with Enummap

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.