-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountedString.java
More file actions
61 lines (55 loc) · 1.32 KB
/
Copy pathCountedString.java
File metadata and controls
61 lines (55 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package chapter17.nine.three;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* ¼ÆËãhashCode
* @author admin
*
*/
public class CountedString {
private static List<String> created=new ArrayList<String>();
private String s;
private int id=0;
public CountedString(String str) {
// TODO Auto-generated constructor stub
s=str;
created.add(s);
for(String s2:created){
if(s2.equals(s)){
id++;
}
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "String:"+s+"id:"+id+"hashCode():"+hashCode();
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
int result=17;
result=37*result+s.hashCode();
result=37*result+id;
return result;
}
public boolean equals(Object obj){
return obj instanceof CountedString && s.equals(((CountedString)obj).s) &&
id==((CountedString)obj).id;
}
public static void main(String[] args) {
Map<CountedString, Integer> map=new HashMap<CountedString, Integer>();
CountedString[] cs=new CountedString[5];
for (int i = 0; i < cs.length; i++) {
cs[i]=new CountedString("hi");
map.put(cs[i], i);
}
System.out.println(map);
for(CountedString cstring:cs){
System.out.println("Looking up"+cstring);
System.out.println(map.get(cstring));
}
}
}