org.mstar.collection.Counter
这个类主要功能是记录放入其中的相同对象的个数(用equals()比较)
例如:
这个类的名字也许需要该以下。
还有什么需要改进的地方,或者还需要哪些方法,请大家提示。
实现如下:
这个类主要功能是记录放入其中的相同对象的个数(用equals()比较)
例如:
Counter c
=
new
Counter();
c.add( " One " );
c.add( " Two " );
c.add( " One " );
c. get ( " One " ) // Result is 2;
c. get ( " Two " ) // Result is 1;
c.add( " One " );
c.add( " Two " );
c.add( " One " );
c. get ( " One " ) // Result is 2;
c. get ( " Two " ) // Result is 1;
这个类的名字也许需要该以下。
还有什么需要改进的地方,或者还需要哪些方法,请大家提示。
实现如下:
1
package org.mstar.collection;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6
7
8 /**/ /**
9 * @author mty
10 *
11 */
12 public class Counter {
13 private Map map;
14
15 public Counter(){
16 map = new HashMap();
17 }
18
19 public void add(Object key){
20 if(map.containsKey(key)){
21 Integer i = (Integer)map.get(key);
22 int amount = i.intValue();
23 map.put(key,new Integer(++amount));
24 }else{
25 map.put(key,new Integer(1));
26 }
27 }
28
29 public void add(Object key,int amount){
30 if(map.containsKey(key)){
31 Integer i = (Integer)map.get(key);
32 int amount2 = i.intValue();
33 map.put(key,new Integer(amount+amount2));
34 }else{
35 map.put(key,new Integer(amount));
36 }
37 }
38
39 public Iterator iterator(){
40 return map.keySet().iterator();
41 }
42
43 public int getCount(Object key){
44 Integer i = (Integer)map.get(key);
45 return i.intValue();
46 }
47
48 public int[] countArray(){
49 int[] result = new int[map.size()];
50 Object[] keys = keyArray();
51 for(int i=0;i<result.length;i++){
52 result[i] = getCount(keys[i]);
53 }
54
55 return result;
56 }
57
58 public Object[] keyArray(){
59 return map.keySet().toArray();
60 }
61
62 public void remove(Object key){
63 if(map.containsKey(key)){
64 Integer i = (Integer)map.get(key);
65 int amount = i.intValue();
66 map.put(key,new Integer(amount<0?0:--amount));
67 }
68 }
69
70 public void remove(Object key,int amount){
71 if(map.containsKey(key)){
72 Integer i = (Integer)map.get(key);
73 int amount2 = i.intValue();
74 map.put(key,new Integer(amount2-amount<0?0:amount2-amount));
75 }
76 }
77
78 public int size(){
79 return map.size();
80 }
81}
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6
7
8 /**/ /**
9 * @author mty
10 *
11 */
12 public class Counter {
13 private Map map;
14
15 public Counter(){
16 map = new HashMap();
17 }
18
19 public void add(Object key){
20 if(map.containsKey(key)){
21 Integer i = (Integer)map.get(key);
22 int amount = i.intValue();
23 map.put(key,new Integer(++amount));
24 }else{
25 map.put(key,new Integer(1));
26 }
27 }
28
29 public void add(Object key,int amount){
30 if(map.containsKey(key)){
31 Integer i = (Integer)map.get(key);
32 int amount2 = i.intValue();
33 map.put(key,new Integer(amount+amount2));
34 }else{
35 map.put(key,new Integer(amount));
36 }
37 }
38
39 public Iterator iterator(){
40 return map.keySet().iterator();
41 }
42
43 public int getCount(Object key){
44 Integer i = (Integer)map.get(key);
45 return i.intValue();
46 }
47
48 public int[] countArray(){
49 int[] result = new int[map.size()];
50 Object[] keys = keyArray();
51 for(int i=0;i<result.length;i++){
52 result[i] = getCount(keys[i]);
53 }
54
55 return result;
56 }
57
58 public Object[] keyArray(){
59 return map.keySet().toArray();
60 }
61
62 public void remove(Object key){
63 if(map.containsKey(key)){
64 Integer i = (Integer)map.get(key);
65 int amount = i.intValue();
66 map.put(key,new Integer(amount<0?0:--amount));
67 }
68 }
69
70 public void remove(Object key,int amount){
71 if(map.containsKey(key)){
72 Integer i = (Integer)map.get(key);
73 int amount2 = i.intValue();
74 map.put(key,new Integer(amount2-amount<0?0:amount2-amount));
75 }
76 }
77
78 public int size(){
79 return map.size();
80 }
81}