您的位置:首页 > 其它

怎样获取一个对象的HashCode地址值

2016-11-24 16:10 344 查看
package test;

import java.util.HashSet;

import java.util.Set;

public class TestB {
 
   @SuppressWarnings({ "rawtypes", "unchecked" })
public static void main (String[] args) {
               TestB tb= new TestB();                      //随便获得一个类本身的 对象 
    List list = new ArrayList();
    //list.add("2");
    Map<K, V> m = new HashMap();

                        Set s = new HashSet<>();             //zz在自己定义一个任意对象这里选集合型Hashset的对象

    //s.add("1");
    int b = s.hashCode();                        //由此看出直接通过hashCode();       就能获得地址值在随便用个整形变量接收并输出就ok了
   int a
=tb.hashCode();                       //此处需注意set对象 不存内容哈希值为0;

 int c = list.hashCode();                                  //list不存时为1

   int d = m.hashCode();                                // map不存时为0

   System.out.println(a);
   System.out.println(b);

           System.out.println(c);
   System.out.println(d);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: