我正在查看源代码java.lang.String并注意到该equals方法没有检查char[]每个String 的后备是否是同一个对象.这不会改善比较时间吗?
包含在此重写版本中的改进:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
/** Begin Optimization **/
if(v1==v2 && i==j){
return true;
}
/** End Optimization **/
while (n-- != 0) {
if (v1[i++] != v2[j++]) …Run Code Online (Sandbox Code Playgroud) 我们目前有两个客户代码"CUSTA"和"CUSTB".CUSTA可以执行操作A1,A2,A3,A4,A5,A6.CUSTB可以根据某些条件执行操作B1,B2,B3,B4,B5,B6.目前他们不再期待更多客户代码,但我希望设计灵活.这些可以存储在数据库中,但正如我所提到的,因为很长一段时间不可能有另一个客户代码,它需要用代码表示.
应用程序逻辑基本算法看起来像
if ConditionX is true
then if customerCode is "CUSTA"
then applicableOperation = 'A1'
else
if customerCode is "CUSTB"
then applicableOperation = 'B1'
end
else
if ConditionY is true
then
if customerCode is "CUSTA"
then applicableOperation = 'A2'
else
if customerCode is "CUSTB"
then applicableOperation = 'B2'
end
else
Run Code Online (Sandbox Code Playgroud)
............... .................
我可以编写switch语句等来清理算法,但主要关注的是如何表示"CUSTA","CUSTB","A1","A2","A3","A4"......"A6" , "B1", "B2" ... "B6".客户代码是否像枚举一样
public enum CustomerCode { CUSTA, CUSTB }
public enum OperationsForA{ A1, A2, A3,...A6 }
public enum OperationsForB{ B1, B2, B3...B6}
Run Code Online (Sandbox Code Playgroud)
我应该创建一个Map …