Class ListUtil


  • public class ListUtil
    extends Object
    Helper methods to manipulate lists.
    • Constructor Detail

      • ListUtil

        public ListUtil()
    • Method Detail

      • stringAsList

        public static List<String> stringAsList​(String st)
        Splits up a string of comma-separated substrings into a string list of those substrings.
        Parameters:
        st - string of comma-separated substrings.
        Returns:
        a list of substrings.
      • stringAsLanguageList

        public static List<LocaleId> stringAsLanguageList​(String input)
        Splits up a string of comma-separated substrings representing locale codes into a string list of LocaleId objects.
        Parameters:
        input - string of comma-separated substrings.
        Returns:
        a list of LocaleId objects.
      • stringAsList

        public static void stringAsList​(List<String> list,
                                        String st)
        Splits up a string of comma-separated substrings into a string list of those substrings.
        Parameters:
        list - a list to put the substrings.
        st - string of comma-separated substrings.
      • stringAsList

        public static List<String> stringAsList​(String st,
                                                String delimiter)
        Splits up a string of delimited substrings into a string list of those substrings.
        Parameters:
        st - string of delimited substrings.
        delimiter - a string delimiting substrings in the string.
        Returns:
        a list of substrings.
      • stringAsList

        public static List<String> stringAsList​(String st,
                                                char delimiter)
        Splits up a string of delimited substrings into a string list of those substrings.
        Parameters:
        st - string of delimited substrings.
        delimiter - a character delimiting substrings in the string.
        Returns:
        a list of substrings.
      • stringAsList

        public static void stringAsList​(List<String> list,
                                        String st,
                                        String delimiter)
        Splits up a string of delimited substrings into a string list of those substrings.
        Parameters:
        list - a list to put the substrings.
        st - string of delimited substrings.
        delimiter - a string delimiting substrings in the string.
      • stringAsArray

        public static String[] stringAsArray​(String st)
        Splits up a string of comma-separated substrings into an array of those substrings.
        Parameters:
        st - string of comma-separated substrings.
        Returns:
        the generated array of strings.
      • stringAsArray

        public static String[] stringAsArray​(String st,
                                             String delimiter)
        Splits up a string of comma-separated substrings into an array of those substrings.
        Parameters:
        st - string of comma-separated substrings.
        delimiter - a string delimiting substrings in the string.
        Returns:
        the generated array of strings.
      • merge

        public static String merge​(String[] array,
                                   int start,
                                   int end,
                                   String joiner)
        Merges specified elements of a given string array into a single string. The merged elements are joined with a given joiner.
        Parameters:
        array - the given array of strings.
        start - index of the start element to be merged.
        end - index of the end element (inclusive) to be merged.
        joiner - string to join elements in the resulting string.
        Returns:
        the string of merged elements.
      • merge

        public static String merge​(List<String> list,
                                   int start,
                                   int end,
                                   String joiner)
        Merges specified items of a given string list into a single string. The merged items are joined with a given joiner.
        Parameters:
        list - the given list of strings.
        start - index of the start item to be merged.
        end - index of the end item (inclusive) to be merged.
        joiner - string to join items in the resulting string.
        Returns:
        the string of merged items.
      • stringAsIntList

        public static List<Integer> stringAsIntList​(String st)
        Converts a string of comma-separated numbers into a list of integers.
        Parameters:
        st - string of comma-separated numbers.
        Returns:
        a list of integers.
      • stringAsIntList

        public static List<Integer> stringAsIntList​(String st,
                                                    String delimiter)
        Converts a string of comma-separated numbers into a list of integers and sorts the list ascendantly.
        Parameters:
        st - string of comma-separated numbers
        delimiter - a string delimiting numbers in the string
        Returns:
        a list of integers
      • stringAsIntList

        public static List<Integer> stringAsIntList​(String st,
                                                    String delimiter,
                                                    boolean sortList)
        Converts a string of comma-separated numbers into a list of integers.
        Parameters:
        st - string of comma-separated numbers
        delimiter - a string delimiting numbers in the string
        sortList - if the numbers in the resulting list should be sorted (ascendantly)
        Returns:
        a list of integers
      • listTrimTrail

        public static void listTrimTrail​(List<String> list)
        Remove empty trailing elements of the given list. Possible empty elements in the head and middle of the list remain if located before a non-empty element.
        Parameters:
        list - the list to be trimmed.
      • listTrimValues

        public static List<String> listTrimValues​(List<String> list)
        Creates a list, containing all trimmed values of a given list of strings. Empty elements remain on the list, non-empty ones are trimmed from both sides. The given list is not changed.
        Parameters:
        list - the given list of strings.
        Returns:
        the list with trimmed elements.
      • stringListAsArray

        public static String[] stringListAsArray​(List<String> list)
        Converts a list of strings into an array of those strings.
        Parameters:
        list - List of strings.
        Returns:
        an array of strings.
      • classListAsArray

        public static Class<?>[] classListAsArray​(List<Class<?>> list)
        Converts a list of class references into an array of those class references.
        Parameters:
        list - List of class references.
        Returns:
        An array of class references.
      • objectListAsArray

        public static Object[] objectListAsArray​(List<Object> list)
        Converts a list of objects into an array of those objects.
        Parameters:
        list - List of objects.
        Returns:
        an array of objects.
      • arrayAsList

        public static <E> List<E> arrayAsList​(E[] array)
        Converts a given array to a list containing all elements of the original array. The resulting list is not backed by the given array. (Changes to the returned list DO NOT "write through" to the array and vice versa.)
        Type Parameters:
        E - the type of the array's element.
        Parameters:
        array - the given array.
        Returns:
        the list of all elements contained in the given array.
      • arrayAsString

        public static String arrayAsString​(String[] array)
        Returns a string, representing a given array of strings. Array elements are separated with a comma in the resulting string.
        Parameters:
        array - the given array of strings.
        Returns:
        the string with comma-separated elements of the given array.
      • arrayAsString

        public static String arrayAsString​(String[] array,
                                           String delimiter)
        Returns a string, representing a given array of strings. Array elements in the resulting string are separated with a given delimiter.
        Parameters:
        array - the given array of strings.
        delimiter - the given delimiter.
        Returns:
        the string with delimited elements of the given array.
      • listAsString

        public static String listAsString​(List<String> list)
        Returns a string, representing a given list of strings. List elements are separated with a comma in the resulting string.
        Parameters:
        list - the given list of strings.
        Returns:
        the string with comma-separated elements of the given list.
      • listAsString

        public static String listAsString​(List<String> list,
                                          String delimiter)
        Returns a string, representing a given list of strings. List elements in the resulting string are separated with a given delimiter.
        Parameters:
        list - the given list of strings.
        delimiter - the given delimiter.
        Returns:
        the string with delimited elements of the given list.
      • intListAsString

        public static String intListAsString​(List<Integer> list)
        Returns a string, representing a given list of integers. List elements are converted into strings ("12" for 12 etc.) and separated with a comma in the resulting string.
        Parameters:
        list - the given list of integers.
        Returns:
        the string with comma-separated converted elements of the given list.
      • intListAsString

        public static String intListAsString​(List<Integer> list,
                                             String delimiter)
        Returns a string, representing a given list of integers. List elements are converted into strings ("12" for 12 etc.) and separated with a given delimiter.
        Parameters:
        list - the given list of integers.
        delimiter - the given delimiter.
        Returns:
        the string with delimited converted elements of the given list.
      • invert

        public static <E> List<E> invert​(List<E> list)
        Returns a copy of the specified list in reverse order. Differs from Collections#reverse in that it does not modify the original list.
        Parameters:
        list - the list to reverse.
        Returns:
        a copy of the reverted list.
      • remove

        public static <E> void remove​(List<E> list,
                                      int start,
                                      int end)
        Removes a range of elements from a given list.
        Type Parameters:
        E - the type of the list's element.
        Parameters:
        list - the given list.
        start - the start index (inclusive) to be removed.
        end - the end index (exclusive) to be removed.
      • copyItems

        public static <E> List<E> copyItems​(List<E> list,
                                            int start,
                                            int end)
        Creates a new list from a range of elements of a given list.
        Type Parameters:
        E - the type of the list's element.
        Parameters:
        list - the given list.
        start - the start index (inclusive) to be included in the resulting list.
        end - the end index (exclusive) to be included in the resulting list.
        Returns:
        a new list, containing all elements in the specified range of the given list.
      • moveItems

        public static <E> List<E> moveItems​(List<E> buffer,
                                            int start,
                                            int end)
        Creates a new list and moves therein a range of elements from a given list. The given list won't contain the moved elements anymore.
        Type Parameters:
        E - the type of the list's element.
        Parameters:
        buffer - the given list.
        start - the start index (inclusive) to be included in the resulting list.
        end - the end index (exclusive) to be included in the resulting list.
        Returns:
        a new list, containing all elements in the specified range of the given list.
      • moveItems

        public static <E> List<E> moveItems​(List<E> buffer)
        Moves all items from a given list to a newly created list. The given list won't contain the moved elements anymore.
        Type Parameters:
        E - the type of the list's element.
        Parameters:
        buffer - the given list.
        Returns:
        the resulting list, containing all elements of the given list.
      • getFirstNonNullItem

        public static <E> E getFirstNonNullItem​(List<E> list)
        Gets a first non-null element of a given list.
        Type Parameters:
        E - the type of the list's element.
        Parameters:
        list - the given list.
        Returns:
        the found element or null if nothing found (all elements are null, or the list is empty).
      • loadList

        public static List<String> loadList​(Class<?> classRef,
                                            String resourceLocation)
        Creates a new list of strings and fills it with the data read from a given resource.
        Parameters:
        classRef - reference to a class associated with the given resource.
        resourceLocation - the name of resource.
        Returns:
        the new list loaded from the resource.
      • loadList

        public static void loadList​(List<String> list,
                                    Class<?> classRef,
                                    String resourceLocation)
        Fills in an existing list of strings with the data read from a given resource.
        Parameters:
        list - the given list of strings.
        classRef - reference to a class associated with the given resource.
        resourceLocation - the name of resource.