0

For an assignment I have to write a method which combines a number of sets into one set and returns it.

This is easy enough, but the interface specifies that the returned set should be backed by the group of sets, that is to say that making a change to one of the subsets affects the overset.

By creating a Set and using .addAll() on each of the subsets, will this result in the desired behavior? The Javadoc doesn't say if it's a shallow or deep copy.

d_inevitable
  • 4,381
  • 2
  • 29
  • 48
  • 2
    Well, you could just try it... – Oliver Charlesworth Apr 26 '14 at 23:39
  • I could, but I'm hours away from getting the program to a testable state. I want to ask now, when I'm seeing the problem, to give people time to weigh in before I write myself into a corner. – DieKatzchen Apr 26 '14 at 23:50
  • see this: http://stackoverflow.com/questions/393968/java-is-there-an-easy-quick-way-to-and-or-or-xor-together-sets http://www.codeproject.com/Questions/202720/union-of-two-set-in-java – Asif Bhutto Apr 27 '14 at 00:07
  • Neither of those articles says whether the resulting union would be affected by the subsets. Only other way I can think of is to add an extra Set to my class and modify my add and remove to also add and remove from that set. But that just feels terribly kludgy. – DieKatzchen Apr 27 '14 at 00:13
  • This is something you could test in 5 minutes in a separate 10-line test program. – Oliver Charlesworth Apr 27 '14 at 00:15
  • @user3577196: You don't need to try it in your big program. It's like 5 lines of Java code to test the behavior. But I think the docs are clear enough in this case: *Adds all of the elements in the specified collection...*, as in, the elements in the collection when `addAll()` is called. – Mark Peters Apr 27 '14 at 00:17
  • And by the way, the deleted answer you commented on actually did what you want, you just needed to read up on the docs for Guava's `Sets.union` instead of assuming it was incorrect. – Mark Peters Apr 27 '14 at 00:18
  • Good point. Bit of a derp moment. For anyone else who finds this question, the answer is no, changing the subsets does not affect the superset. Plan Kludge it is! – DieKatzchen Apr 27 '14 at 00:29

1 Answers1

0

For anyone else who finds this question, the answer is no, changing the subsets does not affect the superset if you've used .addAll() from the standard library.