Java Collections

Sorting in collection framework

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">We can sort the elements of&colon;<&sol;p>&NewLine;<ol>&NewLine;<li><b>String objects<&sol;b><&sol;li>&NewLine;<li><b>Wrapper class objects<&sol;b><&sol;li>&NewLine;<li><b>User-defined class objects<&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<p><b>Collections <&sol;b>class provides static methods for sorting the elements of collection&period;If collection elements are of <i><b>Set <&sol;b><&sol;i>type&comma; we can use <i><b>TreeSet<&sol;b><&sol;i>&period; But We cannot sort the elements of <i><b>List<&sol;b><&sol;i>&period; <i><b>Collections <&sol;b><&sol;i>class provides methods for sorting the elements of List type elements&period;<br &sol;>&NewLine;<b>Method of <i>Collections <&sol;i>class for sorting <i>List <&sol;i>elements<&sol;b><br &sol;>&NewLine;<i><b>public void sort&lpar;List list&rpar;&colon; <&sol;b><&sol;i>is used to sort the elements of <i><b>List<&sol;b><&sol;i>&period; <i><b>List<&sol;b><&sol;i> elements must be of <i><b>Comparable <&sol;b><&sol;i>type&period;<&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>Note&colon;<&sol;b> <i><b>String <&sol;b><&sol;i>class and <i><b>Wrapper <&sol;b><&sol;i>classes implements the <i><b>Comparable <&sol;b><&sol;i>interface&period; So if you store the objects of string or wrapper classes&comma; it will be <i><b>Comparable<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<p><b>Example of Sorting the elements of <i>List <&sol;i>that contains string objects-<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;util&period;&ast;&semi; &NewLine;class Simple12&lbrace; &NewLine;public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; &NewLine;ArrayList al&equals;new ArrayList&lpar;&rpar;&semi; &NewLine;al&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine;al&period;add&lpar;"Sweetu"&rpar;&semi; &NewLine;al&period;add&lpar;"Adesh"&rpar;&semi; &NewLine;al&period;add&lpar;"Vinesh"&rpar;&semi; &NewLine; &NewLine;Collections&period;sort&lpar;al&rpar;&semi; &NewLine;Iterator itr&equals;al&period;iterator&lpar;&rpar;&semi; &NewLine; &NewLine;while&lpar;itr&period;hasNext&lpar;&rpar;&rpar;&lbrace; &NewLine;System&period;out&period;println&lpar;itr&period;next&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b>Output&colon;<&sol;b><br &sol;>&NewLine;Adesh<br &sol;>&NewLine;Dinesh<br &sol;>&NewLine;Sweetu<br &sol;>&NewLine;Vinesh<&sol;div>&NewLine;<p>&NewLine;<b>Example of Sorting the elements of <i>List <&sol;i>that contains <i>Wrapper<&sol;i> class objects<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;util&period;&ast;&semi; &NewLine;class Simple12&lbrace; &NewLine;public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; &NewLine;ArrayList al&equals;new ArrayList&lpar;&rpar;&semi; &NewLine;al&period;add&lpar;Integer&period;valueOf&lpar;201&rpar;&rpar;&semi; &NewLine;al&period;add&lpar;Integer&period;valueOf&lpar;101&rpar;&rpar;&semi; &NewLine;al&period;add&lpar;230&rpar;&semi;&sol;&sol;internally will be converted into objects as Integer&period;valueOf&lpar;230&rpar; &NewLine; &NewLine;Collections&period;sort&lpar;al&rpar;&semi; &NewLine; &NewLine;Iterator itr&equals;al&period;iterator&lpar;&rpar;&semi; &NewLine;while&lpar;itr&period;hasNext&lpar;&rpar;&rpar;&lbrace; &NewLine;System&period;out&period;println&lpar;itr&period;next&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b>Output&colon;<&sol;b><br &sol;>&NewLine;101<br &sol;>&NewLine;201<br &sol;>&NewLine;230<&sol;div>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<b>Java Collections Tutorial<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;arraylist-class-in-java-collection&sol;">ArrayList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedlist-class-in-collection&sol;">LinkedList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;listiterator-interface-in-collection&sol;">ListIterator interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;">HashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashset-class-in-collection&sol;">LinkedHashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treeset-classin-collection&sol;">TreeSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashset-and-treeset&sol;">Difference between TreeSet &amp&semi; HashSet<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;map-interface-in-collection&sol;">Map interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashmap-class-in-collection-framework&sol;">HashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-does-java-hashmap-work-internally&sol;">How does work HashMap&quest;<&sol;a> <&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-treemap-vs-hashmap&sol;">Difference between TreeMap vs HashMap<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashmap-class-in-collection&sol;">LinkedHashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treemap-class-in-collection-framework&sol;">TreeMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashtable-class-in-collection-framework&sol;">Hashtable class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashmap-and&sol;">Difference between HashMap and HashTable in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;sorting-in-collection-framework&sol;">Sorting<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-comparable-and-comparator&sol;">Comparable interface<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashmap-and&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;sorting-in-collection-framework&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashmap-and&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-collections-interview-questions&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '407'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago