集合类的一些知识

来源:互联网 发布:矩阵条件数多大算病态 编辑:程序博客网 时间:2024/05/21 14:42

1.  map的key为非string对象时,如何判断key值是否存在

     需要重写equals 方法和 hascode 方法, 举例如下

    // 获取内部类对象
    public BiCostForExcel getBiCostForExcel(String moldNo,String workOrderNo,String inTime,String sectionName,String workType){
        return new BiCostForExcel(moldNo, workOrderNo, inTime, sectionName, workType);
    }
    
    //  内部类,用于加工工单导出EXCEL 构造数据
    public    class BiCostForExcel{
        private String moldNo ;
        private String workOrderNo ;
        private String inTime ;
        private String sectionName ;
        private String workType ;
        
        public BiCostForExcel(){}
        
        public BiCostForExcel(String moldNo,String workOrderNo,String inTime,String sectionName,String workType){
            this.moldNo = moldNo;
            this.workOrderNo = workOrderNo;
            this.inTime = inTime;
            this.sectionName = sectionName;
            this.workType = workType;
        }


    @Override
      public boolean equals(Object obj) {
            BiCostForExcel other = (BiCostForExcel) obj;
           // 增加equals true
                  if(moldNo.equals(other.moldNo)&&workOrderNo.equals(other.workOrderNo)&&inTime.equals(other.inTime)&&sectionName.equals(other.sectionName)&&workType.eq              uals(other.workType)){
                return true;
            }


@Override
        public int hashCode() {
            /*final int prime = 31;
            int result = 1;
            result = prime * result + getOuterType().hashCode();
            result = prime * result + ((inTime == null) ? 0 : inTime.hashCode());
            result = prime * result + ((moldNo == null) ? 0 : moldNo.hashCode());
            result = prime * result + ((sectionName == null) ? 0 : sectionName.hashCode());
            result = prime * result + ((workOrderNo == null) ? 0 : workOrderNo.hashCode());
            result = prime * result + ((workType == null) ? 0 : workType.hashCode());
            return result;*/
            
            int ret = new String(moldNo).hashCode() ^ new String(workOrderNo).hashCode()^ new String(sectionName).hashCode()^ new String(inTime).hashCode()^ new String(workType).hashCode();
            // 也可以用下面这种方式计算hashCode
            //  int ret = String.valueOf(id).hashCode() ^ String.valueOf(type).hashCode();
                //System.out.println(ret);
            return ret;
                
        }


public static void main(String[] args) {
        BiCostForExcel biCostForExcel1 =  new BiCostVo().getBiCostForExcel("1", "2", "2016/12/22", "CNC", "W");
        BiCostForExcel biCostForExcel2 =  new BiCostVo().getBiCostForExcel("1", "2", "2016/12/22", "CNC", "W");
        System.out.println(biCostForExcel1==biCostForExcel2);
        System.out.println(biCostForExcel1.equals(biCostForExcel2));

        Map<BiCostForExcel, Double[]> map = new LinkedHashMap<BiCostForExcel, Double[]>();
        map.put(biCostForExcel1, new Double[]{3.2d,4.3d});
        System.out.println(map.containsKey(biCostForExcel2));
    }


0 0
原创粉丝点击