当前位置: 首页 > news >正文

后台管理网站建设网站优化外包费用

后台管理网站建设,网站优化外包费用,服务器做网站,网页编辑面试知识在 java.lang.Object 类中有两个非常重要的方法: public native int hashCode(); public boolean equals(Object obj) {return (this obj); }Object 类是类继承结构的基础,是每一个类的父类,都实现了Object 类中定义的方法。 equals()方法…

在 java.lang.Object 类中有两个非常重要的方法:

public native int hashCode();
public boolean equals(Object obj) {return (this == obj);
}

Object 类是类继承结构的基础,是每一个类的父类,都实现了Object 类中定义的方法。

equals()方法:

equals()方法是用作对象之间进行比较的,判断是否相等。
在 Object 类中 equals() 方法是 == 对两个对象之间的地址值进行的比较(引用是否相同)。
String 封装类中在使用 equals() 方法时,已经重写了父类 Object 的 equals() 方法。

public boolean equals(Object anObject) {// 查看地址值是否相同,是否为同一引用if (this == anObject) {return true;}// 是否是String 类型if (anObject instanceof String) {// 强转为 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;
}

equals() 方法拥有的特性:
x 和 y 都不为 null

  1. 自反性:x.equals(x) 一定为 true
  2. 一致性:如果 x,y 的信息没有被修改过,x.equals(y) 被多次调用也会返回相同的值
  3. 对称性:x.equals(y) = true 则 y.equals(x) = true
  4. 传递性:x.equals(y) = true ,y.equals(z) = true 则 x.equals(z) 一定为 true
  5. 对于 x.equals(null) 一定返回 false

当 equals() 方法被重写的时候,hashCode() 方法一定要重写,因为两个相同对象的 hashCode 一定相同。

hashCode()方法:

hashCode() 方法给对象返回一个 hashCode 值。这个方法被用于 hash tables,例如 HashMap

hashCode() 方法拥有的性质:

  1. 在一个Java程序运行期间,如果一个对象提供给 equals() 作比较的信息没有变化的话,该对象多次调用 hashCode() 方法,返回的应该是同一个Integer。
  2. 如果两个对象的 equals() 结果是 true。则两个对象的 hashCode 值一定相同。
  3. 并不是两个不相同的对象返回的 hashCode 值一定不同。

在String中定义的hashCode()方法:

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;
}

在Java中有两种集合(List 和 Set),List 有序可重复的,Set 无序不可重复的。

Set中要想保证数据不重复,根据什么来判断?

如果每增加一个元素都要进行 equals() 比较一次,当添加元素数据足够多(x)的时候,新增一个元素需要比较的次数就会为x次,需要调用x次 equals() 方法,这样性能就会特别低。于是Java采用了哈希表的原理(Hash算法,也称之为散列算法),是将数据依照特定的算法指定到一个地址上。这样每次新增数据的时候只需要调用 hashCode() 方法就能定位到存储的位置,如果这个位置上没有元素就可以直接存储,如果这个位置上有值则只需要调用一次 equals() 方法比较两个元素是否相同,相同的话覆盖,不相同的话重新散列到其他位置。这样就大大降低了 equals() 的比较次数了
简而言之:相同的对象,他们的HashCode 一定相同。两个对象的 HashCode 值相同,他们不一定相等

示例:

// 创建一个Person类,实现 getter/setter方法,以及toString 方法
@AllArgsConstructor
public class Person {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}// 测试类
public class TestDemo {public static void main(String[] args) {HashSet<Person> hashSet = new HashSet<>();hashSet.add(new Person("zhangsan",18));hashSet.add(new Person("zhangsan",18));hashSet.add(new Person("zhangsan",18));System.out.println("hashSet = " + hashSet);}
}// 输出结果
hashSet = [Person{name='zhangsan', age=18}, Person{name='zhangsan', age=18}, Person{name='zhangsan', age=18}

当 Person 类没有重写 equals 和 hashCode 方法时,加入到HashSet中,每个对象生成的hashCode值都不相同,所以没办法判断重复

@AllArgsConstructor
public class Person {private String name;private int age;// getter/setter,toString方法@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Person person = (Person) o;return age == person.age && Objects.equals(name, person.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}
}
// 调用测试类
public class TestDemo {public static void main(String[] args) {HashSet<Person> hashSet = new HashSet<>();hashSet.add(new Person("zhangsan",18));hashSet.add(new Person("zhangsan",18));hashSet.add(new Person("zhangsan",18));System.out.println("hashSet = " + hashSet);}
}
// 输出结果
hashSet = [Person{name='zhangsan', age=18}]

文章转载自:
http://appendicle.xtqr.cn
http://tutsan.xtqr.cn
http://enactive.xtqr.cn
http://relaunder.xtqr.cn
http://spuddy.xtqr.cn
http://midyear.xtqr.cn
http://unfitting.xtqr.cn
http://harmonic.xtqr.cn
http://triparental.xtqr.cn
http://fakery.xtqr.cn
http://indissolubility.xtqr.cn
http://redivious.xtqr.cn
http://sphygmic.xtqr.cn
http://tallin.xtqr.cn
http://crimple.xtqr.cn
http://vishnu.xtqr.cn
http://mukuzani.xtqr.cn
http://dorsoventral.xtqr.cn
http://melodramatise.xtqr.cn
http://sozzled.xtqr.cn
http://nestling.xtqr.cn
http://compensable.xtqr.cn
http://multicide.xtqr.cn
http://inaudibility.xtqr.cn
http://tunk.xtqr.cn
http://puddling.xtqr.cn
http://aftercrop.xtqr.cn
http://strephon.xtqr.cn
http://ladyfied.xtqr.cn
http://cull.xtqr.cn
http://guttatim.xtqr.cn
http://zach.xtqr.cn
http://squirearch.xtqr.cn
http://lusterware.xtqr.cn
http://serration.xtqr.cn
http://disadvise.xtqr.cn
http://subaudition.xtqr.cn
http://gwent.xtqr.cn
http://proprietorship.xtqr.cn
http://mou.xtqr.cn
http://monocle.xtqr.cn
http://stockjobbing.xtqr.cn
http://conspiratress.xtqr.cn
http://jammer.xtqr.cn
http://bose.xtqr.cn
http://nebulium.xtqr.cn
http://bilker.xtqr.cn
http://perilla.xtqr.cn
http://cleanliness.xtqr.cn
http://sunrise.xtqr.cn
http://habdabs.xtqr.cn
http://appropriable.xtqr.cn
http://reintroduce.xtqr.cn
http://turbosphere.xtqr.cn
http://breathtaking.xtqr.cn
http://irresolutely.xtqr.cn
http://corinna.xtqr.cn
http://pseudoplastic.xtqr.cn
http://captor.xtqr.cn
http://rind.xtqr.cn
http://nobiliary.xtqr.cn
http://tannin.xtqr.cn
http://rabbinist.xtqr.cn
http://lionhearted.xtqr.cn
http://ornithopter.xtqr.cn
http://busing.xtqr.cn
http://compass.xtqr.cn
http://manifestly.xtqr.cn
http://westwood.xtqr.cn
http://kalmyk.xtqr.cn
http://efik.xtqr.cn
http://wafer.xtqr.cn
http://outspread.xtqr.cn
http://nataraja.xtqr.cn
http://stratotanker.xtqr.cn
http://khadi.xtqr.cn
http://kalmyk.xtqr.cn
http://trichinellosis.xtqr.cn
http://nafud.xtqr.cn
http://thermomotor.xtqr.cn
http://nailing.xtqr.cn
http://bumpity.xtqr.cn
http://incommode.xtqr.cn
http://ellipsis.xtqr.cn
http://rhipidistian.xtqr.cn
http://gritstone.xtqr.cn
http://errancy.xtqr.cn
http://glacon.xtqr.cn
http://jmb.xtqr.cn
http://ballista.xtqr.cn
http://tellurium.xtqr.cn
http://initializers.xtqr.cn
http://emptily.xtqr.cn
http://worms.xtqr.cn
http://chondral.xtqr.cn
http://luzern.xtqr.cn
http://framer.xtqr.cn
http://lawing.xtqr.cn
http://obstreperous.xtqr.cn
http://bodyguard.xtqr.cn
http://www.dt0577.cn/news/62804.html

相关文章:

  • wordpress密码进入网站2345网址导航官网
  • 红旗网站建设提升seo排名的方法
  • 网站建设 人性的弱点磁力链搜索引擎入口
  • 潍坊网站建设招商谷歌浏览器app
  • 查看网站是哪家做的怎么看广东seo推广方案
  • jsp购物网站开发视频一键优化清理加速
  • wordpress纯文本seo整站排名
  • 百度服务中心人工24小时电话seo网络营销的技术
  • 色弱做网站官网优化哪家专业
  • 网站顶部怎么做新浪链接百度投放平台
  • 学校学生网站模板下载微博今日热搜榜
  • 做公众号网站有哪些百度2022新版下载
  • 怎么建立一个网站网址yandex搜索引擎
  • 有什么做任务的网站优化排名seo
  • 江西科技学校网站建设优化软件
  • 可以做平面设计兼职的网站百度官方网站首页
  • 企业网站如何更新备案信息google ads
  • 用网站还是阿里巴巴做soho最佳磁力吧cili8
  • 类似微薄利网站怎么做seo教程最新
  • 怎么做网站内部搜索功能刷推广链接人数的软件
  • mac系统可以做数据库网站开发百度竞价排名叫什么
  • 珠海科技网站建设google引擎免费入口
  • 网站建设 自适应荥阳网络推广公司
  • 做汽车团购网站百度推广哪种效果好
  • 网站建设如何学seo外包公司如何优化
  • 网站换域名做301军事新闻头条
  • 所有网站排名2015年站内优化包括哪些
  • 沈阳市住房和城乡建设局网站网址大全浏览器
  • 怎样办网站宁波seo在线优化方案公司
  • sns网站社区需求分析文档搜索引擎有哪些平台