Class IJCCollections


  • public final class IJCCollections
    extends java.lang.Object
    Utility methods related to collections. The prefix IJC is there just for convenience - to not conflict with JDK Collections class. Otherwise this class is completely independent on IJC code-base.
    Since:
    5.12
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Pair<java.util.List<T>,​java.util.List<T>> computeDiff​(java.util.List<? extends T> listA, java.util.List<? extends T> listB)
      Gets a symmetric difference of two lists.
      static <T> java.util.List<? extends T> listFromNullable​(T nullableElement)
      If nullableElement is non-null, returns a singleton list containing that reference; otherwise returns Collections.emptyList().
      static <T> java.util.Set<? extends T> setFromNullable​(T nullableElement)
      If nullableElement is non-null, returns a singleton set containing that reference; otherwise returns Collections.emptySet().
      static <T> java.util.stream.Collector<T,​com.google.common.collect.ImmutableList.Builder<T>,​com.google.common.collect.ImmutableList<T>> toImmutableList()
      Provides Collector which reduces stream into Guava ImmutableList.
      static <T,​K,​V>
      java.util.stream.Collector<T,​?,​com.google.common.collect.ImmutableMap<K,​V>>
      toImmutableMap​(java.util.function.Function<? super T,​? extends K> keyMapper, java.util.function.Function<? super T,​? extends V> valueMapper)
      Provides Collector which reduces stream into Guava ImmutableMap.
      • Methods inherited from class java.lang.Object

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

      • listFromNullable

        public static <T> java.util.List<? extends T> listFromNullable​(T nullableElement)
        If nullableElement is non-null, returns a singleton list containing that reference; otherwise returns Collections.emptyList().
        Type Parameters:
        T - type of the element
        Parameters:
        nullableElement - element or null
        Returns:
        the list
      • setFromNullable

        public static <T> java.util.Set<? extends T> setFromNullable​(T nullableElement)
        If nullableElement is non-null, returns a singleton set containing that reference; otherwise returns Collections.emptySet().
        Type Parameters:
        T - type of the element
        Parameters:
        nullableElement - element or null
        Returns:
        the set
      • computeDiff

        public static <T> Pair<java.util.List<T>,​java.util.List<T>> computeDiff​(java.util.List<? extends T> listA,
                                                                                      java.util.List<? extends T> listB)
        Gets a symmetric difference of two lists.

        The symmetric difference of two sets A and B is the set of elements that are in exactly one of A and B.

        Type Parameters:
        T - The common type of elements in the lists.
        Parameters:
        listA - The first list.
        listB - The second list.
        Returns:
        A pair of lists that contain elements found only in listA and those found only in listB respectively. Those two lists together comprise the symmetric difference of listA and listB.
        Since:
        6.1
      • toImmutableList

        public static <T> java.util.stream.Collector<T,​com.google.common.collect.ImmutableList.Builder<T>,​com.google.common.collect.ImmutableList<T>> toImmutableList()
        Provides Collector which reduces stream into Guava ImmutableList. Example usage:
         List<Integer> list = IntStream.range(0, 7)
            .boxed()
            .collect(IJCCollections.toImmutableList());
         list.add(42); // fails on runtime
         
        Since:
        17.9.0
      • toImmutableMap

        public static <T,​K,​V> java.util.stream.Collector<T,​?,​com.google.common.collect.ImmutableMap<K,​V>> toImmutableMap​(java.util.function.Function<? super T,​? extends K> keyMapper,
                                                                                                                                                       java.util.function.Function<? super T,​? extends V> valueMapper)
        Provides Collector which reduces stream into Guava ImmutableMap. Example usage:
          Map<Integer, String> map = IntStream.range(0, 7)
              .boxed()
              .collect(IJCCollections.toImmutableMap(Integer::intValue, integer -> integer + "asdf "));
         
        Since:
        19.27.0