zoukankan      html  css  js  c++  java
  • Java 将自己定义的对象作为HashMap的key

    须要继承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;
    		}
    	}
    }
    


  • 相关阅读:
    Oracle 基础系列之1.3 用户管理
    Oracle 系统常用命令
    Gogs安装
    centos7 Minimal安装没有ifconfig
    centos 上安装nodejs v8.0.0
    Linux 学习笔记
    使用 weinre 远程调试移动端页面
    Linux tree命令
    innodb分区
    Innodb表空间
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4009465.html
Copyright © 2011-2022 走看看