📄 treeset.java
字号:
* @return <tt>true</tt> 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 of the specified collection is null. */ public boolean addAll(Collection c) { // Use linear-time version if applicable if (m.size()==0 && c.size() > 0 && c instanceof SortedSet && m instanceof TreeMap) { SortedSet set = (SortedSet)c; TreeMap map = (TreeMap)m; Comparator cc = set.comparator(); Comparator mc = map.comparator(); if (cc==mc || (cc != null && cc.equals(mc))) { map.addAllForTreeSet(set, PRESENT); return true; } } return super.addAll(c); } /** * Returns a view of the portion of this set whose elements range from * <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, exclusive. (If * <tt>fromElement</tt> and <tt>toElement</tt> are equal, the returned * sorted set is empty.) The returned sorted set is backed by this set, * so changes in the returned sorted set are reflected in this set, and * vice-versa. The returned sorted set supports all optional Set * operations.<p> * * The sorted set returned by this method will throw an * <tt>IllegalArgumentException</tt> if the user attempts to insert an * element outside the specified range.<p> * * Note: this method always returns a <i>half-open range</i> (which * includes its low endpoint but not its high endpoint). If you need a * <i>closed range</i> (which includes both endpoints), and the element * type allows for calculation of the successor of a specified value, * merely request the subrange from <tt>lowEndpoint</tt> to * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt> * is a sorted set of strings. The following idiom obtains a view * containing all of the strings in <tt>s</tt> from <tt>low</tt> to * <tt>high</tt>, inclusive: <pre> * SortedSet sub = s.subSet(low, high+"\0"); * </pre> * * A similar technique can be used to generate an <i>open range</i> (which * contains neither endpoint). The following idiom obtains a view * containing all of the strings in <tt>s</tt> from <tt>low</tt> to * <tt>high</tt>, exclusive: <pre> * SortedSet sub = s.subSet(low+"\0", high); * </pre> * * @param fromElement low endpoint (inclusive) of the subSet. * @param toElement high endpoint (exclusive) of the subSet. * @return a view of the portion of this set whose elements range from * <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, * exclusive. * @throws ClassCastException if <tt>fromElement</tt> and * <tt>toElement</tt> cannot be compared to one another using * this set's comparator (or, if the set has no comparator, * using natural ordering). * @throws IllegalArgumentException if <tt>fromElement</tt> is greater than * <tt>toElement</tt>. * @throws NullPointerException if <tt>fromElement</tt> or * <tt>toElement</tt> is <tt>null</tt> and this set uses natural * order, or its comparator does not tolerate <tt>null</tt> * elements. */ public SortedSet subSet(Object fromElement, Object toElement) { return new TreeSet(m.subMap(fromElement, toElement)); } /** * Returns a view of the portion of this set whose elements are strictly * less than <tt>toElement</tt>. The returned sorted set is backed by * this set, so changes in the returned sorted set are reflected in this * set, and vice-versa. The returned sorted set supports all optional set * operations.<p> * * The sorted set returned by this method will throw an * <tt>IllegalArgumentException</tt> if the user attempts to insert an * element greater than or equal to <tt>toElement</tt>.<p> * * Note: this method always returns a view that does not contain its * (high) endpoint. If you need a view that does contain this endpoint, * and the element type allows for calculation of the successor of a * specified value, merely request a headSet bounded by * <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt> * is a sorted set of strings. The following idiom obtains a view * containing all of the strings in <tt>s</tt> that are less than or equal * to <tt>high</tt>: <pre> SortedSet head = s.headSet(high+"\0");</pre> * * @param toElement high endpoint (exclusive) of the headSet. * @return a view of the portion of this set whose elements are strictly * less than toElement. * @throws ClassCastException if <tt>toElement</tt> is not compatible * with this set's comparator (or, if the set has no comparator, * if <tt>toElement</tt> does not implement <tt>Comparable</tt>). * @throws IllegalArgumentException if this set is itself a subSet, * headSet, or tailSet, and <tt>toElement</tt> is not within the * specified range of the subSet, headSet, or tailSet. * @throws NullPointerException if <tt>toElement</tt> is <tt>null</tt> and * this set uses natural ordering, or its comparator does * not tolerate <tt>null</tt> elements. */ public SortedSet headSet(Object toElement) { return new TreeSet(m.headMap(toElement)); } /** * Returns a view of the portion of this set whose elements are * greater than or equal to <tt>fromElement</tt>. The returned sorted set * is backed by this set, so changes in the returned sorted set are * reflected in this set, and vice-versa. The returned sorted set * supports all optional set operations.<p> * * The sorted set returned by this method will throw an * <tt>IllegalArgumentException</tt> if the user attempts to insert an * element less than <tt>fromElement</tt>. * * Note: this method always returns a view that contains its (low) * endpoint. If you need a view that does not contain this endpoint, and * the element type allows for calculation of the successor of a specified * value, merely request a tailSet bounded by * <tt>successor(lowEndpoint)</tt>. For example, suppose that <tt>s</tt> * is a sorted set of strings. The following idiom obtains a view * containing all of the strings in <tt>s</tt> that are strictly greater * than <tt>low</tt>: <pre> * SortedSet tail = s.tailSet(low+"\0"); * </pre> * * @param fromElement low endpoint (inclusive) of the tailSet. * @return a view of the portion of this set whose elements are * greater than or equal to <tt>fromElement</tt>. * @throws ClassCastException if <tt>fromElement</tt> is not compatible * with this set's comparator (or, if the set has no comparator, * if <tt>fromElement</tt> does not implement <tt>Comparable</tt>). * @throws IllegalArgumentException if this set is itself a subSet, * headSet, or tailSet, and <tt>fromElement</tt> is not within the * specified range of the subSet, headSet, or tailSet. * @throws NullPointerException if <tt>fromElement</tt> is <tt>null</tt> * and this set uses natural ordering, or its comparator does * not tolerate <tt>null</tt> elements. */ public SortedSet tailSet(Object fromElement) { return new TreeSet(m.tailMap(fromElement)); } /** * Returns the comparator used to order this sorted set, or <tt>null</tt> * if this tree set uses its elements natural ordering. * * @return the comparator used to order this sorted set, or <tt>null</tt> * if this tree set uses its elements natural ordering. */ public Comparator comparator() { return m.comparator(); } /** * Returns the first (lowest) element currently in this sorted set. * * @return the first (lowest) element currently in this sorted set. * @throws NoSuchElementException sorted set is empty. */ public Object first() { return m.firstKey(); } /** * Returns the last (highest) element currently in this sorted set. * * @return the last (highest) element currently in this sorted set. * @throws NoSuchElementException sorted set is empty. */ public Object last() { return m.lastKey(); } /** * Returns a shallow copy of this <tt>TreeSet</tt> instance. (The elements * themselves are not cloned.) * * @return a shallow copy of this set. */ public Object clone() { TreeSet clone = null; try { clone = (TreeSet)super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(); } clone.m = new TreeMap(m); clone.keySet = clone.m.keySet(); return clone; } /** * Save the state of the <tt>TreeSet</tt> instance to a stream (that is, * serialize it). * * @serialData Emits the comparator used to order this set, or * <tt>null</tt> if it obeys its elements' natural ordering * (Object), followed by the size of the set (the number of * elements it contains) (int), followed by all of its * elements (each an Object) in order (as determined by the * set's Comparator, or by the elements' natural ordering if * the set has no Comparator). */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out any hidden stuff s.defaultWriteObject(); // Write out Comparator s.writeObject(m.comparator()); // Write out size s.writeInt(m.size()); // Write out all elements in the proper order. for (Iterator i=m.keySet().iterator(); i.hasNext(); ) s.writeObject(i.next()); } /** * Reconstitute the <tt>TreeSet</tt> instance from a stream (that is, * deserialize it). */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden stuff s.defaultReadObject(); // Read in Comparator Comparator c = (Comparator)s.readObject(); // Create backing TreeMap and keySet view m = (c==null ? new TreeMap() : new TreeMap(c)); keySet = m.keySet(); // Read in size int size = s.readInt(); ((TreeMap)m).readTreeSet(size, s, PRESENT); } private static final long serialVersionUID = -2479143000061671589L;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -