Java Reference
In-Depth Information
1
public void remove( )
2
{
3
if( expectedModCount != modCount )
4
throw new ConcurrentModificationException( );
5
6
if( lastVisited == null )
7
throw new IllegalStateException( );
8
9
AnyType valueToRemove = lastVisited.element;
10
11
TreeSet.this.remove( valueToRemove );
12
13
expectedModCount++;
14
visited--;
15
lastVisited = null;
16
17
if( !hasNext( ) )
18
return;
19
20
// Remaining code reinstates the stack, in case of rotations
21
AnyType nextValue = current.element;
22
path.clear( );
23
AANode<AnyType> p = root;
24
for( ; ; )
25
{
26
path.push( p );
27
int result = compare( nextValue, p.element );
28
if( result < 0 )
29
p = p.left;
30
else if( result > 0 )
31
p = p.right;
32
else
33
break;
34
}
35
path.pop( );
36
current = p;
37
}
figure 19.76
remove
method for
TreeSetIterator
by providing
compareTo
, while in
HashMap
it is extended by providing
equals
and
hashCode
.
Lines 17-21 declare the three abstract methods. These are factories that
create the appropriate concrete object and return it through the interface type.
For instance, in
TreeMap
,
makeEmptyKeySet
returns a newly constructed
TreeSet
,
whereas in
HashMap
,
makeEmptyKeySet
returns a newly constructed
HashSet
.
Most important,
makePair
creates an object of type
Map.Entry
that represents
Search WWH ::
Custom Search