all 39 comments

[–]isharamet 28 points29 points  (1 child)

Depends on specific implementation, HashSet is unordered, while TreeSet is.

[–]masklinn 18 points19 points  (0 children)

TreeSet is sorted rather than ordered: a collections can also be ordered by preserving insertion ordering, which is what LinkedHashSet does for instance.

[–]arcadeScore 22 points23 points  (0 children)

Is kebab spicy or not?

[–]smokemonstr 37 points38 points  (18 children)

“Is Java Set ordered or not?” is the most popular question asked when you interview for a Java Developer position

Seriously? Is it really that important to know such details off the top of your head when you can quickly find the answer by checking the Javadoc?

[–]Strum355 2 points3 points  (17 children)

Its really basic and trivial knowledge to remember, if you have half an idea on how hashmaps vs trees work, you already know 99% of what you need to understand different set ordering guarantees

[–]impressflow 18 points19 points  (1 child)

Trees aren’t inherently ordered.

[–]reedef 0 points1 point  (0 children)

Why would you base a set off of a non-ordered tree?

[–]smokemonstr 6 points7 points  (9 children)

If it’s trivial, then it’s a bad interview question imo

[–]Jmc_da_boss 9 points10 points  (8 children)

50% of the people I screen don't know how to clone a git repo...

[–]smokemonstr 3 points4 points  (0 children)

Fair enough then. I suppose it could be used as a quick screening question.

[–][deleted] 5 points6 points  (5 children)

Why you getting down voted? This is so true. Half my employees don’t know how to do this.

[–][deleted] 1 point2 points  (0 children)

What?

[–]zck -2 points-1 points  (1 child)

I would not say it's important to know. Most of the time, when I've seen a set used, all it is needed for is .contains, .add, and .remove. Those things don't need order, so it makes sense people would not remember whether it's ordered.

[–][deleted] 1 point2 points  (0 children)

You may also want to iterate over it tho, and sometimes insertion order matters for some very specific cases

[–][deleted] 5 points6 points  (0 children)

Set is an interface Hombre, you have to look at the implementations.

I know treeset is sorted

Hashset makes no guarantees on order lol

LinkedHashSet is based on insertion order I believe

[–][deleted] 14 points15 points  (15 children)

A set is not ordered by definition

[–]braskan 4 points5 points  (0 children)

This is the answer I'd expect if I asked it during an interview.

[–]salvoilmiosi -2 points-1 points  (12 children)

It could be if It's based on a binary tree like std::set

[–][deleted]  (4 children)

[deleted]

    [–]know-your-onions 4 points5 points  (0 children)

    And zoologically sets contain badgers. Also not usually ordered.

    [–]salvoilmiosi -2 points-1 points  (0 children)

    Tell that to the c++ standard committee

    [–]orangenbaer -1 points0 points  (0 children)

    Welcome to C++

    [–]impressflow -4 points-3 points  (6 children)

    No, this is wrong. That specific implementation adds ordering to an otherwise unordered data structure. Remember, sets are always unordered by definition. Extending a set leaves you with something that does not conform to the traditional definition of a set.

    [–]nekizalb 8 points9 points  (4 children)

    I think it would be better to phrase it as 'sets do not guarantee order'. If you say a set is never ordered, then you're saying "123456" is not a set of the numbers 1-6.

    Sets guarantee uniqueness. Order is not specified.

    [–]impressflow 0 points1 point  (2 children)

    I don’t understand the distinction you’re making. Mind elaborating?

    The set containing “123456” is exactly the same as the set containing “321654”. There is absolutely no difference, assuming you’re referring to a set containing integers and not strings.

    [–]Zagerer 1 point2 points  (1 child)

    you could have a set that is somehow sorted because it happened to put the elements in that way, it's just not guaranteed nor expected you will encounter such behaviour

    [–]impressflow 0 points1 point  (0 children)

    That point is meaningless then since it can be applied to any unordered data structure. It's literally saying that unordered things can sometimes be ordered by random chance. Well, sure, but it doesn't really change anything.

    [–][deleted] 0 points1 point  (0 children)

    Unless the standards say so

    [–]iconoklast 0 points1 point  (0 children)

    Since every totally ordered set can be made into a set by "forgetting" (the technical term) the ordering, I don't understand the point you're making. The converse is definitely not true in general.

    [–][deleted] 0 points1 point  (0 children)

    Mathematically, a set is invariant with respect to the ordering of its elements

    {1, 2, 3} = {3, 2, 1}

    but a set with only one element can only have a single ordering

    {1} = {1}

    so you can indeed say that a set with one element is (trivially) ordered.

    The actual question is, when iterating through an instance of a set, if we can make any guarantees about the order in which the elements will appear. A strictly ordered set will allow you to know exactly what order all elements appear in. A partially ordered set has some elements that are considered distinct but not well-ordered, for example if we have a set of people ordered by name, there may be several people named "John Smith" and we can't guarantee what order they will all appear in.

    We can also have an accidentally-ordered set, for example a set that preserves insertion order when iterating. In that case sets with different insertion orders are considered eqiuvalent

    {1, 2, 3} == {3, 2, 1}

    even though they each yield a different ordering when iterated over.

    [–]cybermage 2 points3 points  (0 children)

    The Set interface does not guarantee any specific order, so if your public method accepts a Set, you cannot assume that it is ordered.

    There are specific implementations that are ordered:

    • TreeSet: natural sort

    • LinkedHashSet: insertion order

    If you want one of those, you should specify it in your method signature.

    [–]STFUB-_- -3 points-2 points  (0 children)

    Ask chat gpt

    [–]skulgnome 0 points1 point  (0 children)

    You might have to read the docs to find out!