0

I came across this questions:

Given:

public static Function<String,String> swap = s -> {
    if(s.equals("A"))
        return "N";
    else
        return s;
    };

And given:

Set<String> set = Set.of("A", "J", "T", "C");
set = set.stream()
    .map(swap)
    .collect(Collectors.toSet());
for(String s : set){
    System.out.print(s);
}

What is the output?

I can tell that the set will contain: N, J, T, C but the possible answers have a several permutation of those four letters.

According to the documentation: Collections.ToSet() returns a collector that is unordered, nothing in the documentation points to the possible new order of this Set.
My question
Am I missing anything in the doc that actually guarantees the order of elements in the new Set

Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53
  • 1
    Are you sure it's not a multiple-choice test? – John Dvorak Apr 24 '21 at 09:39
  • 4
    A `Set` in Java does not know the concept of "order". For example, the `Set` interface does not provide a `get(int index)` method. Furhtermore, the [documentation of `Set`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Set.html) explicitly states that: "*The iteration order of set elements is unspecified and is subject to change.*" – Turing85 Apr 24 '21 at 09:42
  • No, you are not unless it's an ordered set that you are collecting to. – Naman Apr 24 '21 at 09:44
  • @JohnDvorak, I am certain it is not a multiple choice ! The questions is actually supplied by Oracle so I am super confused ! :D – Moha the almighty camel Apr 24 '21 at 09:48
  • You could just run the code and check, the order will probably stay the same accross executions, but it depends on the internals – Lino Apr 24 '21 at 09:50
  • @Turing85, I think the order is subject to change in case of adding new elements or removing new elements, but if the elements are the same, the order will be the same, just not defiend, correct ? – Moha the almighty camel Apr 24 '21 at 09:56
  • 1
    @Mohathealmightycamel that is an assumption that is not stated in the documentation. If we take a look at [`Set::iterator`'s documentation](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Set.html#iterator()) it states that: "*The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).*" It does not say that `Set`s containing identical objects (wrt. `equals()`) will produce the same iteration order. It does not even guarantee that the order stays the same over consecutive calls to `Set::iterator`. – Turing85 Apr 24 '21 at 09:59
  • @Mohathealmightycamel tl;dr is: `Set`s are w.l.o.g. unordered and make no assumption / give no guarantee whatsover about any kind of order. The exception to this rules are `Set`-implementations that explicitly support order, e.g. implementations of [`SortedSet`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/SortedSet.html). – Turing85 Apr 24 '21 at 10:03
  • Since sets are unordered, you can answer `NJTC` or `CJNT` or some other order, and they must accept the answer. – Andreas Apr 24 '21 at 10:28

0 Answers0