Java Entry使用


 参考: http://blog.csdn.net/sunmenggmail/article/details/8952712 

     http://www.cnblogs.com/fstang/archive/2013/04/20/3032097.html

我希望要一个ArrayList<Entry>,类似C++中的pair对象,但是Map.Entry是个接口,不能实例化,可以像下面这样写

     /**
     * 选取连续性属性列和因变量列的共2列的数据————根据连续型属性的列索引——要提示因变量只能有1列
     *比如temperature是第三列,找到temperature和decisionIndex的这2列数据
     * 
@param  index                                                              连续型属性的列索引
     * 
@return  ArrayList<Entry<MetaCell, MetaCell>> 返回连续性属性列和因变量列数据——会出现2个都是热,对应因变量取值为no的相同情况——不能用Map,可用ArrayList<Entry<MetaCell, MetaCell>>
     
*/
     public ArrayList<Entry<MetaCell, MetaCell>> getDecisionValue( int index) {
         if( this.decisionIndex.length!=1){
            System.out.println("错误!模型要求因变量为单因变量");
            System.exit(-1); // 退出
             return  null;
        }
         // 1.以下实现了key可以相同的ArrayList类型的Map功能(key可重复)
        ArrayList<Entry<MetaCell, MetaCell>> list =  new ArrayList<Entry<MetaCell, MetaCell>>( this.cellData.m); // 初始化——Entry参考http: // blog.csdn.net/sunmenggmail/article/details/8952712 和  http://www.cnblogs.com/fstang/archive/2013/04/20/3032097.html
         for ( int i = 0; i <  this.cellData.m; i++) {
            list.add( new AbstractMap.SimpleEntry<MetaCell, MetaCell>( this.cellData.data.get(i).get(index),  this.cellData.data.get(i).get( this.decisionIndex[0].getValue())));
        }
         // 2.排序
        Collections.sort(list,  new Comparator<Entry<MetaCell, MetaCell>>(){
            @Override
             public  int compare(Entry<MetaCell, MetaCell> o1, Entry<MetaCell, MetaCell> o2) {
                 return o1.getKey().compareTo(o2.getKey()); // key比较——大于0则表示升序——这里key肯定是DoubleCell,自动调用DoubleCell中的compareTo(重写)
            }
            
        });
         return list;  

    } 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM