2
\$\begingroup\$

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<? extends Object, ? extends Object> getSingleMapFromLisOfArrayOfObject(List<? extends Object[]> Objects, int keyIdex, int valueIndex)
        throws Exception
    {
        Map<Object, Object> map = null;
        if (Objects != null) {
            map = new HashMap<Object, Object>(Objects.size());
            for (Object[] array : Objects) {
                if (Math.max(keyIdex, valueIndex) > (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);

}
\$\endgroup\$
0
2
\$\begingroup\$

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 <K,V> Map<K, V> getMapFromObjectList(
       List<? extends Object[]> objects, 
       Class<K> keyClass, Class<V> valueClass, 
       int keyIndex, int valueIndex) 
    {
        if (object == null) {
          return Collections.EMPTY_MAP;
        }
        Map<K, V> map = new HashMap<K,V>(objects.size());
        for (Object[] array : objects) {
            if (Math.max(keyIndex, valueIndex) > (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).

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the nice answer :). I know that java.uit.HashMap allows null key but null cannot be treated as good identifier, so i skipped the null key. \$\endgroup\$
    – khushnood
    May 23 '14 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.