Class IterableUtils

  • All Implemented Interfaces:
    Utils

    public abstract class IterableUtils
    extends java.lang.Object
    implements Utils
    The utility class for Iterable
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Iterable
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> void forEach​(java.lang.Iterable<E> elements, java.util.function.Consumer<E> elementConsumer)
      Iterates over the given Iterable and applies the provided Consumer to each element.
      static boolean isIterable​(java.lang.Class<?> clazz)
      Checks if the given class is assignable from Iterable.
      static boolean isIterable​(java.lang.Object object)
      Checks if the given object is an instance of Iterable.
      static <E> void iterate​(java.lang.Iterable<E> elements, java.util.function.Consumer<E> elementConsumer)
      Iterates over the given Iterable and applies the provided Consumer to each element.
      • Methods inherited from class java.lang.Object

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

      • isIterable

        public static boolean isIterable​(@Nullable
                                         java.lang.Object object)
        Checks if the given object is an instance of Iterable.

        Example Usage

        
         List<String> list = Arrays.asList("a", "b", "c");
         boolean result1 = IterableUtils.isIterable(list);     // true
         boolean result2 = IterableUtils.isIterable("string"); // false
         boolean result3 = IterableUtils.isIterable(null);     // false
         
        Parameters:
        object - the object to check
        Returns:
        true if the object is an instance of Iterable, false otherwise
      • isIterable

        public static boolean isIterable​(@Nullable
                                         java.lang.Class<?> clazz)
        Checks if the given class is assignable from Iterable.

        Example Usage

        
         boolean result1 = IterableUtils.isIterable(List.class);  // true
         boolean result2 = IterableUtils.isIterable(String.class); // false
         boolean result3 = IterableUtils.isIterable(null);        // false
         
        Parameters:
        clazz - the class to check
        Returns:
        true if the class is assignable from Iterable, false otherwise
        See Also:
        ClassUtils.isAssignableFrom(Class, Class)
      • iterate

        public static <E> void iterate​(@Nullable
                                       java.lang.Iterable<E> elements,
                                       @Nonnull
                                       java.util.function.Consumer<E> elementConsumer)
        Iterates over the given Iterable and applies the provided Consumer to each element.

        Example Usage

        
         List<String> list = Arrays.asList("a", "b", "c");
         IterableUtils.iterate(list, System.out::println);
         // Output:
         // a
         // b
         // c
         
        Type Parameters:
        E - the type of elements in the Iterable
        Parameters:
        elements - the Iterable to iterate over
        elementConsumer - the Consumer to apply to each element
      • forEach

        public static <E> void forEach​(@Nullable
                                       java.lang.Iterable<E> elements,
                                       @Nonnull
                                       java.util.function.Consumer<E> elementConsumer)
        Iterates over the given Iterable and applies the provided Consumer to each element.

        This method is equivalent to iterate(Iterable, Consumer).

        Example Usage

        
         List<String> list = Arrays.asList("a", "b", "c");
         IterableUtils.forEach(list, System.out::println);
         // Output:
         // a
         // b
         // c
         
        Type Parameters:
        E - the type of elements in the Iterable
        Parameters:
        elements - the Iterable to iterate over
        elementConsumer - the Consumer to apply to each element
        See Also:
        iterate(Iterable, Consumer)