Java 将自己定义的对象作为HashMap的key
2024-08-30 05:03:18
须要继承Map的equals函数和hashCode函数
package com.category; import java.util.HashMap; public class GenCategoryLevelData { private static HashMap<Category, Integer> categoryLevel = new HashMap<Category, Integer>();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub } /**
* 分类
* @author ouyangyewei
*
*/
public static class Category {
public long categoryId;
public String categoryName; @Override
public boolean equals(Object obj) {
Category category = (Category)obj;
return (this.categoryId==category.categoryId
&& this.categoryName.equals(category.categoryName));
}
@Override
public final int hashCode() {
int hashCode = 17;
hashCode = hashCode * 31 + 1;
hashCode = hashCode * 31 + 1;
return hashCode;
} public Category() {
// empty
}
public Category(long categoryId, String categoryName) {
this.categoryId = categoryId;
this.categoryName = categoryName;
}
public long getCategoryId() {
return categoryId;
}
public void setCategoryId(long categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
}
}
最新文章
- 集群节点间网络通信TIPC
- windows下如何安装和启动MySQL
- tiny4412学习一:编译uboot,体验裸机
- 【Spring开发】—— Spring Core
- 禁止Android的StatusBar下拉
- word 中Sentences、Paragraph等含义和用法
- 配置php中的Oracle扩展
- 自己实现的一个简单的EF框架(反射实现)
- 深入理解计算机系统_3e 第三章家庭作业 CS:APP3e chapter 3 homework
- new words
- VS快捷键失效问题
- 【CF1154】题解
- python bytes类型去除尾部字节
- 用casperjs模拟登录,支持多个账户登录
- openx _金额
- 电商商品规则属性sku 工具类
- 线程不安全的类不要轻易做为static变量使用,及如何使用ThreadLocal将共享变量变为独享变量
- vue2.0非父子间进行通讯
- Ubuntu下改变文件权限
- Why Isn&#39;t curr_items Decreasing When Items Expire?