zoukankan      html  css  js  c++  java
  • java entry

    我希望要一个ArrayList<Entry>,类似C++中的pair,

    可是Map.Entry是个接口,不能实例化,能够像以下这样写


    HashMap<Integer, Integer> G = new HashMap<Integer,Integer>();
    G.put(1, 9); G.put(4, 6); G.put(2, 8);G.put(3, 7);    
    ArrayList<Map.Entry<Integer, Integer>> arrayList = new 
            ArrayList<Map.Entry<Integer, Integer>(G.entrySet());

    ArrayList<Map.Entry<Integer, Integer>> arrayList = new 
        ArrayList<Map.Entry<Integer, Integer>>();
    arrayList.add(new AbstractMap.SimpleEntry(1, 9));

    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("d", 2);
    map.put("c", 1);
    map.put("b", 1);
    map.put("a", 3);
    
    List<Map.Entry<String, Integer>> infoIds =
        new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
    
    //排序前
    for (int i = 0; i < infoIds.size(); i++) {
        String id = infoIds.get(i).toString();
        System.out.println(id);
    }
    //d 2
    //c 1
    //b 1
    //a 3
    
    //排序
    Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {   
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {      
            //return (o2.getValue() - o1.getValue()); 
            return (o1.getKey()).toString().compareTo(o2.getKey());
        }
    }); 
    
    //排序后
    for (int i = 0; i < infoIds.size(); i++) {
        String id = infoIds.get(i).toString();
        System.out.println(id);
    }
    //依据key排序
    //a 3
    //b 1
    //c 1
    //d 2
    //依据value排序
    //a 3
    //d 2
    //b 1
    //c 1


    List<Map.Entry<String,Double>> termls = new ArrayList<Map.Entry<String,Double>>();
    			for(String s: tf.get(i).keySet()) {
    				termls.add(new AbstractMap.SimpleEntry(s, tf.get(i).get(s)*1.0*Math.log10(count*1.0/df.get(s))));
    			}
    			Collections.sort(termls, new Comparator<Map.Entry<String, Double>>(){
    				public int compare(Map.Entry<String, Double> o1,Map.Entry<String, Double> o2){
    					return(o2.getValue().compareTo(o1.getValue()));
    				}
    				
    			}
    			);
    			System.out.println("line " + (i+1));
    			for (int ind = 0; ind < termls.size(); ++ind){
    				System.out.print(termls.get(ind).toString()+"  ");
    			}
    			System.out.println();


  • 相关阅读:
    关于使用Java Mail进行邮件发送,抛出Could not connect to SMTP host: xx@xxx.com, port: 25的异常可能
    百度地图和solr展示资源和附近等功能的实现 四
    Python爬虫入门-3
    Python爬虫入门-2
    Python爬虫入门-1
    Python装饰器专题-限制函数调用次数(10s调用一次)
    32个Python爬虫项目让你一次吃到撑
    时间复杂度趣图分析
    各类数据库默认端口总结
    ansible使用三(ansible roles)
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4275778.html
Copyright © 2011-2022 走看看