Class Sets


  • public final class Sets
    extends Object
    Static utility methods pertaining to Set instances. Also see this class's counterparts Lists, Maps and Queues.

    See the Guava User Guide article on Sets.

    Since:
    2.0 (imported from Google Collections Library)
    Author:
    Kevin Bourrillion, Jared Levy, Chris Povirk
    • Method Detail

      • newHashSet

        public static <E> HashSet<E> newHashSet()
        Creates a mutable, empty HashSet instance.

        Note: if mutability is not required, use ImmutableSet#of() instead.

        Note: if E is an Enum type, use EnumSet.noneOf(java.lang.Class<E>) instead.

        Returns:
        a new, empty HashSet
      • newHashSetWithExpectedSize

        public static <E> HashSet<E> newHashSetWithExpectedSize​(int expectedSize)
        Creates a HashSet instance, with a high enough "initial capacity" that it should hold expectedSize elements without growth. This behavior cannot be broadly guaranteed, but it is observed to be true for OpenJDK 1.6. It also can't be guaranteed that the method isn't inadvertently oversizing the returned set.
        Parameters:
        expectedSize - the number of elements you expect to add to the returned set
        Returns:
        a new, empty HashSet with enough capacity to hold expectedSize elements without resizing
        Throws:
        IllegalArgumentException - if expectedSize is negative
      • unmodifiableNavigableSet

        public static <E> NavigableSet<E> unmodifiableNavigableSet​(NavigableSet<E> set)
        Returns an unmodifiable view of the specified navigable set. This method allows modules to provide users with "read-only" access to internal navigable sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its collection views, result in an UnsupportedOperationException.

        The returned navigable set will be serializable if the specified navigable set is serializable.

        Parameters:
        set - the navigable set for which an unmodifiable view is to be returned
        Returns:
        an unmodifiable view of the specified navigable set
        Since:
        12.0