com.hazelcast.util

## Class CollectionUtil

- java.lang.Object
- - com.hazelcast.util.CollectionUtil

-     public final class CollectionUtil
      extends Object

  Various collection utility methods.

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static <K,V> List<V>` | `addToValueList(Map<K,List<V>> map, K key, V value)` Adds a value to a list of values in the map.
    `static <T> T` | `getItemAtPositionOrNull(Collection<T> collection, int position)` Returns the n-th item or `null` if collection is smaller.
    `static boolean` | `isEmpty(Collection collection)` Returns `true` if the given collection is `null` or empty, otherwise returns `false`.
    `static boolean` | `isNotEmpty(Collection collection)` Returns `true` if the given collection is not `null` and not empty, otherwise returns `false`.
    `static <T> Collection<T>` | `nullToEmpty(Collection<T> collection)` Returns an empty Collection if argument is null.
    `static <C> void` | `objectToDataCollection(Collection<C> objectCollection, Collection<Data> dataCollection, SerializationService serializationService, String errorMessage)` Converts a collection of any type to a collection of `Data`.
    `static <C> Collection<Data>` | `objectToDataCollection(Collection<C> collection, SerializationService serializationService)` Converts a collection of any type to a collection of `Data`.
    `static int[]` | `toIntArray(Collection<Integer> collection)` Converts a `Collection` to a primitive `int[]` array.
    `static List<Integer>` | `toIntegerList(int[] array)` Converts an int array to an Integer `List`.
    `static long[]` | `toLongArray(Collection<Long> collection)` Converts a `Collection` to a primitive `long[]` array.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### isEmpty

          public static boolean isEmpty(Collection collection)

      Returns `true` if the given collection is `null` or empty, otherwise returns `false`.
      Parameters:
        `collection` - the given collection
      Returns:
        `true` if collection is empty

    - #### isNotEmpty

          public static boolean isNotEmpty(Collection collection)

      Returns `true` if the given collection is not `null` and not empty, otherwise returns `false`.
      Parameters:
        `collection` - the given collection
      Returns:
        `true` if collection is not empty

    - #### addToValueList

          public static <K,V> List<V> addToValueList(Map<K,List<V>> map,
                                                     K key,
                                                     V value)

      Adds a value to a list of values in the map.

      Creates a new list if no list is found for the key.

      Parameters:
        `map` - the given map of lists
        `key` - the key of the target list
        `value` - the value to add to the target list
      Returns:
        the updated list of values

    - #### getItemAtPositionOrNull

          public static <T> T getItemAtPositionOrNull(Collection<T> collection,
                                                      int position)

      Returns the n-th item or `null` if collection is smaller.
      Parameters:
        `collection` - the given collection
        `position` - position of the wanted item
      Returns:
        the item on position or `null` if the given collection is too small
      Throws:
        `NullPointerException` - if collection is `null`

    - #### objectToDataCollection

          public static <C> Collection<Data> objectToDataCollection(Collection<C> collection,
                                                                    SerializationService serializationService)

      Converts a collection of any type to a collection of `Data`.
      Parameters:
        `collection` - the given collection
        `serializationService` - will be used for converting object to `Data`
      Returns:
        collection of data
      Throws:
        `NullPointerException` - if collection is `null` or contains a `null` item

    - #### objectToDataCollection

          public static <C> void objectToDataCollection(Collection<C> objectCollection,
                                                        Collection<Data> dataCollection,
                                                        SerializationService serializationService,
                                                        String errorMessage)

      Converts a collection of any type to a collection of `Data`.
      Parameters:
        `objectCollection` - object items
        `dataCollection` - data items
        `serializationService` - will be used for converting object to `Data`
        `errorMessage` - the errorMessage when an item is null
      Throws:
        `NullPointerException` - if collection is `null` or contains a `null` item

    - #### toIntArray

          public static int[] toIntArray(Collection<Integer> collection)

      Converts a `Collection` to a primitive `int[]` array.
      Parameters:
        `collection` - the given collection
      Returns:
        a primitive int[] array
      Throws:
        `NullPointerException` - if collection is `null`

    - #### toLongArray

          public static long[] toLongArray(Collection<Long> collection)

      Converts a `Collection` to a primitive `long[]` array.
      Parameters:
        `collection` - the given collection
      Returns:
        a primitive long[] array
      Throws:
        `NullPointerException` - if collection is `null`

    - #### toIntegerList

          public static List<Integer> toIntegerList(int[] array)

      Converts an int array to an Integer `List`.

      The returned collection can be modified after it is created; it isn't protected by an immutable wrapper.

      Parameters:
        `array` - the array
      Returns:
        the list. throws `NullPointerException` if array is null.

    - #### nullToEmpty

          public static <T> Collection<T> nullToEmpty(Collection<T> collection)

      Returns an empty Collection if argument is null.

Copyright © 2019. All rights reserved.