24 April 2016

Nhận biết tên và địa chỉ của 1 người trong hashSet


Main.java
package edu.aptech1;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Main {
 @SuppressWarnings("unused")
 public static void main(String[] args) {
  ArrayList<String> list = new ArrayList<String>();
  Student a = new Student("Ngoc", "Ha Noi", 111);
  Student b = new Student("Ngoc", "Ho Chi Minh", 11);
  Student c = new Student("Ngoc", "Ho Chi Minh", 1111);

  Set<Student> s = new HashSet<Student>();
  s.add(a);
  s.add(b);
  s.add(c);
  System.out.println("Set size: " + s.size());
  for(Student s1 : s){
   System.out.println(s1.getName() +" "+s1.getLocation()+" "+s1.getMoney());
  }

 }

}
Student.java
package edu.aptech1;

public class Student {
 private String name;
 private String location;
 private int money;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Student(String name) {
  super();
  this.name = name;
 }

 public Student() {
  super();
 }

 public String getLocation() {
  return location;
 }

 public void setLocation(String location) {
  this.location = location;
 }

 public int getMoney() {
  return money;
 }

 public void setMoney(int money) {
  this.money = money;
 }

 public Student(String name, String location, int money) {
  super();
  this.name = name;
  this.location = location;
  this.money = money;
 }

 @Override
 public int hashCode() {
  return location.hashCode(); // location hay name --> ok
 }

 @Override
 public boolean equals(Object obj) {
  if (!(obj instanceof Student)) {
   return false;
  }
  Student s = (Student) obj;
  if (s.getName() == null || s.getLocation() == null) {
   return false;
  }
  return s.getName().equals(name) && s.getLocation().equals(location) && getMoney() == money;
 }
}


                    
                    
                    
                  

Related Posts:

  • Kết nối Microsoft Access bằng Java Phần 1: Kết nối Microsoft Access bằng Java Nếu bạn sử dụng MS Access 2003(đuôi là .mdb) thì không phải cài đặt thêm Driver Còn nếu như bạn sử dụng MS Access 2007, 20120 (đuôi là .accdb)  thì các bạn cài đặt thêm:… Read More
  • Tạo Gmail không cần xác nhận Create  Unlimited Gmail or YouTube Account Without Phone Verification These steps must be followed after each YouTube account creation along with the steps we normally do (Privacy clear + cache clear +IP change). I … Read More
  • Bấm cáp mạng Mạng Bấm cáp mạng Chuẩn bị: -                Cáp mạng: CAT5 hoặc CAT6 (tức là dây mạng) -       &nbs… Read More
  • Tắt cảnh báo giả mạo virus trên smartphone Virus luôn là mối lo ngại của bất cứ ai. Để phòng tránh virus, bạn có thể cài trên máy các phần mềm diệt virus nhưng đôi khi bạn vẫn nhận được các thông báo thiết bị của bạn đang nhiễm virus nặng và cần cài thêm một phần mềm… Read More
  • Thêm Payooner vào Adsense CÁCH NHẬP TÀI KHOẢN PAYONEER VÀO Adsense Chuẩn bị: Bank account holder name Routing number Account number Các thông tin này anh em có thể đăng nhập vào Payoneer lấy nhé, vào mục Receive > Global Paym… Read More

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang