0

I have a task to do for my classes. I need a collection to keep unique values, but java.util.Set is not enough for me.

What I have to do - if I add words "dogs" and "cats" to collection and afterwards I try to add "dog" and "cat", in my collection will be only the "dogs" and "cats".

If I add "ABCDEFG" and afterwards I try to add "BCDE", "DEFG", etc., it will not be added to my collection too.

How can I write this code?

foxbuur
  • 169
  • 1
  • 9
  • What happens if you add `"dog"` and then you try to add `"dogs"`? – Paul Boddington Apr 11 '16 at 00:31
  • 1
    What happens if you have "dogs" and then remove "dog"? Retaining "tail" isn't the correct answer, I suppose ;-) – laune Apr 11 '16 at 00:34
  • 1
    Your requirements are unclear, but it sounds like you want to create a set such that if string _s_ is in the set, then **no** proper substring of _s_ will also be in the set. Is that what you need? – ajb Apr 11 '16 at 00:34
  • at this moment I'm guessing that the teacher did not consider this case. – foxbuur Apr 11 '16 at 00:37
  • Could we assume that in this case, the collection will be contain two words? Words won't be removed, we only add new words. – foxbuur Apr 11 '16 at 00:38
  • 1
    I think you need to ask the teacher for some clarifications. – ajb Apr 11 '16 at 00:40
  • You can still use (extend) a `HashSet`, but method add will have to be overridden: iterate the set and test (hint: contains) existing elements. – laune Apr 11 '16 at 00:41
  • Just a friendly tip, you may want to read over this page: [The How-To-Ask Guide](https://stackoverflow.com/help/how-to-ask) so you can always be sure that your questions are easily answerable and as clear as possible. Be sure to include any efforts you've made to fix the problem you're having, and what happened when you attempted those fixes. Also don't forget to your show code and any error messages! – Matt C Apr 11 '16 at 00:58
  • 1
    @laune extending `HashSet` in a way that totally changes the definition of `add` does not sound like a good idea. That would be a misuse of extension. I believe it would violate LSP, for starters. – ajb Apr 11 '16 at 01:03
  • @ajb But the stronger principle is to do homework with MAW (minimum amount of work) ;-) - But, seriously, the definition of add in java.util.set wouldn't be violated, right? The concept of *set* as in math suffers somewhat when by adding a string you add all substrings as well. Perhaps overloading `addAll( String )`? Just go for MAW - it's not worth more effort. – laune Apr 11 '16 at 05:54
  • a) we don't remove anything, b) if I add, in this order, "ABCD", "CDEF", "ABCDEF" there will be 3 words in collection, c) if I add "ABCDEF" and then "ABCD", "CDEF", there will be only 1 word ("ABCDEF"), d) if I add "ABCDEF" and then "EFGH", there will be 2 words in collection – foxbuur Apr 11 '16 at 11:12
  • @laune Suppose you override `add` with a method that doesn't add the string if it's a substring of some other existing string. Now a method that takes an abstract `Set` parameter can say `set.add(s); boolean b = set.contains(s);` and have the latter unexpectedly be `false`, which is not the correct behavior for a `Set`. Alternatively, if you also modify `contains` to return `true` for all substrings, then you'll get `set.contains(s)` returning `true` for strings that were never added with `add`, which also is unexpected behavior. So this *would* violate the correct `Set` behavior. – ajb Apr 12 '16 at 01:36
  • OK, now that you've clarified your requirements: what you need to do is write an `add` method that goes through every item that was previously added to the collection and check whether the new string is equal to or a substring of one of the previous strings. If you go through the entire collection without finding an existing string with this condition, then add it. Otherwise don't. – ajb Apr 12 '16 at 04:19
  • @ajb Actually, I think it is futile to discuss an extremely simplistic and utterly academic exercise, obviously designed for the singular goal to make students write that loop you've aptly described in your comment or I hinted at in mine. – laune Apr 12 '16 at 05:49

0 Answers0