/** * Constructs a new instance of {@code TreeSet} containing the elements of the specified SortedSet * and using the same Comparator. * * @param set the SortedSet of elements to add. */ public TreeSet(SortedSet<E> set) { this(set.comparator()); Iterator<E> it = set.iterator(); while (it.hasNext()) { add(it.next()); } }
/** * Adds all of the elements in the specified collection to this set. * * @param c collection containing elements to be added to this set * @return {@code true} if this set changed as a result of the call * @throws ClassCastException if the elements provided cannot be compared with the elements * currently in the set * @throws NullPointerException if the specified collection is null or if any element is null and * this set uses natural ordering, or its comparator does not permit null elements */ public boolean addAll(Collection<? extends E> c) { // Use linear-time version if applicable if (m.size() == 0 && c.size() > 0 && c instanceof SortedSet && m instanceof TreeMap) { SortedSet<? extends E> set = (SortedSet<? extends E>) c; TreeMap<E, Object> map = (TreeMap<E, Object>) m; Comparator<? super E> cc = (Comparator<? super E>) set.comparator(); Comparator<? super E> mc = map.comparator(); if (cc == mc || (cc != null && cc.equals(mc))) { map.addAllForTreeSet(set, PRESENT); return true; } } return super.addAll(c); }
/** @tests java.util.TreeSet#TreeSet(java.util.SortedSet) */ public void test_ConstructorLjava_util_SortedSet() { // Test for method java.util.TreeSet(java.util.SortedSet) ReversedIntegerComparator comp = new ReversedIntegerComparator(); SortedSet myTreeSet = db.createTreeSet("test", comp, null); for (int i = 0; i < objArray.length; i++) myTreeSet.add(objArray[i]); SortedSet anotherTreeSet = db.getTreeSet("test"); anotherTreeSet.addAll(myTreeSet); assertTrue("TreeSet is not correct size", anotherTreeSet.size() == objArray.length); for (int counter = 0; counter < objArray.length; counter++) assertTrue( "TreeSet does not contain correct elements", anotherTreeSet.contains(objArray[counter])); assertEquals( "TreeSet does not answer correct comparator", anotherTreeSet.comparator().getClass(), comp.getClass()); assertEquals( "TreeSet does not use comparator", anotherTreeSet.first(), objArray[objArray.length - 1]); }
/** * Constructs a new tree set containing the same elements and using the same ordering as the * specified sorted set. * * @param s sorted set whose elements will comprise the new set * @throws NullPointerException if the specified sorted set is null */ public TreeSet(SortedSet<E> s) { this(s.comparator()); addAll(s); }
public static ImmutableSortedSet copyOfSorted(SortedSet sortedset) { Comparator comparator1 = sortedset.comparator(); if (comparator1 == null) comparator1 = NATURAL_ORDER; return copyOfInternal(comparator1, sortedset); }
/** @tests java.util.TreeSet#comparator() */ public void test_comparator() { // Test for method java.util.Comparator java.util.TreeSet.comparator() ReversedIntegerComparator comp = new ReversedIntegerComparator(); SortedSet myTreeSet = db.createTreeSet("test", comp, null); assertTrue("Answered incorrect comparator", myTreeSet.comparator() == comp); }