Java tutorial
//package com.java2s; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /** * * pre : resultSet must be a valid List<Map> <br> * post : a Map<keyValue, Map> * * @param resultSet * result set from sql. no need order by * @param key * the key to get the value to be the new key for the returned map * @return null if resultSet is null */ @SuppressWarnings("rawtypes") public static Map<Object, Map> sqlListToMapWithUniqueKey(List<Map> resultSet, String key) { if (resultSet == null) return null; Map<Object, Map> rs = new HashMap<Object, Map>(resultSet.size()); for (Map map : resultSet) { rs.put(map.get(key), map); } return rs; } }