Java Type Conversion – Best Practices for Java Type Conversions

javatype conversion

What is the current best practice for all the type conversions necessary in a Java web application? For example, HttpServletRequest.getParameters(...) returns String[], but Hibernate does not allow String[] to be used in an IN clause for a numeric column. Therefore I need to convert String[] to Long[] via Long.valueOf(). What is the best way to handle this, short of rewriting in another language? Do people just create a class full of little static methods to do this?

Best Answer

If you use a JAX-RS implementation like Jersey, you'll get much improved parameter handling and could map it directly to a List.

If you don't want to go that far, you could also pull tricks like using Guava Collections2.transform and a Function<String, Long> on an Arrays.asList view of the array. Not the most compact way, but each piece is reusable so it wouldn't be so bad.