top of page

Coffee and Tips Newsletter

Inscreva-se na nossa newsletter semanal

Nos vemos em breve!

Using Comparator.comparing to sort Java Stream

  • Writer: JP
    JP
  • Jun 25, 2023
  • 2 min read

Updated: Feb 5, 2024


Introduction


Sorting data is a common task in many software development projects. When working with collections of objects in Java, a powerful and flexible approach to sorting is to use the Comparator.comparing interface in conjunction with Streams. In this post, we are going to show that using Comparator.comparing to sort Java Stream can make sorting elegant and efficient.


What is the Comparator.comparing interface?


The Comparator.comparing interface is a feature introduced in Java 8 as part of the java.util.Comparator. package. It provides a static method called comparing that allows you to specify a key function (sort key) to compare objects. This function is used to extract a value from an object and compare it against that value during sorting.


Flexibility in sorting with Comparator.comparing


One of the main advantages of the Comparator.comparing interface is its flexibility. With it, we can perform sorting in different fields of an object, allowing the creation of complex sorting logic in a simple and concise way. Notice in the code below that simply in the sorted() method, we pass the Comparator.comparing interface as an argument, which in turn, passes the city field as an argument using method reference (People::getCity) performing the sort by this field.

Output


Monica

John

Mary

Anthony

Seth



Multi-criteria ordering


Often, it is necessary to perform sorting based on multiple criteria. This is easily achieved with the Comparator.comparing. interface by simply chaining together several comparing methods, each specifying a different criterion. Java will carry out the ordering according to the specified sequence. For example, we can sort the same list by city and then by name: Comparator.comparing(People::getCity).thenComparing(People:: getName).

Ascending and descending sort


Another important advantage of the Comparator.comparing interface is the ability to perform sorting in both ascending and descending order. To do this, just chain the method reversed() as in the code below:

Output


Seth

Mary

John

Anthony

Monica


Efficiency and simplicity


By using the Comparator.comparing interface in conjunction with Streams, sorting becomes more efficient and elegant. The combination of these features allows you to write clean code that is easy to read and maintain. Furthermore, Java internally optimizes sorting using efficient algorithms, resulting in satisfactory performance even for large datasets.


Final conclusion


The Comparator.comparing interface is a powerful tool to perform the sorting of Streams in Java. Its flexibility, ascending and descending sorting capabilities, support for multiple criteria, and efficient execution make it a valuable choice for any Java developer. By taking advantage of this interface, we can obtain a more concise, less verbose and efficient code, facilitating the manipulation of objects in a Stream.


Hope you enjoyed!



















16 Comments


melaniemarshall6592
a day ago

Hitclub 957 mình thấy bạn bè nhắc nên tiện tay bấm vào xem thử, kiểu xem giao diện thôi chứ không định đọc sâu. Vào cái là thấy họ làm bố cục khá thoáng, nhìn nhẹ mắt chứ không bị nhồi chữ. Mình để ý mấy tiêu đề kiểu “Cam kết đổi mới và bền vững” đọc lướt là hiểu hướng họ muốn nói, nghe cũng hợp với mảng kiến trúc. Phần giới thiệu về việc kết hợp sáng tạo với chức năng viết vừa đủ, không cố làm màu nên cảm giác dễ tin hơn. Kéo xuống mấy khối nội dung được chia gọn gàng, khoảng trắng ổn nên lướt nhanh vẫn nắm được ý. Nói chung mình thích…

Like

nginhm.an.gm.an.g
6 days ago

https://kjcvip.net/ mình thấy bạn bè nhắc hoài nên ghé thử cho biết, chủ yếu xem trang họ làm ra sao chứ chưa có thời gian ngồi tìm hiểu sâu. Cảm giác đầu tiên là giao diện nhìn khá hiện đại, bố cục chia thành từng mảng rõ nên lướt một vòng cũng không bị ngợp. Mình để ý phần tin tức cập nhật theo ngày đặt khá dễ thấy, kiểu ai thích xem nhanh thông tin mới thì tiện. Mấy tiêu đề và khối nội dung canh thẳng hàng, chữ nghĩa rõ ràng nên mắt đỡ phải “căng” để tìm. Nói chung vào xem thử cho vui mà thấy cách họ chia block và đặt mục tin tức ngay trên…

Like

davidthom.a.s.282.55
Aug 02

https://hitclub-mobile.com/ mình ghé thử đúng kiểu “xem cho biết” vì thấy bạn bè nhắc hoài, chứ cũng không định ngồi lâu. Vào cái là thấy giao diện khá dễ chịu, mấy mục được chia thành từng khối nhìn phát hiểu luôn, lướt nhanh không bị rối mắt. Mình có kéo xuống phần giới thiệu thì thấy họ viết khá kỹ, có nhắc tới giấy phép kiểu PAGCOR với Isle of Man gì đó, đọc lướt thôi nhưng cũng tạo cảm giác họ muốn làm rõ thông tin cho người mới. Nói chung thao tác bấm qua lại ổn, không bị giật lag gì trên máy mình. Mấy tiêu đề và bảng thông tin sắp theo cột gọn gàng nên nhìn…

Like

Nhựt Nguyễn
Nhựt Nguyễn
Jul 23

World clicker is a name I kept seeing pop up in a few idle game reviews lately so I got curious and tried it. The interface is simple, nothing excessive, fits well for light, casual play during downtime. Tested it on my older Android phone and it still ran fine, no freezing or major issues.

Like

uyenghomsoet.h.uy.e.n+abc123
Jul 18

https://gmnc.club/ dạo này thấy mọi người nhắc hoài nên mình bấm vô coi thử cho biết. Không có đọc kỹ nội dung đâu, mình chỉ để ý trải nghiệm lướt trên điện thoại thôi. Vừa vào là thấy giao diện chia khối khá rõ ràng, nhìn cái là biết chỗ nào là phần chính, không bị nhồi chữ nên đỡ rối mắt. Cái mình thích là menu đặt dễ thấy, bấm qua lại mấy mục nhanh gọn, không phải kéo lên kéo xuống tìm hoài. Mấy chỗ hiển thị thông tin cũng trình bày theo dạng cột gọn gàng nên đọc lướt khá nhẹ đầu. Nói chung dùng ổn áp, nhất là cách họ sắp xếp các khối nội dung…

Like
bottom of page