Java Reference
In-Depth Information
Display 15.37
Set<T>
Class
(part 3 of 4)
52
public
void
output( )
53
{
54
Node position = head;
55
while
(position != null)
56
{
57
System.out.print(position.data.toString( ) + " ");
58
position = position.link;
59
}
60
System.out.println( );
61
}
62
/**
63
Returns a new set that is the union
64
of this set and the input set.
65
*/
66
public
Set<T> union(Set<T> otherSet)
67
{
68
Set<T> unionSet =
new
Set<T>( );
69
// Copy this set to unionSet.
70
Node<T> position = head;
71
while
(position !=
null
)
72
{
73
unionSet.add(position.data);
74
position = position.link;
75
}
76
// Copy otherSet items to unionSet.
77
// The add method eliminates any duplicates.
78
position = otherSet.head;
79
while
(position != null)
80
{
81
unionSet.add(position.data);
82
position = position.link;
83
}
84
return
unionSet;
85
}
86
/**
87
Returns a new set that is the intersection
88
of this set and the input set.
89
*/
90
public
Set<T> intersection(Set<T> otherSet)
91
{
92
Set<T> interSet =
new
Set<T>( );
93
// Copy only items in both sets.
94
Node<T> position = head;
95
while
(position != null)
96
{
97
if
(otherSet.contains(position.data))