用來當key的實體
一定要改寫該類別的 hashCode()跟 boolean equlas(Object obj)


 *改寫public int hashCode( ) ---分開儲存,增加搜尋速度,有16層
                     (原來的hashCode,是回傳記憶體位置)
 *改寫public boolean equals(Object obj)   -- 知道第幾層後,用來比對key
                     (原本只能比對記憶體位置,不適用)

 用來當value的實體,依照key的位層貯存,不用改寫hashCode( )

 *equals一樣,hashCode也要一樣 
 (equals有用到的,才可以用在hashCode裡)

import java.util.*;
import java.io.*;
  

 



包裝類別

包- Boxing 
取- enBoxing
autoBoxing 


第七章  static

靜態方法裡不能出現this 或 super,會編譯失敗
讀非靜態變數,需有實體及路徑。

7-6 靜態初始區塊
載入時執行

public class InitBlockTest {
   static String a = "static statements";

   static {   //靜態初始區塊
      System.out.println(a);
   }

   public static void main(String[] args) {
         System.out.println("main method running....");
         System.out.println("請按任意鍵繼續.....");
         try {
            System.in.read();
         } catch(Exception e){ }

        InitBlockTest b = new InitBlockTest();
   }

   InitBlockTest() {
      System.out.println("constructor");
   }

   {      //非靜態初始區塊,主程式執行前執行
      System.out.println("second init block");
   }

   static {
      System.out.println("third init block");
   }
}



7-8   final   
method : 不能被override 
變數: 只有一次設定機會
    如果是參考型別(class型態),實體內容可以更動

空白final變數: 沒有設定初始值。
  在所有建構子中必須指派其值(只能一次),否則會編譯失敗

7-19
新版:  import static 。。。。。
匯入某類別中的某個靜態變數或方法



 

public class HashSetTest {
  public static void main(String[] args) throws Exception {
      Student a = new Student("John", 100, 90, 80);
      Student b = new Student("Robert", 80, 75, 87);
      Student c = new Student("Tom", 100, 0, 0);
      HashSet hs = new HashSet();
      hs.add(a);
      hs.add(b);
      hs.add(c);
      BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));     
      System.out.print("name:");
      String name=keyin.readLine();
      System.out.print("chinese:");     
      int chinese=Integer.parseInt(keyin.readLine());
      System.out.print("eng:");     
      int eng=Integer.parseInt(keyin.readLine());
      System.out.print("math:");     
      int math=Integer.parseInt(keyin.readLine());
      Student d = new Student(name, chinese, eng, math);
      System.out.println(hs.contains(d));
  }
}

class Student {
    String name;
    int chinese, eng, math;
    Student(String name, int chinese, int eng, int math) {
        this.name=name;
        this.chinese=chinese;
        this.eng=eng;
        this.math=math;
    }

//改寫
//-----------------------------
    public int hashCode(){
     return chinese+eng+math+name.charAt(0)+name.length();
    }

    public boolean equals(Object obj){

        if(obj instanceof Student){             //   instanceof  左邊是變數,右邊是型態(類別名稱)
                 // 沒有親子關係,會編譯失敗;有可能的話,看執行結果
          Student t = (Student)obj;
          if(t.name.equals(this.name) && t.chinese==this.chinese && t.eng==this.eng && t.math==this.math)
             //也可寫成name.equals(t.name).....
            return true;
        }
        return false;
    }
//-----------------------------


}



考古題


 

7. Which of the following statements are true?
1)The hashCode method of an object can return any primitive integral type
2) If two objects are equal according to the equals(Object) method, calling the hashCode method on each
    of the objects must produce the same result.
3) The hashcode method of an object must return the same value consistently from one execution of an
    application to another.
4) The signature of the hashcode method of the Object class is public int hashCode()

Ans. 2,4

--------------------------------------------------------------------------------
8. Given the following class definition
public class ValuePair implements Comparable{
    private int  iLookUp;
       
    public ValuePair(int iLookUp, String sValue){
        this.iLookUp=iLookUp;    
    }   
 
    public void setLookUp(int iLookUp){
        this.iLookUp = iLookUp;
    }
    public int getLookUp(){
        return iLookUp;
    }
 
    public boolean equals(Object o){
      if( o instanceof ValuePair){
          ValuePair vp = (ValuePair) o;  
          if(iLookUp == vp.getLookup()){
              return true;
      }
      return false;
    }
  
  
    public int compareTo(Object o) {
        ValuePair vp = (ValuePair) o;
        Integer iwLookUp= new Integer(vp.getLookUp());
        if(iwLookUp.intValue() < iLookUp){
            return -1;
        }
       
        if(iwLookUp.intValue() > iLookUp){
            return +1;
        }
        return 0;
    }
}

Which of the following would be valid hashCode methods?
1)
public int hashCode() {
    return (int) System.currentTimeMillis();   //回傳現在電腦時間
}

2)
public char hashCode(){
    reutrn (char) iLookUp;
}

3)
public int hashCode(){
    return iLookUp;
}

4)
public int hashCode(){
    return iLookup * 100;
}

ANS. 3

--------------------------------------------------------------------------------
9. Given the following code
public class Boxes{
    String sValue;

    Boxes(String sValue){
        this.sValue=sValue;
    }
   
    public String getValue(){
        return sValue;
    }

    public boolean equals(Object o){
        String s = (String) o;
        if (sValue.equals(s) ){
            return true;
        }else{
            return false;
        }
    }

    public int hashCode(){
        return sValue.hashCode();
    }

}

Which of the following statements are true?
1) The hashCode method is correctly implemented
2) This class will not compile as String has no hashCode method
3) The hashCode method is not incorrectly implemented
4) This class will not compile because the compareTo method is not implemented

字串已有實作正確的equals跟hashCode

ANS。 1

--------------------------------------------------------------------------------

 

創作者介紹
創作者 撒姆爾紀事 的頭像
leavescat

撒姆爾紀事

leavescat 發表在 痞客邦 留言(0) 人氣( 584 )