一.toString(): 在Object类里面定义toString()方法的时候返回的对象的哈希code码,这个hashcode码不能简单明了的表示出对象的属性.所以要重写toString()方法.当需要将一个对象输出到显示器时,通常要调用他的toString()方法,将对象的内容转换为字符串.java中的所有类默认都有一个toString()方法.默认情况下 System.out.println(对象名)或者System.out.println(对象名.toString())输出的是此对象的…
import java.util.Set; import java.util.HashSet; public class SetTest { public static void main(String[] args) { /* *对于用户自己定义类型的数据放在容器(Set)中 *务必重写equals和hashCode方法 *要不然stu1和stu2放在容器中,和觉得是两个不同的元素 **/ //set中存放的元素是无序的 //set中存储的元素是不能够反复的(依据equals方法和hashCo…
package com.ddy; public class User {     private Integer id;     private String name;     private String address;     private String phone;     public Integer getId() {         return id;     }     public void setId(Integer id) {         this.id = id…
http://jingyan.baidu.com/article/d5a880eb8fb61d13f147cc99.html 1.为什么必须重写这两个方法. 2.什么事hashSet去重,符合什么样的逻辑?(属性相同的不同对象在调用其hashCode方法后,返回的是同样的哈希码,这样的对象就是重复的.都则机器对象都是非重复的,返回的hashcode都是Obejct Hash方法,跟本地实现有关,有可能是内存地址)…
1.*为什么要重写equals方法,首先我们来看一下equals源码: 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[] =…
对象作为 map 的 key 时,需要重写 hashCode 和 equals方法 如果没有重写 hashCode 方法,那么下面的代码示例会输出 null 我们首先定义一个对象:BmapPoint,假如这个对象只重写了 equals 方法,没有重写 hashCode 方法 package mm_test; /** * @Function: TODO ADD FUNCTION. <br/> * @Date: 2016年3月7日 下午4:29:23 * * @author zhangmengme…
在实际应用中经常会比较两个对象是否相等,比如下面的Address类,它有两个属性:String province 和 String city. public class Address { private String province; private String city; public String getProvince() { return province; } public void setProvince(String province) { this.province = p…
public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; int[] c = {1, 2, 4, 6}; //下面这个方法打印的是a数组的引用地址 System.out.println(a.toString()); //下面这个方法比较的是两个数组的引用是否相等 System.out.println("a.equals(b):"+a.equals(b…
public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; int[] c = {1, 2, 4, 6}; //下面这个方法打印的是a数组的引用地址 System.out.println(a.toString()); //下面这个方法比较的是两个数组的引用是否相等 System.out.println("a.equals(b):"+a.equals(b…
关于Object类的equals的特点,对于非空引用: 1.自反性:x.equals(x) return true : 2.对称性:x.equals(y)为true,那么y.equals(x)也为true: 3.传递性:x.equals(y)为true,y.equals(z)为true,那么x.equals(z)也为true: 4.一致性:x.equals(y)的第一次调用为true,那么x.equals(y)的第二次,第三次,...,第n次调用也为true,前提条件是在比较之间没有修改x,也没…