"Effective Java" 9, using Enummap instead of ordinal index

Source: Internet
Author: User

 PackageCn.xf.cp.ch02.item33;ImportJava.util.EnumMap;ImportJava.util.HashSet;ImportJava.util.Map;ImportJava.util.Set;Importorg.junit.Test;/*** Function: Ordinal index * Time: 4:24:48 * File: Herbold.java *@authorAdministrator **/ Public classherbold{ Public enumType {Annual, perennial, biennial}PrivateString name; Privatetype type; /*** Package Area Permissions *@paramname *@paramtype*/Herbold (String name, type type) { This. Name =name;  This. Type =type; } @Override PublicString toString () {returnname; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicType GetType () {returntype; }     Public voidsetType (Type type) { This. Type =type; }    }

Use the results of the ordinal and Enummap respectively:

 PackageCn.xf.cp.ch02.item33;ImportJava.util.EnumMap;ImportJava.util.HashSet;ImportJava.util.Map;ImportJava.util.Set;Importorg.junit.Test;ImportCn.xf.cp.ch02.item33.HerbOld.Type; Public classherbtest{@Test Public voidtes1t () {herbold garden[]=        { NewHerbold ("Basil", type.annual),NewHerbold ("Carroway", type.biennial),NewHerbold ("Dill", type.annual),NewHerbold ("Lavendar", type.perennial),NewHerbold ("Parsley", type.biennial),NewHerbold ("Rosemary", Type.perennial)}; //using the map, the key storage type, the value holds the collection, the Enummap is specifically used for enumeration keys in the mapMapNewEnummapclass); //Create a collection of all types         for(Herbold.type t:herbold.type.values ()) Herbsbytype.put (T,NewHashset());  for(Herbold H:garden) Herbsbytype.get (H.gettype ()). Add (h);    System.out.println (Herbsbytype); }
  
@Test Public voidtest1 () {herbold garden[]= { NewHerbold ("Basil", type.annual),NewHerbold ("Carroway", type.biennial),NewHerbold ("Dill", type.annual),NewHerbold ("Lavendar", type.perennial),NewHerbold ("Parsley", type.biennial),NewHerbold ("Rosemary", Type.perennial)}; //when using the collection of this class, we have three kinds of this type, so we want to separate the set, three sets represent annual, perennial,//biennial three types//Create a relative number of arrayssetNewset[herbold.type.values (). length]; //the corresponding set type creates the corresponding set set for(inti = 0; i < herbsbytype.length; ++i) {Herbsbytype[i]=NewHashset(); } //determines the position of the corresponding type collection based on the location of the enumeration type and adds the data to it for(Herbold ho:garden) {//ordinal, this returns the position of the enumeration type at the time of declarationHerbsbytype[ho.gettype (). Ordinal ()].add (HO); } //Traverse for(inti = 0; i < herbsbytype.length; ++i) {//This is a running operation here. /*** But here's a note: When you access an index that is indexed by an enumerated ordinal, you know exactly what type of int represents the enum, and you have to think about it, * How to correctly use an int corresponding to the set type is the corresponding * While int does not provide security for enumerated types. If you use the wrong value, the program will silently complete the unexpected work .*/System.out.printf ("%s:%s%n", HerbOld.Type.values () [i], herbsbytype[i]); } }}

Ordinal:

Test1 ()

Using enummap,tes1t

"Effective Java" 9, using Enummap instead of ordinal index

Related Article

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.