当前位置:网站首页>String equals hashcode
String equals hashcode
2022-08-08 10:01:00 【WinkeyTseng_YongTai】
package equal;
/**
* 关于equals hashCode
*
* @author ZengWenFeng
* @mobile 13805029595
* @email [email protected]
*/
public class A
{
/** The value is used for character storage. */
private final char value[] = null;
/** Cache the hash code for the string */
private int hash; // Default to 0
private String code;
private String name;
public A(String code, String name)
{
this.code = code;
this.name = name;
}
public int hashCode()
{
int h = hash;
if (h == 0 && value.length > 0)
{
char val[] = value;
for (int i = 0; i < value.length; i++)
{
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
// public boolean equals(Object anObject)
// {
// if (this == anObject)
// {
// return true;
// }
//
// if (anObject instanceof String)
// {
// String anotherString = (String) anObject;
// int n = value.length;
// if (n == anotherString.value.length)
// {
// char v1[] = value;
// char v2[] = anotherString.value;
// int i = 0;
// while (n-- != 0)
// {
// if (v1[i] != v2[i])
// return false;
// i++;
// }
// return true;
// }
// }
//
// return false;
// }
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (this == obj)
{
return true;
}
if (this.getClass() != obj.getClass())
{
return false;
}
A a = (A) obj;
return code.equals(a.code) && name.equals(a.name);
}
public static void main(String[] args)
{
A obj1 = new A("005129", "zwf");
A obj2 = new A("005129", "zwf");
A obj3 = new A("005128", "zwf");
System.out.println(obj1.equals(obj2));
System.out.println(obj1.hashCode());
System.out.println(obj2.hashCode());
System.out.println("------");
System.out.println(obj1.equals(obj3));
System.out.println(obj1.hashCode());
System.out.println(obj3.hashCode());
System.out.println("------");
System.out.println("005129".hashCode());
System.out.println(new String("005129").hashCode());
System.out.println("005129".equals(new String("005129")));
/**
*
*
true
2018699554
1311053135
------
false
2018699554
118352462
------
1420155875
1420155875
true
*
*/
}
}
边栏推荐
猜你喜欢
随机推荐
d实验新异常
2万字50张图玩转Flink面试体系
Bytes and Characters and Common Encodings
使用.NET简单实现一个Redis的高性能克隆版(三)
使用C# 调用api接口获取法定节假日(百度api)
牛客收藏上万的神作!这份阿里P8手写的MySQL主从原理手册真的牛
五、业务数据分析
图像分割 总结
The entity List to excel
1252_FreeRTOS_堆栈溢出检查方法与测试
mysql 性能分析
Tensorflow基础概念
Recommend 100 nice English songs
01-MQ介绍以及产品比较
LVS负载均衡群集
LVS负载均衡群集及NAT模式群集
Excel method is commonly used in text function 5
移动端/嵌入式-CV模型-2018:MobileFaceNets
机器学习模型太慢?来看看英特尔(R) 扩展加速
hdu4635 Strongly connected(tarjan计算强连通通分量+缩点+思想)