OnJava8-Examples/containers/CountedString.java

69 lines
1.9 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: containers/CountedString.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// Creating a good hashCode().
import java.util.*;
import static net.mindview.util.Print.*;
public class CountedString {
private static List<String> created =
2015-05-05 11:20:13 -07:00
new ArrayList<>();
2015-04-20 15:36:01 -07:00
private String s;
private int id = 0;
public CountedString(String str) {
s = str;
created.add(s);
// id is the total number of instances
// of this string in use by CountedString:
for(String s2 : created)
if(s2.equals(s))
id++;
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public String toString() {
return "String: " + s + " id: " + id +
" hashCode(): " + hashCode();
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public int hashCode() {
// The very simple approach:
// return s.hashCode() * id;
// Using Joshua Bloch's recipe:
int result = 17;
result = 37 * result + s.hashCode();
result = 37 * result + id;
return result;
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public boolean equals(Object o) {
return o instanceof CountedString &&
s.equals(((CountedString)o).s) &&
id == ((CountedString)o).id;
}
public static void main(String[] args) {
Map<CountedString,Integer> map =
2015-05-05 11:20:13 -07:00
new HashMap<>();
2015-04-20 15:36:01 -07:00
CountedString[] cs = new CountedString[5];
for(int i = 0; i < cs.length; i++) {
cs[i] = new CountedString("hi");
2015-05-06 12:09:38 -07:00
map.put(cs[i], i); // Autobox int to Integer
2015-04-20 15:36:01 -07:00
}
print(map);
for(CountedString cstring : cs) {
print("Looking up " + cstring);
print(map.get(cstring));
}
}
} /* Output: (Sample)
{String: hi id: 4 hashCode(): 146450=3, String: hi id: 1 hashCode(): 146447=0, String: hi id: 3 hashCode(): 146449=2, String: hi id: 5 hashCode(): 146451=4, String: hi id: 2 hashCode(): 146448=1}
Looking up String: hi id: 1 hashCode(): 146447
0
Looking up String: hi id: 2 hashCode(): 146448
1
Looking up String: hi id: 3 hashCode(): 146449
2
Looking up String: hi id: 4 hashCode(): 146450
3
Looking up String: hi id: 5 hashCode(): 146451
4
*///:~