RUVIDEO
Поделитесь видео 🙏

Sorting collections in java | Comparable & Comparator in java | Java9s.com

https://java9s.com
Example code : https://github.com/java9s/tutorials/tree/master/java/JavaCollections
To sort the elements, we should be able to compare the element with another element and only then we can judge which one should go first and which in the next position.

Java collections framework has an interface called Comparable which has a method called compareTo method.

CompareTo method is where the criteria is declared and this is called natural ordering for that kind of objects

So, How does compareTo method decides which object is less than, equal or greater than an object?

compareTo method accepts an object of same instance type and based on certain criteria defined, it returns an integer which indicates if an object is less or greater or equal in ordering.
This Collections class defines two methods called Sort and Sort a list with a comparator.

These two methods accept a List as a collection and orders all the elements based on certain criteria.

Now, if you observe the signature of the sort method, you can see that it is static and it only accepts the collection with elements which has implemented the comparable interface.


This is because, without implementing Comparable, the will be no compareTo which defines the natural ordering of elements.

You can see that this documentation clearly states that it uses natural ordering which means that it relies on compareTo method.
Collections framework has an interface called Comparator which defines compare method.

It accepts two objects of a type and compares both on certain criteria and then returns an integer.

Similar to compareTo method, this method also returns a -ve integer or +ve integer or zero.

If a negative integer is returned, first argument is less or comes after the second object.

If a positive integer is returned, first argument is comes first and second one always after the first one.

If zero, then both stand at the same level in ordering.


Complete Java Beginners Tutorials:
https://www.youtube.com/playlist?list=PLkiVxfkxAYd1Typb8x-2nbzgsxdMQesKg

Multithreading in Java tutorial:
https://www.youtube.com/playlist?list=PLkiVxfkxAYd1i9eX9X74eoTjKZXzvITMX

Spring 3 MVC Framework tutorials:
https://www.youtube.com/playlist?list=PLA3AD4211EBE70E86

Spring Framework Tutorials:
https://www.youtube.com/playlist?list=PL2882729612B70122

Design Patterns in Java:
https://www.youtube.com/playlist?list=PLkiVxfkxAYd1xWYj9aOWoTevqok0VoYe7

https://www.youtube.com/playlist?list=PLkiVxfkxAYd2TAn--1BH1ERvYbHxh11Qs
Complete Generics Tutorials playlist

Collections in Java
https://www.youtube.com/playlist?list=PLkiVxfkxAYd1aPKf45Byl24NqSNgHGbOf

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Sorting collections in java | Comparable & Comparator in java | Java9s.com», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.