Bifalgorithm post · Computer science one · Handling javanull values
How should a Comparator order elements in a SortedSet?
How should a Comparator order elements in a SortedSet? (Computer science one · Handling javanull values) — "6 //sortedInts has elements 30, 20, 10 in descendin…
Ideas in this flowchart
- SortedSet relies on a Comparator that orders elements consistently.
- A SortedSet is still a Set and does not allow duplicate elements.
- If the Comparator returns zero, the attempted element is not inserted.
- The byName Comparator orders Student objects by first name and last name.
- Two distinct students may both be named John Doe but have different IDs.
- The byName Comparator returns zero for those students because their names match.
- Only one of the students can exist in the SortedSet.
- Comparators must break ties appropriately.
- When names match, the Comparator should compare the students' IDs.
- A TreeSet is created with the byName Comparator.
- Students added to the SortedSet are ordered by name.
- The loop prints each student from the sorted set.
- The sorted integer example contains 30, 20, and 10 in descending order.