集合練習——Set部分,集合set部分

來源:互聯網
上載者:User

集合練習——Set部分,集合set部分

我們知道list儲存的是有序不唯一的元素。

set儲存的是無序唯一的元素。

那麼下面看一個例子吧:

package CollectionPart;import java.util.HashSet;import java.util.Set;public class HashSet1 {    public static void main(String[] args) {        Set mySet = new HashSet();        mySet.add("df");        mySet.add("df");        System.out.println(mySet.size());        for (Object object : mySet) {            System.out.println(object);        }    }}

運行結果:

1
df

 

這就充分說明了,set集合裡面儲存的元素是唯一的。

那麼如下代碼將會反映一個問題:

package CollectionPart;import java.util.HashSet;import java.util.Set;public class HashSet1 {    public static void main(String[] args) {        Set mySet = new HashSet();        mySet.add("df");        mySet.add("df");                Employee_1 e1 = new Employee_1("1","lifei",23,"2013-02-09");        Employee_1 e2 = new Employee_1("1","lifei",23,"2013-02-09");                mySet.add(e1);        mySet.add(e2);                System.out.println(mySet.size());        for (Object object : mySet) {            System.out.println(object);        }    }}

運行結果:

3
df
Employee_1 [employeeId=1, employeeName=lifei, employeeAge=23, employeeHireDate=2013-02-09]
Employee_1 [employeeId=1, employeeName=lifei, employeeAge=23, employeeHireDate=2013-02-09]

我們儲存的元素明明一模一樣,但是結果好像不如人意,不是說好set裡面儲存的都是不同的元素麼,e1 跟e2明顯是 同一個對象。

這就需要考量set集合在判定元素是否相等的時候,是通過什麼進行判別的。

滑鼠移到這個 add方法上面就會有提示:

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.

這個說明還是有必要讀一下的:添加特定元素給set集合,如果不存在這樣一個元素的時候。通常情況下,為當前set集合添加元素e時,如果這個set集合沒有已經存在一個e2的話,【通過equals函數來比較】如果已經存在此元素的話,那麼add方法的調用並不會修改set集合,並且會返回false。

//有兩個點要試:1、null的值可以儲存幾個,從說明上感覺是1個。2、第二次添加已經存在的元素的時候,會報添加失敗,而不是第二次的值覆蓋第一次的值。這個是純想象的,因為覆蓋就有添加了一次,這感覺上會產生更多的操作。不如不允許添加來的效率高。

package CollectionPart;import java.util.HashSet;import java.util.Set;public class HashSet1 {    public static void main(String[] args) {        Set mySet = new HashSet();        mySet.add("df");        boolean add = mySet.add("df");                System.out.println("是否添加成功:"+add);        Employee_1 e1 = new Employee_1("1","lifei",23,"2013-02-09");        Employee_1 e2 = new Employee_1("1","lifei",23,"2013-02-09");                mySet.add(e1);        mySet.add(e2);                System.out.println(mySet.size());        for (Object object : mySet) {            System.out.println(object);        }        mySet.add(null);        boolean add2 = mySet.add(null);        System.out.println("是否添加成功:"+add2+",此時set集合中的元素個數:"+mySet.size());    }}

運行結果:

是否添加成功:false
3
df
Employee_1 [employeeId=1, employeeName=lifei, employeeAge=23, employeeHireDate=2013-02-09]
Employee_1 [employeeId=1, employeeName=lifei, employeeAge=23, employeeHireDate=2013-02-09]
是否添加成功:false,此時set集合中的元素個數:4

 

接下來就要處理,這個 equals的問題了,就可以回去考究這個 set集合中對於元素的equals的方法的比較了,畢竟,e1 跟e2 是一個人。要想辦法,把它們區分開。查看預設的 Employee中的equals方法

 

我們所寫的員工類裡面並沒有一個 equals方法,說明這是 繼承來的:所以敲出equals 坐等聯想然後得到這樣一個函數:

@Override    public boolean equals(Object obj) {        // TODO Auto-generated method stub        return super.equals(obj);    }

滑鼠放在 equals上面得到這樣一個說明:

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

簡單翻譯一下:歡迎斧正,

判斷其他的對象是否與當前對象相等。

反射性?reflexive?:對於任何值,x.equals(s) 應該返回true.

對稱性:對於非空值x,y,如果x.equals(y)返回true時,若且唯若y.equals(x)也返回true.

傳遞性:對於非空值x,y和z,如果 x.equals(y)返回true,y.equals(z)返回true,那麼x.equals(z)應該也返回true.

一致性?:不論怎麼調用,結果都應該是一致的,要麼true,要麼false,當參與比較的兩個非空值,x,y,在這種不斷地比較過程中始終未被修改的時候。

對於非空值 x,x.equals(null) 的傳回值應該是false.

equals方法用來描述object類中兩個對象是否相等的關係,如果非空值x,y的返回結果是true的話,那麼x,y執行的是相同的實體。

註:通常情況下也要重寫hashCode方法,只要equals方法被重寫。為了hashCode方法保持總體聯絡,為了標明相同的對象具有相同的hash碼。

所以,這才是 Set集合要注意的地方,只要使用Set集合的話,就要記得重寫equals 方法和 hashCode方法。【好在這兩個方法都可以自動產生。】

在ssh裡面,如果用到多對多的情況的時候,要記得重寫這兩個方法。

不過重點好像還是沒有解釋清楚。就是 e1 究竟為什麼不等於e2.

我們通過快速鍵ctrl+滑鼠左鍵,找到他的實現方法:

public boolean equals(Object obj) {        return (this == obj);    }

發現。他們在底層調用的就是這樣的語句,那麼問題來了,這比較的是什麼,this指代的是什嗎?我們通常在校正是否null 指標的時候總會,輸出一下,當前對象。那麼這個比較就是返回的當前這個 對象的地址值,如果 這個 值不相等的話,就認為兩者不相等。好了,那麼現在我們認為 id相同並且,姓名相同的話,這兩個元素就是一個實體,事實上,最好把所有屬性都包含進去,但是 可以利用後期系統的某些存在唯一性限制式的屬性,作為兩個實體是否相等的標識。

 

Set集合要說明的就是這一個一定要重寫 equals() 方法和 hashCode()方法,別的沒有了。

 

習題:列印員工姓名及性別,利用迭代器遍曆

員工的欄位有 工號 姓名 年齡 入職時間。

由於改寫了 員工類的 不妨新寫一個 實體類。

//寫的時候 複製的,所以 就輸出 姓名和年齡吧

package CollectionPart;/** * 為了Set的聯絡,比Employee_1多重寫了兩個方法 *  */public class Employee_2 {        private String employeeId;    private String employeeName;    private int employeeAge;    private String employeeHireDate;    /*    這裡就儲存成String 類型,在 資料庫裡面設定成 date格式。    然後 利用Date today = new Date();    SimpleDateFormat fm = new SimpleDateFormat("YYYY-MM-dd");    來做    */    public String getEmployeeId() {        return employeeId;    }    public void setEmployeeId(String employeeId) {        this.employeeId = employeeId;    }    public String getEmployeeName() {        return employeeName;    }    public void setEmployeeName(String employeeName) {        this.employeeName = employeeName;    }    public int getEmployeeAge() {        return employeeAge;    }    public void setEmployeeAge(int employeeAge) {        this.employeeAge = employeeAge;    }    public String getEmployeeHireDate() {        return employeeHireDate;    }    public void setEmployeeHireDate(String employeeHireDate) {        this.employeeHireDate = employeeHireDate;    }    @Override    public String toString() {        return "Employee_1 [employeeId=" + employeeId + ", employeeName=" + employeeName + ", employeeAge="                + employeeAge + ", employeeHireDate=" + employeeHireDate + "]";    }    public Employee_2(String employeeId, String employeeName, int employeeAge, String employeeHireDate) {        super();        this.employeeId = employeeId;        this.employeeName = employeeName;        this.employeeAge = employeeAge;        this.employeeHireDate = employeeHireDate;    }    public Employee_2() {        super();    }    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + employeeAge;        result = prime * result + ((employeeHireDate == null) ? 0 : employeeHireDate.hashCode());        result = prime * result + ((employeeId == null) ? 0 : employeeId.hashCode());        result = prime * result + ((employeeName == null) ? 0 : employeeName.hashCode());        return result;    }    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        Employee_2 other = (Employee_2) obj;        if (employeeAge != other.employeeAge)            return false;        if (employeeHireDate == null) {            if (other.employeeHireDate != null)                return false;        } else if (!employeeHireDate.equals(other.employeeHireDate))            return false;        if (employeeId == null) {            if (other.employeeId != null)                return false;        } else if (!employeeId.equals(other.employeeId))            return false;        if (employeeName == null) {            if (other.employeeName != null)                return false;        } else if (!employeeName.equals(other.employeeName))            return false;        return true;    }
}
package CollectionPart;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class HashSetPractise_1 {    public static void main(String[] args) {        Employee_2 e1= new Employee_2("dept1_001", "lifei", 23, "2016-02-05");        Employee_2 e2= new Employee_2("dept1_001", "lifei", 23, "2016-02-05");        Employee_2 e3= new Employee_2("dept1_002", "life2", 24, "2016-03-05");        Employee_2 e4= new Employee_2("dept3_001", "life3", 28, "2015-03-05");        Set<Employee_2> mySet = new HashSet<Employee_2>();        mySet.add(e1);        mySet.add(e2);        mySet.add(e3);        mySet.add(e4);        Iterator<Employee_2> iterator = mySet.iterator();        System.out.println("員工人數為:"+mySet.size());        while (iterator.hasNext()) {            Employee_2 employee_2 = iterator.next();            System.out.println("當前員工: "+employee_2.getEmployeeName()+" ,年齡為:"+employee_2.getEmployeeAge());        }    }}

運行結果:

員工人數為:3
當前員工: life3 ,年齡為:28
當前員工: lifei ,年齡為:23
當前員工: life2 ,年齡為:24

 

發現一個事兒,就是 確實是無序的。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.