Class ListExtensions


  • public final class ListExtensions
    extends java.lang.Object
    Extensions class for use with List objects.
    • Constructor Summary

      Constructors 
      Constructor Description
      ListExtensions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> boolean containAtleastOneObject​(java.util.List<T> toSearch, java.util.List<T> search)
      This Method look in the List toSearch if at least one Object exists in the List search.
      static java.util.List<java.util.List<java.lang.Integer>> getAllCombinations​(@NonNull java.util.List<java.lang.Integer> possibleNumbers, int combinationSize)
      Gets all possible combinations from the given list of Integer objects
      static <T> java.util.List<java.util.List<T>> getCombinations​(@NonNull java.util.List<T> possibleValues, int combinationSize)
      Gets all possible combinations from the given list
      static <T> T getFirst​(java.util.List<T> list)
      Gets the first object from the given List.
      static <T> T getLast​(java.util.List<T> list)
      Gets the last object from the given List.
      static <T> ModifiedCollections<T> getModifiedCollections​(java.util.Collection<T> previous, java.util.Collection<T> next)
      Gets the modified lists. finding from an old list which elements have been removed and which have been added.
      static <T> java.util.List<T> getSameElementsFromLists​(java.util.List<T> toSearch, java.util.List<T> search)
      This method decorates the retainAll method and returns the result in a new list
      static <T> boolean hasNext​(@NonNull java.util.List<T> list, T element)
      Checks if the given List has a next element from the given element
      static <T> boolean hasPrevious​(@NonNull java.util.List<T> list, T element)
      Checks if the given List has a next element from the given element
      static <T> boolean isEqualListOfArrays​(java.util.List<T[]> one, java.util.List<T[]> other)
      Compare the given two Collection objects in equality.
      static <T> boolean isFirst​(java.util.List<T> list, T element)
      Checks if the given element is the first in the given list.
      static <T> boolean isLast​(java.util.List<T> list, T element)
      Checks if the given element is the last in the given list.
      static <T> java.util.List<T> rearrange​(T element, @NonNull java.util.List<T> listToResort, int rearrangeToIndex)
      Rearrange the order from the given List to the given rearranged index
      static <T> T removeFirst​(java.util.List<T> list)
      Removes the first object from the given List.
      static <T> T removeLast​(java.util.List<T> list)
      Removes the last object from the given List.
      static <T> java.util.List<T> removeLastValues​(java.util.List<T> v, int remove)
      The Method removeLastValues(ArrayList, int) remove the last Values.
      static <T> java.util.List<T> revertOrder​(java.util.List<T> listToRevert)
      Reverts the order from the given List.
      static <T> void shuffle​(java.util.List<T> source, java.util.List<T> destination, int[] selectedElements)
      Shuffle selected elements in the source list to the destination list from the given indexes in the array selectedElements.
      static <T> void sortByProperty​(java.util.List<T> list, java.lang.String property, boolean ascending)
      Sort over the given property.
      static <T> java.util.List<java.util.List<T>> splitToParts​(java.util.Collection<T> collection, int size)
      Splits the given Collection to parts to the specified size and returns a list with the parts.
      static <T> T[] toArray​(@NonNull java.util.List<T> list)
      To array.
      static <T> T[] toArray​(T... elements)
      To array.
      static <T> java.util.List<T> toList​(java.util.Enumeration<T> enumaration)
      Converts the given enumaration to a Vector.
      static <T> java.util.List<T> toList​(java.util.Set<T> set)
      Converts the given Set to a list.
      static <T> java.lang.Object[] toObjectArray​(T... elements)
      Converts the given parameter elements to an object array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ListExtensions

        public ListExtensions()
    • Method Detail

      • containAtleastOneObject

        public static <T> boolean containAtleastOneObject​(java.util.List<T> toSearch,
                                                          java.util.List<T> search)
        This Method look in the List toSearch if at least one Object exists in the List search.
        Type Parameters:
        T - the generic type
        Parameters:
        toSearch - The List to search.
        search - The List to inspect.
        Returns:
        Returns true if there is at least one Object equal from the two List otherwise false.
      • getFirst

        public static <T> T getFirst​(java.util.List<T> list)
        Gets the first object from the given List.
        Type Parameters:
        T - the generic type
        Parameters:
        list - the List.
        Returns:
        Returns the first object from the given List or null if the List is empty or null.
      • getLast

        public static <T> T getLast​(java.util.List<T> list)
        Gets the last object from the given List.
        Type Parameters:
        T - the generic type
        Parameters:
        list - the List.
        Returns:
        Returns the last object from the given List or null if the List is empty or null.
      • getModifiedCollections

        public static <T> ModifiedCollections<T> getModifiedCollections​(java.util.Collection<T> previous,
                                                                        java.util.Collection<T> next)
        Gets the modified lists. finding from an old list which elements have been removed and which have been added.
        Type Parameters:
        T - the generic type
        Parameters:
        previous - the previous collection i.e. the collection from database.
        next - the next collection i.e. the current collection in the view.
        Returns:
        's the ModifiedLists in which the lists are keeped.
      • getSameElementsFromLists

        public static <T> java.util.List<T> getSameElementsFromLists​(java.util.List<T> toSearch,
                                                                     java.util.List<T> search)
        This method decorates the retainAll method and returns the result in a new list
        Type Parameters:
        T - the generic type
        Parameters:
        toSearch - The list to search
        search - the list to inspect
        Returns:
        the new list with the intersection of the objects
      • isEqualListOfArrays

        public static <T> boolean isEqualListOfArrays​(java.util.List<T[]> one,
                                                      java.util.List<T[]> other)
        Compare the given two Collection objects in equality.
        Type Parameters:
        T - the generic type of the elements
        Parameters:
        one - the one
        other - the other
        Returns:
        true, if the given two Collection objects are equal otherwise false
      • isFirst

        public static <T> boolean isFirst​(java.util.List<T> list,
                                          T element)
        Checks if the given element is the first in the given list.
        Type Parameters:
        T - the generic type
        Parameters:
        list - the list
        element - the element
        Returns:
        true if the given element is the first otherwise false
      • isLast

        public static <T> boolean isLast​(java.util.List<T> list,
                                         T element)
        Checks if the given element is the last in the given list.
        Type Parameters:
        T - the generic type
        Parameters:
        list - the list
        element - the element
        Returns:
        true if the given element is the last otherwise false
      • hasNext

        public static <T> boolean hasNext​(@NonNull
                                          @NonNull java.util.List<T> list,
                                          T element)
        Checks if the given List has a next element from the given element
        Type Parameters:
        T - the generic type
        Parameters:
        list - the list
        element - the element
        Returns:
        true, if successful
      • hasPrevious

        public static <T> boolean hasPrevious​(@NonNull
                                              @NonNull java.util.List<T> list,
                                              T element)
        Checks if the given List has a next element from the given element
        Type Parameters:
        T - the generic type
        Parameters:
        list - the list
        element - the element
        Returns:
        true, if successful
      • rearrange

        public static <T> java.util.List<T> rearrange​(@NonNull
                                                      T element,
                                                      @NonNull
                                                      @NonNull java.util.List<T> listToResort,
                                                      int rearrangeToIndex)
        Rearrange the order from the given List to the given rearranged index
        Type Parameters:
        T - the generic type of the elements
        Parameters:
        listToResort - the list to resort
        element - the element to rearrange
        rearrangeToIndex - the rearrange to index
        Returns:
        the rearranged List
      • removeFirst

        public static <T> T removeFirst​(java.util.List<T> list)
        Removes the first object from the given List.
        Type Parameters:
        T - the generic type
        Parameters:
        list - the List.
        Returns:
        Removes and returns the first object from the given List or null if the List is empty or null.
      • removeLast

        public static <T> T removeLast​(java.util.List<T> list)
        Removes the last object from the given List.
        Type Parameters:
        T - the generic type
        Parameters:
        list - the List.
        Returns:
        Removes and returns the last object from the given List or null if the List is empty or null.
      • removeLastValues

        public static <T> java.util.List<T> removeLastValues​(java.util.List<T> v,
                                                             int remove)
        The Method removeLastValues(ArrayList, int) remove the last Values.
        Type Parameters:
        T - the generic type
        Parameters:
        v - The Vector with the received Messages.
        remove - How much to remove.
        Returns:
        the list
      • revertOrder

        public static <T> java.util.List<T> revertOrder​(java.util.List<T> listToRevert)
        Reverts the order from the given List.
        Type Parameters:
        T - the generic type
        Parameters:
        listToRevert - The List to revert.
        Returns:
        The reverted List.
      • shuffle

        public static <T> void shuffle​(java.util.List<T> source,
                                       java.util.List<T> destination,
                                       int[] selectedElements)
        Shuffle selected elements in the source list to the destination list from the given indexes in the array selectedElements.
        Type Parameters:
        T - the generic type
        Parameters:
        source - the source
        destination - the destination
        selectedElements - the selected elements
      • sortByProperty

        public static <T> void sortByProperty​(java.util.List<T> list,
                                              java.lang.String property,
                                              boolean ascending)
        Sort over the given property. Note: the property should be implement the Comparable interface.
        Type Parameters:
        T - the generic type of the list
        Parameters:
        list - the list to sort.
        property - the property to sort.
        ascending - if true the sort will be ascending ohterwise descending.
      • splitToParts

        public static <T> java.util.List<java.util.List<T>> splitToParts​(java.util.Collection<T> collection,
                                                                         int size)
        Splits the given Collection to parts to the specified size and returns a list with the parts.
        Type Parameters:
        T - the generic type
        Parameters:
        collection - The collection to split
        size - How to split.
        Returns:
        a List with the splited Parts
      • toArray

        @SafeVarargs
        public static <T> T[] toArray​(T... elements)
        To array.
        Type Parameters:
        T - the generic type
        Parameters:
        elements - the elements
        Returns:
        the t[]
      • toArray

        public static <T> T[] toArray​(@NonNull
                                      @NonNull java.util.List<T> list)
        To array.
        Type Parameters:
        T - the generic type
        Parameters:
        list - the list
        Returns:
        the array or null if the list is empty
      • toList

        public static <T> java.util.List<T> toList​(java.util.Enumeration<T> enumaration)
        Converts the given enumaration to a Vector.
        Type Parameters:
        T - the generic type
        Parameters:
        enumaration - The Enumeration to convert.
        Returns:
        A new Vector with the content of the given Enumeration.
      • toList

        public static <T> java.util.List<T> toList​(java.util.Set<T> set)
        Converts the given Set to a list.
        Type Parameters:
        T - the generic type of the elements
        Parameters:
        set - the set
        Returns:
        A new list
      • toObjectArray

        @SafeVarargs
        public static <T> java.lang.Object[] toObjectArray​(T... elements)
        Converts the given parameter elements to an object array.
        Type Parameters:
        T - the generic type
        Parameters:
        elements - The elements that will be in the returned object array
        Returns:
        An Object array.
      • getAllCombinations

        public static java.util.List<java.util.List<java.lang.Integer>> getAllCombinations​(@NonNull
                                                                                           @NonNull java.util.List<java.lang.Integer> possibleNumbers,
                                                                                           int combinationSize)
        Gets all possible combinations from the given list of Integer objects
        Parameters:
        possibleNumbers - the possible numbers
        combinationSize - the size of the combination to generate
        Returns:
        all possible combinations from the given list of Integer objects
      • getCombinations

        public static <T> java.util.List<java.util.List<T>> getCombinations​(@NonNull
                                                                            @NonNull java.util.List<T> possibleValues,
                                                                            int combinationSize)
        Gets all possible combinations from the given list
        Type Parameters:
        T - the generic type of the elements in the list
        Parameters:
        combinationSize - the size of the elements of the combinations to generate
        possibleValues - the list with the element values
        Returns:
        all possible combinations from the given list