Java List to Int List toIntegerList(List strList, boolean failOnException)

Here you can find the source of toIntegerList(List strList, boolean failOnException)

Description

Parse a list of String into a list of Integer.

License

Open Source License

Parameter

Parameter Description
strList can't be null
failOnException if an element can not be parsed should we return null or add a null element to the list.

Return

list of all String parsed as Integer or null if failOnException

Declaration

public static List<Integer> toIntegerList(List<String> strList, boolean failOnException) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**// w w  w.  java  2s .c om
     * 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.
     * @param strList can't be null
     * @param failOnException if an element can not be parsed should we return null or add a null element to the list.
     * @return list of all String parsed as Integer or null if failOnException
     */
    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;
    }
}

Related

  1. toIntegerArray(List values)
  2. toIntegerList(final E[] array)
  3. toIntegerList(int... array)
  4. toIntegerList(int[] array)
  5. toIntegerList(int[] intArray)
  6. toIntegers(final List longs)
  7. toIntegers(List values)
  8. toIntList(List list)
  9. toIntList(Set members)