[ThinkingInJava] 32. Recursive counting and thinkinginjava Recursion
/*** Book: Thinking In Java * function: to count pet, we provide a tool that can track the number of different types of Pet and use map. Here we use dynamic instanceof () * We can use Class. isAssignableFrom () is used to create a general counting tool not limited to Pet * file: PetCount3.java * Time: April 14, 2015 09:56:41 * Author: cutter_point */package net. mindview. util; import java. util. hashMap; import java. util. map; import Lesson14TypeInformation. pets. pet; public class TypeCounter extends HashMap <Class <?>, Integer> {private Class <?> BaseType; public TypeCounter (Class <?> BaseType) {this. baseType = baseType;} public void count (Object obj) {Class <?> Type = obj. getClass (); if (! BaseType. isAssignableFrom (type) // determines whether the type is set. {throw new RuntimeException (obj + "incorrect type:" + type + ", shocould be type or subtype of" + baseType );} this. countClass (type); // Add a type} private void countClass (Class <?> Type) {Integer quantity = this. get (type); // get the number of attributes of this type // Add one or add a class object this. put (type, quantity = null? 1: quantity + 1); // obtain the parent Class and reference the Class <?> SuperClass = type. getSuperclass (); if (superClass! = Null & baseType. isAssignableFrom (superClass) countClass (superClass); // recursion, the parent type will also add 1} public String toString () {StringBuilder result = new StringBuilder ("{"); for (Map. entry <Class <?>, Integer> pair: this. entrySet () // traverses all non-repeated objects {result. append (pair. getKey (). getSimpleName (); // class name result. append ("="); result. append (pair. getValue (); result. append (",");} result. delete (result. length ()-2, result. length (); // remove the last "," result. append ("}"); return result. toString ();}}
When a class comes in, we need to count it. But does this class count its parent class? The parent class should also be added with 1. What about the parent class of the parent class? Therefore, we use recursion, which is a layer-by-layer upward recursion until the base class is found, and then the base class is also 1.
/*** Book: Thinking In Java * function: to count pet, we provide a tool that can track the number of different types of Pet and use map. Here we use the dynamic instanceof () * file: PetCount4.java * Time: April 14, 2015 09:25:29 * Author: cutter_point */package Lesson14TypeInformation; import net. mindview. util. typeCounter; import static net. mindview. util. print. *; import Lesson14TypeInformation. pets. pet; import Lesson14TypeInformation. pets. pets; public class PetCount4 {public static void main (String args []) {TypeCounter counter = new TypeCounter (Pet. class); for (Pet pet: Pets. createArray (20) {printnb (pet. getClass (). getSimpleName () + ""); counter. count (pet) ;}print (); print (counter );}}
Output:
Rat-obj2 Manx-obj2 Cymric-obj2 Mutt-obj2 Pug-obj2 Cymric-obj2 Pug-obj2 Manx-obj2 Cymric-obj2 Rat-obj2 permanent-obj2 Hamster-obj2 permanent-obj2 Mutt -obj2 Mutt-obj2 Cymric-obj2 Mouse-obj2 Pug-obj2 Mouse-obj2 Cymric-obj2
{Dog = 6, Rodent = 5, Rat = 2, Hamster = 1, Pug = 3, Cymric = 5, Manx = 7, Cat = 9, Mutt = 3, pet = 20, Mouse = 2, EgyptianMau = 2} obj1