码迷,mamicode.com
首页 > 编程语言 > 详细

Java Entry使用

时间:2014-10-29 14:39:01      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   使用   java   for   

 参考: 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; 

    } 

Java Entry使用

标签:style   blog   http   io   color   ar   使用   java   for   

原文地址:http://www.cnblogs.com/whaozl/p/4059274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!