In order to sort elements in a collection in Java, there are several approaches, including using the Collections.sort() method, implementing the Comparable or Comparator interfaces, or using a stream and sorting using a lambda expression.
Using the Collections.sort() method: This method sorts a given list in-place according to their natural ordering or using a specified Comparator implementation. Here’s an example:
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.add("Dave");
Collections.sort(names); // Sort in natural order
System.out.println(names); // Output: [Alice, Bob, Charlie, Dave]
List<Integer> numbers = new ArrayList<>();
numbers.add(42);
numbers.add(17);
numbers.add(99);
numbers.add(8);
Collections.sort(numbers, Comparator.reverseOrder()); // Sort in reverse order
System.out.println(numbers); // Output: [99, 42, 17, 8]
Implementing the Comparable interface: This interface defines a natural ordering for a class, allowing instances of the class to be sorted based on that ordering. Here’s an example:
public class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Person other) {
return this.age - other.age; // Compare by age
}
}
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 20));
people.add(new Person("Charlie", 30));
Collections.sort(people); // Sort by age
for (Person p : people) {
System.out.println(p.getName() + " - " + p.getAge());
}
// Output:
// Bob - 20
// Alice - 25
// Charlie - 30
Implementing the Comparator interface: This interface allows for custom comparison of objects, and is useful when sorting classes that do not have a natural ordering or when we want to use a different ordering than the natural one. Here’s an example:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 20));
people.add(new Person("Charlie", 30));
Collections.sort(people, Comparator.comparingInt(Person::getAge)); // Sort by age
for (Person p : people) {
System.out.println(p.getName() + " - " + p.getAge());
}
// Output:
// Bob - 20
// Alice - 25
// Charlie - 30
Using a stream and sorting using a lambda expression: This approach allows us to use the sorted() method of a stream to sort elements according to a given lambda expression. Here’s an example:
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.add("Dave");
List<String> sortedNames = names.stream().sorted().collect(Collectors.toList());
System.out.println(sortedNames); // Output