Class PersistentTreeSet<E>

    • Field Detail

      • EMPTY

        public static final PersistentTreeSet EMPTY
        Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering"). An attempt to use it with other items will blow up at runtime. Either a withComparator() method will be added, or this will be removed.
    • Method Detail

      • empty

        public static <T extends Comparable<T>> PersistentTreeSet<T> empty()
        Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering"). An attempt to use it with other items will blow up at runtime. Either a withComparator() method will be added, or this will be removed.
      • ofComp

        public static <T> PersistentTreeSet<T> ofComp​(Comparator<? super T> comp)
        Returns a new PersistentTreeSet of the given comparator. Always use this instead of starting with empty() because there is no way to assign a comparator to an existing set.
      • ofComp

        public static <T> PersistentTreeSet<T> ofComp​(Comparator<? super T> comp,
                                                      Iterable<T> elements)
        Returns a new PersistentTreeSet of the given comparator and items.
        Parameters:
        comp - A comparator that defines the sort order of elements in the new set. This becomes part of the set (it's not for pre-sorting).
        elements - items to go into the set. In the case of a duplicate element, later values in the input list overwrite the earlier ones.
        Returns:
        a new PersistentTreeSet of the specified comparator and the given elements
      • ofMap

        public static <T> PersistentTreeSet<T> ofMap​(ImSortedMap<T,​?> i)
        Returns a new PersistentTreeSet of the keys and comparator in the given map. Since PersistentTreeSet is just a wrapper for a PersistentTreeMap, this can be a very cheap operation.
      • comparator

        public Comparator<? super E> comparator()
        Returns the comparator used to order the items in this set, or null if it uses Fn2.DEFAULT_COMPARATOR (for compatibility with java.util.SortedSet).
        Specified by:
        comparator in interface SortedSet<E>
      • contains

        public boolean contains​(Object o)
        Returns true if the set contains the given item in O(log n) time. This is the defining method of a set.
        Specified by:
        contains in interface Collection<E>
        Specified by:
        contains in interface Set<E>
        Specified by:
        contains in interface UnmodSet<E>
      • equals

        public boolean equals​(Object other)
        This is designed to be correct, rather than fully compatible with TreeSet.equals(). TreeSet.equals() does not take ordering into account and this does. You want better equality? Define an Equator. This is for Java@trade; interop.
        Specified by:
        equals in interface Collection<E>
        Specified by:
        equals in interface Set<E>
        Overrides:
        equals in class AbstractUnmodSet<E>
      • first

        public E first()
        Use head() inherited from Sequence instead of this method which is inherited from SortedSet. This method returns the first element if it exists, or throws a NoSuchElementException if the set is empty. head() returns an Option of the first element where as this method throws an exception if this set is empty. I had to rename the method on Sequence from first() to head() to work around this. Also returning an Option is thread-safe for building a lazy sequence. The alternative, examining the rest() of a sequence to see if it's == Sequence.empty() gets ugly very quickly and makes many transformations eager (especially flatMap).
        Specified by:
        first in interface SortedSet<E>
      • head

        @NotNull
        public @NotNull Option<E> head()
        The first item in this iterable.
        Specified by:
        head in interface Transformable<E>
        Specified by:
        head in interface UnmodIterable<E>
        Returns:
        an eagerly evaluated result which is a single item.
      • isEmpty

        public boolean isEmpty()
        This is a convenience method inherited from Collection that returns true if size() == 0 (if this set contains no elements).
        Specified by:
        isEmpty in interface Collection<E>
        Specified by:
        isEmpty in interface Set<E>
        Specified by:
        isEmpty in interface UnmodCollection<E>
        Specified by:
        isEmpty in interface UnmodSet<E>
      • last

        public E last()
        Inherited from SortedSet, returns the last item in this set, or throw an exception if this set is empty. Good luck with that.
        Specified by:
        last in interface SortedSet<E>
      • put

        @NotNull
        public @NotNull PersistentTreeSet<E> put​(E e)
        Adds an element. If the element already exists in this set, the new value overwrites the old one. If the new element is the same as an old element (based on the address of that item in memory, not an equals test), the old set may be returned unchanged.
        Specified by:
        put in interface BaseSet<E>
        Specified by:
        put in interface ImSortedSet<E>
        Parameters:
        e - the element to add to this set
        Returns:
        a new set with the element added (see note above about adding duplicate elements).
      • size

        public int size()
        The size of this set.
        Specified by:
        size in interface Collection<E>
        Specified by:
        size in interface Set<E>
        Specified by:
        size in interface Sized