泛型、hashmap的应用

package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class getParameter {
	
	public ArrayList<Map<String,ResultSet>> getParameter_test(String api_code_string) throws SQLException{
		List<String> apiCodeList = Arrays.asList(api_code_string.split(","));
		System.out.println("##########开始对api_code进行排序##########");
		Connection c=null;
		Map<String,Integer> sorted_map=new HashMap();
		for(int i=0;i<apiCodeList.size();i++) {
			int tmp;
			String apiCode = apiCodeList.get(i);
			try {
				Class.forName("org.postgresql.Driver");
				ResultSet rs2 = null;
				c = DriverManager.getConnection("xxxxxxx", "xxxxxxx",
						"xxxxxxx");
				Statement stmt2=c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
			
				rs2=stmt2.executeQuery("select api_weight from xxx where api_code='"
						+apiCode+"';");
				if(rs2.next()) {
					rs2.first();
					tmp=rs2.getInt(1);
					sorted_map.put(apiCode, tmp);
//					System.out.println("数据长这样"+rs2.getInt(1));
				}else {
					sorted_map.put(apiCode,0);
				}			
				rs2.close();
				stmt2.close();
			}catch(Exception e) {
				e.printStackTrace();
			}finally {c.close();};
		}
	    ArrayList<Map.Entry<String,Integer>> list = new ArrayList<>(sorted_map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());//升序,前边加负号变为降序
            }
        });
        List<String> apiCodeListSored=new ArrayList<>();
        Iterator<Map.Entry<String, Integer>> iterator = list.iterator();
        for(Map.Entry<String, Integer> m : list){
            System.out.println(m.getKey()+"="+m.getValue());
            apiCodeListSored.add(m.getKey());
        }
        System.out.println(apiCodeListSored);
        ArrayList<Map<String,ResultSet>> list2=new ArrayList<>();
		for (int i = 0; i < apiCodeListSored.size(); i++) {
			String apiCode = apiCodeListSored.get(i);
			Map<String,ResultSet> result_map=new HashMap();
			try {
				Class.forName("org.postgresql.Driver");
				ResultSet rs = null;
				c = DriverManager.getConnection("xxxxxxx", "xxxxxxx",
						"xxxxxxx");
				Statement stmt = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
				rs = stmt.executeQuery(
						"select api_url,mongodb_host,mongodb_port,mongodb_dbname,mongodb_collection from xxxx where api_code='"
								+ apiCode + "';");
				result_map.put(apiCode, rs);
				list2.add(result_map);
				rs.close();
				stmt.close();
			}catch(Exception e) {
					e.printStackTrace();;
			}finally {
					c.close();
			}
		}		
		return list2;
	} 
	public static void main(String args[]) throws SQLException {
		ArrayList<Map<String,ResultSet>>s=null;
		getParameter gp=new getParameter();
		s=gp.getParameter_test("371,364,888,666,717,918,819,999,456,909");
		for (int i=0;i<s.size();i++) {
			Map<String,ResultSet> m=new HashMap<>();
			m=s.get(i);
			for (Map.Entry<String, ResultSet> entry : m.entrySet()) {
				System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
		}	
	}
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值