The union of two collections consists of their contents combined into a new collection. Define a method union for the class BagOfStrings that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method’s one argument. Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. Specifically, suppose that bag1 and bag2 are BagOfStrings objects; bag1 contains the String objects a, b, and c; and bag2 contains the String objects b, b, d, and e. After the statement
BagOfStrings everything = bag1.union(bag2);
executes, the bag everything contains the strings a, b, b, b, c, d, and e. Note that union does not affect the contents of bag1 and bag2.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here