Sorting a list of objects is a typical activity in Java that may be achieved in a variety of ways. When working with sophisticated data structures and algorithms, or when showing data through a user interface, sorting items is frequently required.
Sorting a list of things entails checking the elements in the list against a set of criteria and rearranging them in a certain order. The order might be ascending or descending based on an object’s attribute such as its name, age, price, or any other feature.
Why is sorting a list of objects required
Sorting a list of objects can be useful in any case where it is important to organize data in a certain order or conduct activities that need ordered data. Sorting a list of items is often beneficial when the data contained inside the objects has to be organized in a precise order. Here are several instances where sorting a list of items may be advantageous:
- Display: Sorting a list of items when showing data in a user interface can assist to present the data in an ordered and intuitive manner. A list of customers, for example, might be organized by name or age for better browsing and comprehension.
- Search: While doing searches, sorting a list of items can aid in optimizing search results by displaying the most relevant facts first. For example, a list of job search results may be sorted by relevance or date posted.
- Analysis: When working with complex data structures and algorithms, such as graphs or search trees, sorting a list of items may be essential. Sorting can help these algorithms perform better by making it easier to discover and access the data being processed.
How to sort list of objects in java
There are several approaches to sorting a list of objects in Java:
- Using Comparable Interface
- Using Comparator Interface
- Using Stream API
Approach 1: Using Comparable Interface
The Comparable interface is a built-in Java interface that enables objects to establish their own natural ordering. The Collections.sort() function may be used to sort objects. You must override the compareTo() function to implement Comparable, which compares the current object to another object of the same type.
Sample Code:
import java.util.*;
public class ComparableInterface{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
// Asking the user to provide the size of the array
int size = scn.nextInt();
int[] list = new int[size];
System.out.printf("Enter the elements in the array: ");
// Creating an array
for(int i = 0; i < size;i++){
list[i] = scn.nextInt();
}
// Sorting the list
Arrays.sort(list);
// Displaying
System.out.printf("Initial Array: ");
for(int num: list){
System.out.print(num + " ");
}
}
}
Output:
Enter the size of the array: 5
Enter the elements in the array: 12 13 20 2 7
Sorted Array: 2 7 12 13 20
Explanation for using Comparable Interface:
- Implement the Comparable interface in the class of the sorted objects.
- To set the comparison criteria, override the compareTo() function.
- To sort the objects in ascending order, use the sort() function on the list.
Approach 2: Using Comparator Interface
The Comparator interface is another built-in interface in Java that allows objects to be sorted based on a specific ordering criteria. A Comparator, as opposed to a Comparable, which is declared within the object class, is a distinct class that may be used to sort items of various kinds. You must construct a new class that implements the Comparator interface and overrides the compare() function in order to use Comparator.
class ComparatorInterface{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> numbers = new ArrayList<Integer>();
System.out.println("Enter some numbers (enter 0 to stop): ");
int num = scanner.nextInt();
while (num != 0) {
numbers.add(num);
num = scanner.nextInt();
}
// Sort the list using a comparator
Collections.sort(numbers, new MyComparator());
System.out.println("Sorted numbers: " + numbers);
}
}
class MyComparator implements Comparator<Integer> {
// Sort the list of numbers using a Comparator
public int compare(Integer num1, Integer num2) {
// Compare the integers using the Integer.compareTo method
return num1.compareTo(num2);
}
}
Output:
Enter some numbers (enter 0 to stop):
12 14 14 18 10 6 4 9 2 18 24 31 0
Sorted numbers: [2, 4, 6, 9, 10, 12, 14, 14, 18, 18, 24, 31]
Explanation for using Comparator Interface:
- Make a new class that conforms to the Comparator interface.
- To set the comparison criteria, override the compare() function.
- To sort the objects in ascending order, pass an instance of the comparator class to the list’s sort() function.
Approach 3: Using Stream API
The Stream API is a Java 8 feature that enables more functional and succinct code when working with collections. The Stream API’s sorted() function may be used to sort a list of objects using a Comparator or the natural ordering specified by the Comparable interface.
import java.util.*;
import java.util.stream.Stream;
public class StreamAPI{
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Use the Scanner to read a line of text from the user
System.out.print("Enter some words separated by spaces: ");
String userInput = scanner.nextLine();
// Split the user's input into an array of words
String[] words = userInput.split(" ");
// Use the StreamAPI to create a stream of the words
Stream<String> wordStream = Stream.of(words);
// Use the StreamAPI to count the number of words
long numWords = wordStream.count();
// Output the number of words to the user
System.out.println("Number of words entered: " + numWords);
// Close the Scanner to free up resources
scanner.close();
}
}
Output:
Enter some words separated by spaces: hello world you are learning java codes
Number of words entered: 7
Explanation for using Stream Api:
- To construct a stream of objects, use the list’s stream() function.
- Use the stream’s sorted() function, giving in a lambda expression that provides the comparison criterion.
- Using the collect() function, return the sorted stream to a list.
Best Approach:
While wondering “how to sort list of objects in java” you may find multiple methods but when we speak about the best approach, the Stream API method can be said as one of the best methods to conduct sorting because:
- Readability: The Stream API offers a simple and legible syntax for executing sophisticated operations on collections, such as sorting.
- Conciseness: Code written with the Stream API is frequently more succinct and simpler to comprehend than code written with loops and conditionals.
- Lazy evaluation: The Stream API employs lazy evaluation, which implies that operations are only performed when they are required. Because the API may eliminate needless calculations, this can contribute to improved efficiency, especially when working with big datasets.
- Parallel processing: The Stream API allows parallel processing, which implies that operations can be performed on several threads at the same time. This can result in considerable speed gains on multi-core devices, particularly when dealing with huge collections.
- Method chaining: The Stream API allows you to chain together several actions, resulting in more compact and understandable code. With a single sentence, you may sort a collection, filter the items, and then map them to a new collection.
Sample Problems for Sorting in Java:
Sample Problem 1:
Given a list of Person objects, write a Java program to sort the list in ascending order based on their age, and then print the sorted list.
Solution:
- We first create a list of Person objects using an ArrayList and add some Person objects to it.
- We sort the list based on the age property of each Person object using the Collections.sort() method and a lambda expression that extracts the age property using the Person::getAge method reference.
- We then print the sorted list using a for-each loop, which iterates over each Person object in the list and prints it using the toString() method we defined in the Person class.
// Import necessary packages
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
// Create a public class named Main
public class Main {
public static void main(String[] args) {
// Create a list of Person objects
List<Person> people = new ArrayList<>();
// Add people to the list
people.add(new Person("John", 25));
people.add(new Person("Jane", 21));
people.add(new Person("Bob", 30));
people.add(new Person("Alice", 28));
// Sort the list by age in ascending order
Collections.sort(people, Comparator.comparingInt(Person::getAge));
// Print the sorted list
for (Person person : people) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
// Create a public class named Person
class Person {
// Declare instance variables
private String name;
private int age;
// Create a constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Create getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Output:
Jane - 21
John - 25
Alice - 28
Bob - 30
Sample Problem 2:
You are working in a library and need to arrange a list of books based on their title, author, and publication year. Write a Java program to sort the list in ascending order based on these criteria, and then print the sorted list.
Solution:
- We first create a list of Book objects using an ArrayList and add some Book objects to it.
- We sort the list based on the title, author, and year properties of each Book object using the Collections.sort() method and a lambda expression that uses the Comparator class to compare the properties in the order we want.
- We create a comparator byTitleAuthorYear that first compares the titles using comparing(), then compares the authors using thenComparing(), and finally compares the years using thenComparingInt().
- We then print the sorted list using a for-each loop, which iterates over each Book object in the list and prints it using the toString() method we defined in the Book class.
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
// Main class that sorts the books
public class BookSorter {
// Main function that gets executed when the program runs
public static void main(String[] args) {
// Creating a list of books
List<Book> books = new ArrayList<Book>();
// Adding books to the list
books.add(new Book("Java for Beginners", "John Smith", 2021));
books.add(new Book("Programming in C++", "Mark Johnson", 2019));
books.add(new Book("The Art of War", "Sun Tzu", 2014));
books.add(new Book("Algorithms and Data Structures", "Jane Doe", 2017));
books.add(new Book("Database Management", "Bob Lee", 2020));
// Sorting the list based on title, author, and publication year
Collections.sort(books, new Comparator<Book>() {
@Override
public int compare(Book book1, Book book2) {
// Sorting based on title, then author, then publication year
int titleCompare = book1.getTitle().compareTo(book2.getTitle());
if (titleCompare != 0) {
return titleCompare;
}
int authorCompare = book1.getAuthor().compareTo(book2.getAuthor());
if (authorCompare != 0) {
return authorCompare;
}
return Integer.compare(book1.getPublicationYear(), book2.getPublicationYear());
}
});
// Printing the sorted list
for (Book book : books) {
System.out.println(book);
}
}
// Book class that holds information about a book
class Book {
private String title;
private String author;
private int publicationYear;
// Constructor that creates a book object
public Book(String title, String author, int publicationYear) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
// Getter methods for the title, author, and publication year
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getPublicationYear() {
return publicationYear;
}
// Overriding the toString method to print the book information in a formatted way
public String toString() {
return "Title: " + title + ", Author: " + author + ", Publication Year: " + publicationYear;
}
}
Output:
Title: Algorithms and Data Structures, Author: Jane Doe, Publication Year: 2017
Title: Database Management, Author: Bob Lee, Publication Year: 2020
Title: Java for Beginners, Author: John Smith, Publication Year: 2021
Title: Programming in C++, Author: Mark Johnson, Publication Year: 2019
Title: The Art of War, Author: Sun Tzu, Publication Year: 2014
Sample Problem 3:
You want to sort a list of savings accounts based on their balance in descending order. Write a Java program to sort the list, and then print the account numbers and balances of the accounts in the sorted list.
Solution:
- We first create a list of SavingsAccount objects using an ArrayList and add some SavingsAccount objects to it.
- We sort the list based on the balance property of each SavingsAccount object using the Collections.sort() method and the Collections.reverseOrder() method to sort the objects in descending order.
- Since SavingsAccount implements the Comparable interface, we can define a natural ordering for the objects based on their balance using the compareTo() method.
- We then print the account numbers and balances of the accounts in the sorted list using a for-each loop, which iterates over each SavingsAccount object in the list and prints its account number and balance using the getAccountNumber() and getBalance() methods we defined in the SavingsAccount class.
// Import required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
// Define a class to sort a list of savings accounts
// Define the main method
public static void main(String[] args) {
// Create a list of savings accounts
ArrayList<SavingsAccount> accounts = new ArrayList<>();
accounts.add(new SavingsAccount("SA001", 5000));
accounts.add(new SavingsAccount("SA002", 7000));
accounts.add(new SavingsAccount("SA003", 3000));
accounts.add(new SavingsAccount("SA004", 9000));
accounts.add(new SavingsAccount("SA005", 6000));
// Sort the list of savings accounts based on balance in descending order
Collections.sort(accounts, new Comparator<SavingsAccount>() {
@Override
public int compare(SavingsAccount o1, SavingsAccount o2) {
return Double.compare(o2.getBalance(), o1.getBalance());
}
});
// Print the account numbers and balances of the accounts in the sorted list
System.out.println("Sorted list of savings accounts:");
for (SavingsAccount account : accounts) {
System.out.println("Account Number: " + account.getAccountNumber() +
", Balance: " + account.getBalance());
}
}
// Declare instance variables
private String accountNumber;
private double balance;
// Define a constructor to create a new savings account object
public SavingsAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Define a method to get the account number of the savings account object
public String getAccountNumber() {
return accountNumber;
}
// Define a method to get the balance of the savings account object
public double getBalance() {
return balance;
}
// The program first creates a list of SavingsAccount objects, with each object having an account number and balance.
// The program then sorts the list of SavingsAccount objects in descending order based on the balance field using the Collections.sort method and a custom Comparator object.
// Finally, the program prints out the account numbers and balances of each SavingsAccount object in the sorted list using a for-each loop.
Output:
Sorted list of savings accounts:
Account Number: SA004, Balance: 9000.0
Account Number: SA002, Balance: 7000.0
Account Number: SA005, Balance: 6000.0
Account Number: SA001, Balance: 5000.0
Account Number: SA003, Balance: 3000.0
Conclusion:
Sorting items in a list is a typical programming activity, and there are various techniques in Java for doing so. The Comparable and Comparator interfaces, lambda expressions, and the Stream API are the most often used techniques.
The optimum strategy for sorting objects in a list is determined by the program’s unique needs, the complexity of the sorting logic, and performance concerns. In general, utilizing the Stream API for bigger collections can give superior speed and easier-to-read code, although using the Comparable and Comparator interfaces for basic sorting operations can be more appropriate.