Nati
Nati

Reputation: 1032

Guava Joiner on SortedSet

I want to get a string of comma separated values, sorted. if using a TreeSet for that purpose, and then using

Joiner.on(',').join(someSortedSet);

will I get the string values sorted ?

Upvotes: 0

Views: 660

Answers (2)

fge
fge

Reputation: 121780

The answer is yes. The javadoc for this method (well, actually there are 4 overloads in total) shows that it takes an Iterable as an argument; and SortedSet implements Iterable (via Set, via Collection).

As to whether it will be sorted, the fact is that SortedSet iterates over values ordered using their natural ordering, and Iterable is "nothing else" than an iterator.

If values weren't sorted, you could sort them anyway:

Joiner.on(',').join(Ordering.natural().sortedCopy(whatever));

Guava is a gold mine!

[edit: fix link]

Upvotes: 6

Jean Logeart
Jean Logeart

Reputation: 53839

Yes you will.

If someSortedSet iterates on the data respecting the order you want, then join will create the string in the right order.

Upvotes: 0

Related Questions