hashcode和equals方法

來源:互聯網
上載者:User

標籤:

分析:

1:Person類

1:姓名和年齡

2:重寫hashCode和equals方法

1:如果不重寫,調用Object類的equals方法,判斷記憶體位址,為false

1:如果是Person類對象,並且姓名和年齡相同就返回true

2:如果不重寫,調用父類hashCode方法

1:如果equals方法相同,那麼hashCode也要相同,需要重寫hashCode方法

3:重寫toString方法

1:不重寫,直接調用Object類的toString方法,列印該對象的記憶體位址

Person類

class Person {

private String name;

private int age;

 

public Person() {

 

}

 

public Person(String name, int age) {

 

this.name = name;

this.age = age;

}

 

@Override

public int hashCode() {

return this.name.hashCode() + age;

}

 

@Override

public boolean equals(Object obj) {

if (!(obj instanceof Person)) {

return false;

}

Person p = (Person) obj;

return this.name.equals(p.name) && this.age == p.age;

}

 

@Override  

public String toString() {

return "Person :name=" + name + ", age=" + age;

}

 

}

 

 

public static void main(String[] args) {

Person p1 = new Person("張三", 19);

Person p2 = new Person("李四", 20);

Person p3 = new Person("王五", 18);

Collection list = new ArrayList();

list.add(p1);

list.add(p2);

list.add(p3);

// isEmpty() 判斷集合是否為空白

boolean empty = list.isEmpty();

System.out.println(empty);

// 返回集合容器的大小

int size = list.size();

System.out.println(size);

         // contains()判斷集合何中是否包含指定對象

boolean contains = list.contains(p1);

System.out.println(contains);

 

// remove(); 將指定的對象從集合中刪除

list.remove(p1);

 

// clear() 清空集合中的所有元素

list.clear();

System.out.println(list);

 

}

 

//使用集合儲存自訂對象2

class Book {

private String name;

private double price;

 

public Book() {

 

}

 

public Book(String name, double price) {

this.name = name;

this.price = price;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public double getPrice() {

return price;

}

 

public void setPrice(double price) {

this.price = price;

}

 

@Override

public int hashCode() {

return (int) (this.name.hashCode() + price);

}

 

@Override

public boolean equals(Object obj) {

if (!(obj instanceof Book)) {

return false;

}

Book book = (Book) obj;

return this.name.equals(book.name) && this.price == book.price;

}

 

@Override

public String toString() {

return "book:@ name:" + this.name + ", price:" + this.price;

}

}

public class Demo1 {

public static void main(String[] args) {

Collection col = new ArrayList();

col.add(new Book("think in java", 100));

col.add(new Book("core java", 200));  System.out.println(col);

}

}

hashcode和equals方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.