3

By a chance it happened to me twice that I got the same Java question during a job interview Java test. For me it seems like a nonsense. It goes something like this:

Which of this collections would you use if you needed a collection with no duplicates and with natural ordering?

  1. java.util.List
  2. java.util.Map
  3. java.util.Set
  4. java.util.Collection

The closest answer would be Set. But as far as I know these interfaces, with exception of List do not define any ordering in their contract but it is the matter of the implementing classes to have or not to have defined ordering.

Was I right in pointing out in the test that the question is wrong?

ps-aux
  • 11,627
  • 25
  • 81
  • 128
  • 1
    Definitely. The only correct answer is 'none of the above', or `java.util.SortedSet.` – user207421 May 28 '15 at 00:29
  • @MaxZoom Come off it. He knows that. Not what he's asking. Read the question. – user207421 May 28 '15 at 01:41
  • @EJP I added my point of view below. – MaxZoom May 28 '15 at 01:55
  • The question is not wrong. Clearly Map is inappropriate. Since List and Set both extend Collection the answer must be Collection. The class you willbprobaby end up using SortedSet implements both Set and Collection. – emory May 30 '15 at 22:08

4 Answers4

4

The first major clue is "no duplicates." A mathematical set contains only unique items, which means no duplicates, so you are correct here.

In terms of ordering, perhaps the interviewer was looking for you to expand upon your answer. Just as a "Set" extends a "Collection" (in Java), there are more specific types of "Sets" possible in Java. See: HashSet, TreeSet, LinkedHashSet. For example, TreeSet is inherited from SortedSet interface.

However, it is most definitely true that a Java set does not provide any ordering. Frankly, I think this is a poorly worded question and you were right to point out the lack in precision.

Community
  • 1
  • 1
2

Yes, you're correct that none of the answers given matches the requirements. A correct answer might have been SortedSet or its subinterface NavigableSet.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
2

A Set with natural ordering is a SortedSet (which extends Set so it is-a Set), and a concrete implementation of that interface is TreeSet (which implements SortedSet so it is-a Set).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • @MaxZoom In the context of a job interview, on a multiple choice exam I would mark "3. `java.util.Set`" (and if given the opportunity, I would explain my reasoning as set forth in my answer here). – Elliott Frisch May 28 '15 at 00:56
2

The correct answer for that test is Set Let's remember that it's asking for an interface that could provide that; given the right implementation, the Set interface could provide it.

  • The Map interface doesn't make any guarantees around what order
    things are stored, as that's implementation specific. However, if you use the right implementation (that is, [TreeMap][1] as spelled out by the docs), then you're guaranteed a natural ordering and no
    duplicate entries. However, there's no requirement about key-value pairs.

  • The Set interface also doesn't make any guarantees around what order things are stored in, as that's implementation specific. But, like TreeMap, [TreeSet][2] is a set that can be used to store things in a
    natural order with no duplicates. Here's how it'd look.
    Set<String> values = new TreeSet<>();

  • The List interface will definitely allow duplicates, which instantly rules it out.

  • The Collection interface doesn't have anything directly implementing it, but it is the patriarch of the entire collections hierarchy. So, in theory, code like this is legal:

    Collection<String> values = new TreeSet<>();

    ...but you'd lose information about what kind of collection it actually was, so I'd discourage its usage.

adadi
  • 71
  • 1
  • 8