当前位置:网站首页>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
*
*/
}
}
边栏推荐
猜你喜欢
随机推荐
Multi-scalar multiplication: state of the art & new ideas
VPP source address NAT
买股票用同花顺安全吗?资金会不会被转走?
oracle中联表相关思考
To make people's consumption safer, more assured and more satisfied
FreeSql 将 Saas 租户方案精简到极致[.NET ORM SAAS]
MySQL中的锁机制详解
业务缓存之体系化设计与开发
牛客收藏上万的神作!这份阿里P8手写的MySQL主从原理手册真的牛
使用分类权重,轻松解决数据不平衡的问题
Loadrunner12.0.2安装及中文语言包安装(汉化)
centos 安装redis
移动端/嵌入式-CV模型-2018:MobelNets-v2
记一次生产内存溢出分析解决
推荐100首好听英文歌
VPP静态映射实现DNAT
code inspection tool
深度解析网易严选和京东的会员体系,建议收藏
Classification of software testing
Mysql数据库架构介绍