The number of objects, equal objects (by equals () method is compared to the counter object () method is saved. You can get the number of one of the objects, or you can get the number of objects and numbers thereof. Can be used in multi-threaded environments. If there is anything unappropriate, please correct it. Import java.util. *; public final class counter {
Private hashtable data = new hashtable (); private set maxObject = collections.synchronizedset; private volatile int mapcount;
Public Boolean Contains (Object Object) {Return Data.Containskey (Object);
Public synchronized void add (object object) {int count = 0; if (contacts (object)) {count = count (object);} Data.put (Object, New Integer ( count); if (Maxcount <= Count) {if (Maxcount Public int count (object object) {return ((Integer) Data.Get (Object)). INTVALUE (); Public synchronized void clear () {data.clear (); maxObject.clear (); maxcount = 0; Public Object [] maxObject () {returnomObject.toarray (); Public synchronized int mapcount () {return map; Public string toString () {enumeration keys = data.keys (); enumeration value = data.elements (); string res = "; while (keys.hasmoreElements ()) {res = keys.nextelement (). Tostring "=" Values.nextelement (). Tostring () ",";} res = "maxObject = ["; item maxKeys = maxObject.iterator (); while (maxKeys.hasnext ()) res = MaxKeys.next (). Tostring () ","; if (res.indexof (",", ")> -1) res.substring (0, res.lend () - 1); res ="] Maxcount = " maxcount; return res.toT7tring ();}}