欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个关于自己定义的类,做为hashMap的key对象的例子 博客分类: java编程  

程序员文章站 2024-03-22 20:09:16
...
package iss.com.equals;

import java.util.Date;

class CountIsable {

 private Date date;

 private String accountNo;

 private String bankCode;

 public CountIsable(Date transactionTime, String accountNo, String bankCode) {
  this.date = transactionTime;
  this.accountNo = accountNo;
  this.bankCode = bankCode;
 }

 public CountIsable(String accountNo, String bankCode) {
  this.accountNo = accountNo;
  this.bankCode = bankCode;
 }
                
 public boolean equals(CountIsable mapKey) {
  if (DateUtil.compareDate(date, mapKey.getDate()) == 0
    && accountNo.equals(mapKey.getAccountNo())) {
   if (mapKey.getBankCode() == null
     || mapKey.getBankCode().equals(bankCode))
    return true;
  }
  return false;
 }
 public boolean equals(Object key) {
  CountIsable mapKey=(CountIsable)key;
  if (DateUtil.compareDate(date, mapKey.getDate()) == 0
    && accountNo.equals(mapKey.getAccountNo())) {
   if (mapKey.getBankCode() == null
     || mapKey.getBankCode().equals(bankCode))
    return true;
  }
  return false;
 }

 public String toString() {
  return date.toString() + accountNo + bankCode;
 }

public int hashCode() {
 int temp =(date.toString() + accountNo + bankCode).hashCode(); 
  return temp;
 }

 public String getAccountNo() {
  return accountNo;
 }

 public String getBankCode() {
  return bankCode;
 }

 public Date getDate() {
  return date;
 }
}