標籤

二元樹 (1) 字串常數池 (1) 投資 (3) 每月損益 (37) 例外處理 (1) 泛型 (2) 股票 (15) 指標 (5) 英文 (8) 基本資料型別 (1) 期貨 (1) 程式交易 (10) 量化投資 (5) 亂亂寫 (3) 概念 (3) 資料結構 (3) 演算法 (3) 數學 (3) 轉型 (1) AMA (1) ArrayList (1) assert (1) BeautifulSoup (1) C/C++ (8) casting (1) ClassCastException (1) classpath (1) Collection (4) Comparable (1) comparTo() (1) constructor (1) database (3) Debian (1) Dropbox (2) EA (2) enum (1) equals() (2) exception (3) extends (1) ffmpeg (1) final (1) Git (1) HashMap (1) HashSet (1) hasNext() (1) HTS (3) instanceof (1) IS-A (1) Iterator (1) JAVA (43) length (1) Linux (31) List (1) Mac (6) Map (1) ML (2) MT4 (6) MySQL (2) next() (1) NullPointerException (1) Number (1) Numpy (2) OpenCart (1) OpenCV (3) OSX (1) overloading (1) overriding (3) pandas (2) PHP (8) PriorityQueue (1) Python (11) Queue (1) random() (1) reverse() (1) Samba (1) SCJP (21) sqrt() (1) synchronized (1) talib (1) ufw (1) uTorrent (1) var-args (2) VHF (1) vim (2) Yhoo知識+ (4)
顯示具有 equals() 標籤的文章。 顯示所有文章
顯示具有 equals() 標籤的文章。 顯示所有文章

2012年10月27日 星期六

HashSet的測試

HashSet實作Set介面,Set介面繼承Collection介面。特性為只有唯一值,而無排序無順序。
未覆寫equals()情況下會測試是否為相同物件後塞入HashSet。

import java.util.*;
public class HashTest{
        String s;
        public HashTest(String s){ this.s = s; }

        public static void main(String[] args){
                HashSet<Object> hs = new HashSet<Object>();
                HashTest ht1 = new HashTest("abcedf");
                HashTest ht2 = new HashTest("abcedf");
                String s1 = new String("abcedf");
                String s2 = new String("abcedf");
                hs.add(ht1);
                hs.add(ht2);
                hs.add(s1);
                hs.add(s2);
                System.out.println(hs.size());
        }
}

==============
3

ht1跟ht2被視為不同物件,而字串s1和s2會被視為相同物件的原因是,String本身就覆寫了equals()的函式。
hs大小為3。

2012年10月26日 星期五

equals()和==

equals():值相等
==:指向同一物件


public class EqualsTest{
        public static void main(String[] args){
                String a = "foo";
                String b = "foo";
                String c = new String("foo");
                String d = new String("foo");
                EqualsTest et1 = new EqualsTest();
                EqualsTest et2 = new EqualsTest();

                if(a == b)      System.out.println("Same objects");  //同物件:指向字串常數池的"foo"
                if(a == c)      System.out.println("Same objects");  //不同物件:c的"foo"不在字串池
                else    System.out.println("Different objects");

                if(c == d)      System.out.println("Same objects");  //不同物件
                else    System.out.println("Different objects");

                if(a.equals(b)) System.out.println("Same string");  //回傳true,String有覆寫equals()
                if(a.equals(c)) System.out.println("Same string");  //回傳true,String有覆寫equals()

                if(et1 == et2)  System.out.println("Same objects");  //不同物件
                else    System.out.println("Different objects");

                if(et1.equals(et2))     System.out.println("Same objects");  //不同物件,沒有覆寫equals()

                else    System.out.println("Different objects");


        }
}


==========


Same objects
Different objects
Different objects
Same string
Same string
Different objects
Different objects