import java.util.*; public class SetExample9 { public static void main (String[] argv) { // Create an instance of a linked list and add some data. LinkedList oddGuys = new LinkedList(); oddGuys.add (1); oddGuys.add (3); oddGuys.add (5); oddGuys.add (7); oddGuys.add (9); // Another set. LinkedList primes = new LinkedList(); primes.add (1); primes.add (2); primes.add (3); primes.add (5); primes.add (7); // Set intersection alternatives. LinkedList intersection = computeIntersection (oddGuys, primes); System.out.println ("Intersection: " + intersection); intersection = computeIntersectionAlt1 (oddGuys, primes); System.out.println ("Intersection: " + intersection); intersection = computeIntersectionAlt2 (oddGuys, primes); System.out.println ("Intersection: " + intersection); intersection = computeIntersectionAlt3 (oddGuys, primes); System.out.println ("Intersection: " + intersection); intersection = computeIntersectionAlt4 (oddGuys, primes); System.out.println ("Intersection: " + intersection); intersection = computeIntersectionAlt5 (oddGuys, primes); System.out.println ("Intersection: " + intersection); LinkedList intersection2 = computeIntersectionAlt6 (oddGuys, primes); System.out.println ("Intersection: " + intersection2); } static LinkedList computeIntersection (LinkedList listA, LinkedList listB) { LinkedList listC = new LinkedList(); for (int i=0; i computeIntersectionAlt1 (LinkedList listA, LinkedList listB) { LinkedList listC = new LinkedList(); Iterator iter = listA.iterator(); while (iter.hasNext()) { Integer K = iter.next(); // To be in the intersection, K needs to be in both sets. if ( listB.contains(K) ) { listC.add (K); } } return listC; } static LinkedList computeIntersectionAlt2 (LinkedList listA, LinkedList listB) { LinkedList listC = new LinkedList(); Iterator iter = listA.iterator(); while (iter.hasNext()) { Integer K = (Integer) iter.next(); // To be in the intersection, K needs to be in both sets. if ( listB.contains(K) ) { listC.add (K); } } return listC; } static LinkedList computeIntersectionAlt3 (LinkedList listA, LinkedList listB) { LinkedList listC = new LinkedList(); for (Iterator iter = listA.iterator(); iter.hasNext(); ) { Integer K = (Integer) iter.next(); if ( listB.contains(K) ) { listC.add (K); } } return listC; } static LinkedList computeIntersectionAlt4 (LinkedList listA, LinkedList listB) { LinkedList listC = new LinkedList(); for (Integer K: listA) { if ( listB.contains(K) ) { listC.add (K); } } return listC; } static LinkedList computeIntersectionAlt5 (LinkedList listA, LinkedList listB) { LinkedList listC = new LinkedList (); Iterator iter = listA.iterator(); while (iter.hasNext()) { Integer K = (Integer) iter.next(); if ( listB.contains(K) ) { listC.add (K); } } return listC; } static LinkedList computeIntersectionAlt6 (LinkedList listA, LinkedList listB) { LinkedList listC = new LinkedList (); Iterator iter = listA.iterator(); while (iter.hasNext()) { Integer K = (Integer) iter.next(); if ( listB.contains(K) ) { listC.add (K); } } return listC; } }