@Override public TreeSet<T> reject(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return filter(predicate.negate()); }
/** * Returns a {@link java.util.stream.Collector} which may be used in conjunction with * {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link TreeSet}. * <p> * The natural comparator is used to compare TreeSet elements. * * @param <T> Component type of the List. * @return A io.vavr.collection.List Collector. */ public static <T extends Comparable<? super T>> Collector<T, ArrayList<T>, TreeSet<T>> collector() { return collector(Comparators.naturalComparator()); }
/** * Converts this to a {@link SortedSet}. * * @param comparator A comparator that induces an order of the SortedSet elements. * @return A new {@link TreeSet}. */ default SortedSet<T> toSortedSet(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return ValueModule.toTraversable(this, TreeSet.empty(comparator), value -> TreeSet.of(comparator, value), values -> TreeSet.ofAll(comparator, values)); }
/** * Creates a TreeSet from int values. * * @param elements int values * @return A new TreeSet of Integer values * @throws NullPointerException if elements is null */ public static TreeSet<Integer> ofAll(int... elements) { Objects.requireNonNull(elements, "elements is null"); return TreeSet.ofAll(Iterator.ofAll(elements)); }
@Test public void testTreeSet() throws Exception { TreeSet<A> src = TreeSet.of(new B("a", "b")); String json = MAPPER.writeValueAsString(new TreeSetPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":[{\"ExtFieldsPojoTest$B\":{\"a\":\"a\",\"b\":\"b\"}}]}"); TreeSetPojo pojo = MAPPER.readValue(json, TreeSetPojo.class); TreeSet<A> restored = pojo.getValue(); Assert.assertEquals(restored.filter(e -> e instanceof B).length(), 1); Assert.assertEquals(restored.head().a, "a"); Assert.assertEquals(((B) restored.head()).b, "b"); }
/** * Alias for {@link TreeSet#of(Comparator, Object)} * * @param <T> Component type of element. * @param comparator The comparator used to sort the elements * @param element An element. * @return A new {@link TreeSet} instance containing the given element */ public static <T> SortedSet<T> SortedSet(Comparator<? super T> comparator, T element) { return TreeSet.of(comparator, element); }
@Test public void testTreeSet() throws Exception { TreeSet<I> src = TreeSet.of(new B()); String json = MAPPER.writeValueAsString(new TreeSetPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":[{\"type\":\"b\"}]}"); TreeSetPojo pojo = MAPPER.readValue(json, TreeSetPojo.class); TreeSet<I> restored = pojo.getValue(); Assert.assertEquals(restored.filter(e -> e instanceof B).length(), 1); }
/** * Returns this {@code TreeSet} if it is nonempty, * otherwise {@code TreeSet} created from iterable, using existing comparator. * * @param other An alternative {@code Traversable} * @return this {@code TreeSet} if it is nonempty, * otherwise {@code TreeSet} created from iterable, using existing comparator. */ @Override public TreeSet<T> orElse(Iterable<? extends T> other) { return isEmpty() ? ofAll(tree.comparator(), other) : this; }
@Override public <C> Map<C, TreeSet<T>> groupBy(Function<? super T, ? extends C> classifier) { return Collections.groupBy(this, classifier, elements -> ofAll(comparator(), elements)); }
/** * Returns a TreeSet containing tuples returned by {@code n} calls to a given Supplier {@code s}. * * @param <T> Component type of the TreeSet * @param comparator The comparator used to sort the elements * @param n The number of elements in the TreeSet * @param s The Supplier computing element values * @return A TreeSet of size {@code n}, where each element contains the result supplied by {@code s}. * @throws NullPointerException if {@code comparator} or {@code s} are null */ public static <T> TreeSet<T> fill(Comparator<? super T> comparator, int n, Supplier<? extends T> s) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(s, "s is null"); return Collections.fill(n, s, TreeSet.empty(comparator), values -> of(comparator, values)); }
@Override public <U> TreeSet<U> map(Function<? super T, ? extends U> mapper) { return map(Comparators.naturalComparator(), mapper); }