当前位置:网站首页>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
*
*/
}
}


边栏推荐
猜你喜欢

mysql 性能分析

四、业务数据解析

小散量化炒股记|打包Py可执行文件,双击就能选出全市场稳步上扬的股票

Multi-scalar multiplication: state of the art & new ideas

Feign application and source code analysis

小程序使用formdata格式传参

oracle中联表相关思考

"Inversion of Control" and "Dependency Inversion", can't you tell the difference?

STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)

LVS负载均衡群集
随机推荐
IR(红外遥控)基本原理
LVS负载均衡群集
推荐100首好听英文歌
面试突击72:输入URL之后会执行什么流程?
COMSOL Multiphysics 6.0 software installation package and installation tutorial
VMWare Esxi 虚拟系统数据存储扩容(增加容量)的简明教程
一、用户数据仓库
数据库不推荐使用外键的9个理由!
2022世界机器人大会即将举办,智能机器人助推传统行业向智能化、数字化转型升级
centos 安装redis
巧用Prometheus来扩展kubernetes调度器
2022 - image classification 】 【 MaxViT ECCV
Detailed Explanation of Gradient Vanishing and Gradient Explosion Problems
Classification of software testing
"Inversion of Control" and "Dependency Inversion", can't you tell the difference?
【无标题】
docker部署redis容器问题
【Collection】3. Wallpaper collection
小程序使用formdata格式传参
机器学习模型太慢?来看看英特尔(R) 扩展加速