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 java.lang.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​(java.util.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​(java.util.Comparator<? super T> comp,
                                                      java.lang.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
      • of

        public static <T extends java.lang.Comparable<T>> PersistentTreeSet<T> of​(java.lang.Iterable<T> items)
        Returns a new PersistentTreeSet of the given comparable items.
      • 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 java.util.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 java.util.SortedSet<E>
      • contains

        public boolean contains​(java.lang.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 java.util.Collection<E>
        Specified by:
        contains in interface java.util.Set<E>
        Specified by:
        contains in interface UnmodSet<E>
      • iterator

        public UnmodSortedIterator<E> iterator()
        Iterates over contents in a guaranteed order. Iterates over contents with no guarantees about their ordering. An unmodifiable iterator A one-time use, mutable, not-thread-safe way to get each value of the underling collection in turn. I experimented with various thread-safe alternatives, but the JVM is optimized around iterators so this is the lowest common denominator of collection iteration, even though iterators are inherently mutable.
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface ImSortedSet<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.Set<E>
        Specified by:
        iterator in interface UnmodCollection<E>
        Specified by:
        iterator in interface UnmodIterable<E>
        Specified by:
        iterator in interface UnmodSet<E>
        Specified by:
        iterator in interface UnmodSortedCollection<E>
        Specified by:
        iterator in interface UnmodSortedIterable<E>
        Specified by:
        iterator in interface UnmodSortedSet<E>
      • equals

        public boolean equals​(java.lang.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 java.util.Collection<E>
        Specified by:
        equals in interface java.util.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 java.util.SortedSet<E>
      • head

        public 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 java.util.Collection<E>
        Specified by:
        isEmpty in interface java.util.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 java.util.SortedSet<E>
      • put

        public 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 java.util.Collection<E>
        Specified by:
        size in interface java.util.Set<E>
        Specified by:
        size in interface Sized
      • subSet

        public ImSortedSet<E> subSet​(E fromElement,
                                     E toElement)
        Return the elements in this set from the start element (inclusive) to the end element (exclusive)
        Specified by:
        subSet in interface ImSortedSet<E>
        Specified by:
        subSet in interface java.util.SortedSet<E>
        Specified by:
        subSet in interface UnmodSortedSet<E>