Generic util method to convert in single map from list of array of Object generally result of JPQL/ hibernate I have designed a generic method to convert list of array of Objects to result into single map.  This is working fine, but I want to check weather a key object is valid key (whether it is overriding equals and hashcode method or immutable instance). Am I missing anything else in my code?

/**
@parms
objects : list of array of object generally result of HQL select
keyIndx : index of key object in the array
valueIndex: index of value object in the array
*/

public static Map&lt;? extends Object, ? extends Object&gt; getSingleMapFromLisOfArrayOfObject(List&lt;? extends Object[]&gt; Objects, int keyIdex, int valueIndex)
        throws Exception
    {
        Map&lt;Object, Object&gt; map = null;
        if (Objects != null) {
            map = new HashMap&lt;Object, Object&gt;(Objects.size());
            for (Object[] array : Objects) {
                if (Math.max(keyIdex, valueIndex) &gt; (array.length - 1)) {
                    throw new IndexOutOfBoundsException(keyIdex + " OR " + valueIndex + " out of bound for array");
                }
                if (array[keyIdex] != null) {
                    map.put(array[keyIdex], array[valueIndex]);
                }
            }
        }
    return (map == null ? Collections.EMPTY_MAP : map);

}
There is not much "generic" here, the code is as type safe as with raw types. Further, throws Exception usually isn't a good idea. Last but not least, method arguments should be written lower-case.

Here my attempt (off the top of my head, couldn't test it):

public static &lt;K,V&gt; Map&lt;K, V&gt; getMapFromObjectList(
       List&lt;? extends Object[]&gt; objects, 
       Class&lt;K&gt; keyClass, Class&lt;V&gt; valueClass, 
       int keyIndex, int valueIndex) 
    {
        if (object == null) {
          return Collections.EMPTY_MAP;
        }
        Map&lt;K, V&gt; map = new HashMap&lt;K,V&gt;(objects.size());
        for (Object[] array : objects) {
            if (Math.max(keyIndex, valueIndex) &gt; (array.length - 1)) {
                    throw new IndexOutOfBoundsException(keyIndex + " OR " + valueIndex + " out of bound for array");
            }
            if (array[keyIdex] != null) {
                map.put(keyClass.cast(array[keyIdex]), 
                        valueClass.cast(array[valueIndex]);
            }
        }
        return map;
    }


Of course this code will fail if the types are not the expected ones, but at least it will fail early, and it is already inherently "dangerous" code. On the other hand, it's reasonable to expect that Hibernate doesn't mess it up out of the blue, once the types are correct.

Note that HashMap supports null keys (although IMHO this design decision is questionable).