zoukankan      html  css  js  c++  java
  • Java HashCode

    在Java中,哈希码代表了对象的一种特征,例如我们判断某两个字符串是否==,如果其哈希码相等,则这两个字符串是相等的。其次,哈希码是一种数据结构的算法。常见的哈希码的算法有:
    1:Object类的hashCode.返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以哈希码也不一样。
    2:String类的hashCode.根据String类包含的字符串的内容,根据一种特殊算法返回哈希码,只要字符串内容相同,返回的哈希码也相同。
    3:Integer类,返回的哈希码就是Integer对象里所包含的那个整数的数值,例如Integer i1=new Integer(100),i1.hashCode的值就是100 。由此可见,2个一样大小的Integer对象,返回的哈希码也一样。

    public class Test  
    {  
         private int num;  
         private String data;  
       
    
         @Override
         public boolean equals(Object obj)  
         {  
            if(this == obj)  
                 return true;  
            if((obj == null) || (obj.getClass() != this.getClass()))  
                return false;  
             // object must be Test at this point  
            Test test = (Test)obj;  
            return num == test.num &&  
            (data == test.data || (data != null && data.equals(test.data)));  
         }  
      
         @Override
        public int hashCode()  
        {  
            int hash = 7;  
            hash = 31 * hash + num;  
            hash = 31 * hash + (null == data ? 0 : data.hashCode());  
            return hash;  
         }  
      
        // other methods  
    }     
    //        Collections.sort(theSelectedColunm, new Comparator() {
    // @Override
    // public int compare(Object o1, Object o2) {
    // JSONObject oj1 = JSONObject.fromObject(o1);
    // JSONObject oj2 = JSONObject.fromObject(o2);
    // return (oj2.getInt("orgId") - oj1.getInt("orgId"));
    //
    // }
    // });

    //     Collections.sort(jsonArray, new Comparator() {
    // @Override
    // public int compare(Object o1, Object o2) {
    // JSONObject oj1 = (JSONObject) o1;
    // JSONObject oj2 = (JSONObject) o2;
    // return oj2.getInt("org_id") - oj1.getInt("org_id");
    // }
    // });

    HttpResponseUtil.writeJson(vo.toJson(), getResponse());
  • 相关阅读:
    ExportToExcel(工作笔记)
    EXCEL中的公式
    HTML基础(二)
    .NET开发人员必知的八个网站
    HTML基础(一)
    Extjs学习笔记(消息框)
    The diff between throw and throw e
    工作中遇到的一些小知识点(备查)
    ASP.NET页面在IE缓存问题的解决
    URL中的"#"
  • 原文地址:https://www.cnblogs.com/lhang55/p/7590611.html
Copyright © 2011-2022 走看看