Guava ImmutableList.sortedCopyOf() method in Java
In this post, we will explore how to use the Guava ImmutableList.sortedCopyOf() method to create an immutable list that contains the elements of another collection or iterable in a sorted order.
Working with immutable collections and sorted collections is a common and useful task in Java. You may need to create an immutable list that contains the elements of another collection or iterable in a sorted order, such as a list of names, numbers, or custom objects. For example, you may want to sort a list of students by their grades, or a list of products by their prices. You can easily achieve all this using Guava ImmutableList.sortedCopyOf() method.
The Guava ImmutableList.sortedCopyOf() method is used to create an immutable list that contains the elements of another collection or iterable in a sorted order. The syntax of this method is:
|
1 2 3 4 5 |
public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf(Iterable<? extends E> elements) public static <E> ImmutableList<E> sortedCopyOf(Comparator<? super E> comparator, Iterable<? extends E> elements) |
The first version uses the natural ordering of the elements to sort them. The elements must implement the Comparable interface and be mutually comparable. The second version uses a custom comparator to sort the elements. The comparator must be consistent with equals. It returns an immutable list that contains the elements of the given collection or iterable in a sorted order. The returned list maintains the sort order as long as all elements remain mutually comparable. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.util.Comparator; import java.util.List; class Student { private String name; private int grade; public Student(String name, int grade) { this.name = name; this.grade = grade; } public String getName() { return name; } public int getGrade() { return grade; } // getters and setters omitted for brevity @Override public String toString() { return name + "(" + grade + ")"; } } class Main { public static void main(String[] args) { List<Integer> numbers = Lists.newArrayList(5, 3, 1, 4, 2); ImmutableList<Integer> sortedNumbers = ImmutableList.sortedCopyOf(numbers); System.out.println(sortedNumbers); // [1, 2, 3, 4, 5] List<String> names = Lists.newArrayList("John", "David", "Adam", "Tom"); ImmutableList<String> sortedNames = ImmutableList.sortedCopyOf(names); System.out.println(sortedNames); // [Adam, David, John, Tom] List<Student> students = Lists.newArrayList( new Student("John", 80), new Student("David", 90), new Student("Adam", 85), new Student("Tom", 75) ); // Sort by name ImmutableList<Student> sortedByName = ImmutableList.sortedCopyOf( Comparator.comparing(Student::getName), students); System.out.println(sortedByName); // [Adam (85), David (90), John (80), Tom (75)] // Sort by grade ImmutableList<Student> sortedByGrade = ImmutableList.sortedCopyOf( Comparator.comparingInt(Student::getGrade), students); System.out.println(sortedByGrade); // [Tom (75), John (80), Adam (85), David (90)] } } |
The Guava ImmutableList.sortedCopyOf() method can be used to simplify and improve the readability of your code when creating an immutable list that contains the elements of another collection or iterable in a sorted order. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.collect.ImmutableList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(5, 3, 1, 4, 2); // Without Guava List<Integer> sortedNumbers = new ArrayList<>(numbers); Collections.sort(sortedNumbers); List<Integer> unmodifiableSortedNumbers = Collections.unmodifiableList(sortedNumbers); System.out.println(unmodifiableSortedNumbers); // [1, 2, 3, 4, 5] // With Guava ImmutableList<Integer> sortedImmutableNumbers = ImmutableList.sortedCopyOf(numbers); System.out.println(sortedImmutableNumbers); // [1, 2, 3, 4, 5] } } |
The Guava ImmutableList.sortedCopyOf() method can also be used in conjunction with other Guava utilities, such as Iterables, Predicates, Functions, etc. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Filter and sort List<Integer> numbers = Arrays.asList(5, 3, 1, 4, 2); ImmutableList<Integer> evenSortedNumbers = ImmutableList.sortedCopyOf( Iterables.filter(numbers, x -> x % 2 == 0)); System.out.println(evenSortedNumbers); // [2, 4] // Transform and sort List<String> names = Arrays.asList("John", "David", "Adam", "Tom"); ImmutableList<String> upperSortedNames = ImmutableList.sortedCopyOf( Iterables.transform(names, String::toUpperCase)); System.out.println(upperSortedNames); // [ADAM, JANE, JOHN, TOM] } } |
That’s all about Guava ImmutableList.sortedCopyOf() method in Java. For more information about this method, you can check out the Guava official documentation or its GitHub repository.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)