Java Reference
In-Depth Information
Display 15.37 Set<T> Class (part 3 of 3)
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))
98 interSet.add(position.data);
99 position = position.link;
100 }
101
The clear , size , and isEmpty methods are identical
to those in Display 15.8 for the LinkedList3 class.
return interSet;
102 }
103 }
 
 
Search WWH ::




Custom Search