-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountedString.java
More file actions
53 lines (45 loc) · 1.21 KB
/
CountedString.java
File metadata and controls
53 lines (45 loc) · 1.21 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
package btp.oneP;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CountedString {
private static List<String> created = new ArrayList<String>();
private String s;
private int id = 0;
public CountedString(String str){
s = str;
created.add(s);
for(String s2:created){
if(s2.equals(s)){
id++;
}
}
}
public int hashCode(){
int result = 17;
result = 37*result+s.hashCode();
result = 37*result+id;
return result;
}
public boolean equals(Object o){
return o instanceof CountedString && s.equals(((CountedString)o).s) && id==((CountedString)o).id;
}
@Override
public String toString() {
return "CountedString [s=" + s + ", id=" + id + "hashCode():"+hashCode()+"]";
}
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.print("Looking up "+cString);
System.out.println(map.get(cString));
}
}
}