Sorting of sets elements in Java
Forums:
HashSet<Character> chars = new HashSet<Character>(Arrays.asList('B', 'A', 'C', 'D'));
String str = "";
for (char c : chars) {
str += (str == "" ? "" : "; ") + c;
}
//str is "A; B; C; D"
List<Character> sortedChars = new ArrayList(chars);
Collections.sort(sortedChars);
str = "";
for (char c : sortedChars) {
str += (str == "" ? "" : "; ") + c;
}
//str is "A; B; C; D"
- Log in to post comments