Chapter 6: enumeration and annotation. Item30: Use Enum to replace int constants. & Item32: Use enumset to replace the bit field. & Item33: Use enummap to replace the ordinal index.

Source: Internet
Author: User
1 package COM. twoslow. cha6; 2 3 Import Java. util. hashmap; 4 Import Java. util. map; 5 6 Public Enum operation {7 8 plus ("+") {9 @ override10 double apply (Double X, Double Y) {11 return X + Y; 12} 13}, minus ("-") {14 @ override15 double apply (Double X, Double Y) {16 return x-y; 17} 18 }, times ("*") {19 @ override20 double apply (Double X, Double Y) {21 return x * Y; 22} 23}, divide ("/") {24 @ override25 do Uble apply (Double X, Double Y) {26 return x/y; 27} 28}; 29 30 Private Static final map <string, Operation> stringtoenum = new hashmap <string, operation> (); 31 32 // convert the custom string representation back to the corresponding enumeration. 33 static {34 for (Operation OP: values () {35 stringtoenum. put (op. tostring (), OP); 36} 37} 38 39 public static operation fromstring (string symbol) {40 return stringtoenum. get (Symbol); 41} 42 43 private final string symbol; 44 45 operation (string symbol) {46 this. symbol = symbol; 47} 48 49 Public String tostring () {50 return symbol; 51} 52 53 abstract double apply (Double X, Double Y); 54}
1 public class text {2 Public Enum style {bold, italic, underline, strikethrough} 3 Public void applystyles (set <style> styles ){...} 4 5} 6 // The usage is as follows: 7 text. applystyles (enumset. of (style. bold, style. italic ));

There are many plants in a garden. They are divided into three types: Annual, perennia, and biennia, which correspond to Herb. type.

What we need to do now is traverse every plant in the garden, divide these plants into three categories, and finally print the classification of the plants after classification:

 1 package com.twoslow.cha6; 2  3 public class Herb { 4     public enum Type { ANNUAL, PERENNIAL, BIENNIAL } 5     private final String name; 6     private final Type type; 7      8     Herb(String name, Type type) { 9         this.name = name;10         this.type = type;11     }12     13     @Override 14     public String toString() {15         return name;16     }17 }
 1 public static void main(String[] args) { 2         Herb[] garden = ...; 3         Map<Herb.Type, Set<Herb>> herbsByType =new EnumMap<Herb.Type, Set<Herb>>(Herb.Type.class); 4  5         for (Herb.Type t : Herb.Type.values()) { 6             herbsByType.put(t, new HashSet<Herb>()); 7         } 8         for (Herb h : garden) { 9             herbsByType.get(h.type).add(h);10         }11         System.out.println(herbsByType);12     }

 

Chapter 6: enumeration and annotation. Item30: Use Enum to replace int constants. & Item32: Use enumset to replace the bit field. & Item33: Use enummap to replace the ordinal index.

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.