以下哪种实现最适合将ResultSet转换为KeyVal对?

时间:2019-04-18 02:02:26

标签: java linkedhashmap

我有2条运行代码将ResultSet数据转换为LinkedHashMap键值对的列表。我需要关于哪种方法更好的建议。请注意,将要输入的数据不是很大。

private static List<LinkedHashMap<String,String>> getKeyValuePair(ResultSet result) throws SQLException {
List<LinkedHashMap<String,String>> keyVal = new ArrayList<>();
LinkedHashMap<String,String> tempData = new LinkedHashMap<>();

int dataCounter = 0;

ResultSetMetaData rsmd = result.getMetaData();
int colCount = rsmd.getColumnCount();

while (result.next()){
for (int cnt01 = 1 ; cnt01 <= colCount; cnt01 ++ ) {
  tempData.put(rsmd.getColumnName(cnt01),result.getString(cnt01) );
}
 **keyVal.add(dataCounter, (LinkedHashMap<String, String>) tempData.clone());**
 dataCounter++;
            tempData.clear();
        }
        return  keyVal;
}

private static List<LinkedHashMap<String,String>> getKeyValuePair(ResultSet result) throws SQLException 
{
        List<LinkedHashMap<String,String>> keyVal = new ArrayList<>();
        int dataCounter = 0;

        ResultSetMetaData rsmd = result.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (result.next()){
        **LinkedHashMap<String,String> tempData = new LinkedHashMap<>();**
            for (int cnt01 = 1 ; cnt01 <= colCount; cnt01 ++ ) {
                tempData.put(rsmd.getColumnName(cnt01),result.getString(cnt01) );
            }
            **keyVal.add(dataCounter, tempData);**
            dataCounter++;
        }
        return  keyVal;
    }

0 个答案:

没有答案