Try adding
...to your code, and that will help with your question. But remember what the documentation for
groupingBy() says:-
There are no guarantees on the type, mutability, serializability, or thread-safety of the Map or List objects returned.
Nor does it say anything about the order of the keys you are grouping by. You could consider putting the entire Map into a
SortedMap or similar. Please read that link to see how a sorted map orders its entries. You will see the workings of
groupingBy() better if you have several Student objects with the same department name. Consider using an
enum containing the different departments instead of
text.
Please don't call a Map xyzSet. Call it studentsMap or similar.