public static List < Integer > toIntegerList ( List < String > strList , boolean failOnException ) { List < Integer > intList = new ArrayList < Integer > ( ) ; for ( String str : strList ) { try { intList . add ( Integer . parseInt ( str ) ) ; } catch ( NumberFormatException nfe ) { if ( failOnException ) { return null ; } else { intList . add ( null ) ; } } } return intList ; }
Parse a list of String into a list of Integer . If one element can not be parsed the behavior depends on the value of failOnException .