java.util.Collections.sort() metoda je prisotna v razredu java.util.Collections. Uporablja se za razvrščanje elementov, ki so prisotni v podanem seznam zbirke v naraščajočem vrstnem redu. Deluje podobno kot java.util.Arrays.sort() metoda, vendar je boljša od tega, ker lahko razvrsti elemente matrike, pa tudi povezanega seznama, čakalne vrste in še veliko več, ki so v njej prisotni.
public static void sort(List myList) myList : A List type object we want to sort. This method doesn't return anything>
primer:
Let us suppose that our list contains {'Geeks For Geeks', 'Friends', 'Dear', 'Is', 'Superb'} After using Collection.sort(), we obtain a sorted list as {'Dear', 'Friends', 'Geeks For Geeks', 'Is', 'Superb'}>
Razvrščanje seznama ArrayList v naraščajočem vrstnem redu
JAVA
// Java program to demonstrate working of Collections.sort()> import> java.util.*;> public> class> Collectionsorting> {> > public> static> void> main(String[] args)> > {> > // Create a list of strings> > ArrayList al => new> ArrayList();> > al.add(> 'Geeks For Geeks'> );> > al.add(> 'Friends'> );> > al.add(> 'Dear'> );> > al.add(> 'Is'> );> > al.add(> 'Superb'> );> > /* Collections.sort method is sorting the> > elements of ArrayList in ascending order. */> > Collections.sort(al);> > // Let us print the sorted list> > System.out.println(> 'List after the use of'> +> > ' Collection.sort() :
'> + al);> > }> }> |
če drugače če java
>
>Izhod
List after the use of Collection.sort() : [Dear, Friends, Geeks For Geeks, Is, Superb]>
Časovna zapletenost : O(N log N) kot časovna kompleksnost Collections.sort() je O(nlog(n)).
Pomožni prostor : O(1)
Razvrščanje seznama ArrayList v padajočem vrstnem redu
JAVA
// Java program to demonstrate working of Collections.sort()> // to descending order.> import> java.util.*;> public> class> Collectionsorting> {> > public> static> void> main(String[] args)> > {> > // Create a list of strings> > ArrayList al => new> ArrayList();> > al.add(> 'Geeks For Geeks'> );> > al.add(> 'Friends'> );> > al.add(> 'Dear'> );> > al.add(> 'Is'> );> > al.add(> 'Superb'> );> > /* Collections.sort method is sorting the> > elements of ArrayList in ascending order. */> > Collections.sort(al, Collections.reverseOrder());> > // Let us print the sorted list> > System.out.println(> 'List after the use of'> +> > ' Collection.sort() :
'> + al);> > }> }> |
>
>Izhod
List after the use of Collection.sort() : [Superb, Is, Geeks For Geeks, Friends, Dear]>
Časovna zapletenost: O(N log N) kot časovna kompleksnost Collections.sort() je O(nlog(n)).
Pomožni prostor: O(1)
Razvrščanje seznama ArrayList glede na uporabniško določene kriterije. Lahko uporabimo Primerjalni vmesnik Za ta namen.
sestava odnosa
Java
// Java program to demonstrate working of Comparator> // interface and Collections.sort() to sort according> // to user defined criteria.> import> java.util.*;> import> java.lang.*;> import> java.io.*;> // A class to represent a student.> class> Student> {> > int> rollno;> > String name, address;> > // Constructor> > public> Student(> int> rollno, String name,> > String address)> > {> > this> .rollno = rollno;> > this> .name = name;> > this> .address = address;> > }> > // Used to print student details in main()> > public> String toString()> > {> > return> this> .rollno +> ' '> +> this> .name +> > ' '> +> this> .address;> > }> }> class> Sortbyroll> implements> Comparator> {> > // Used for sorting in ascending order of> > // roll number> > public> int> compare(Student a, Student b)> > {> > return> a.rollno - b.rollno;> > }> }> // Driver class> class> Main> {> > public> static> void> main (String[] args)> > {> > ArrayList ar => new> ArrayList();> > ar.add(> new> Student(> 111> ,> 'bbbb'> ,> 'london'> ));> > ar.add(> new> Student(> 131> ,> 'aaaa'> ,> 'nyc'> ));> > ar.add(> new> Student(> 121> ,> 'cccc'> ,> 'jaipur'> ));> > System.out.println(> 'Unsorted'> );> > for> (> int> i=> 0> ; i System.out.println(ar.get(i)); Collections.sort(ar, new Sortbyroll()); System.out.println('
Sorted by rollno'); for (int i=0; i System.out.println(ar.get(i)); } }> |
>
>
moj živi kriketIzhod
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 121 cccc jaipur 131 aaaa nyc>
Arrays.sort() proti zbirkam.sort() Arrays.sort deluje za nize, ki so lahko tudi primitivnega podatkovnega tipa. Zbirke .sort() deluje za predmete, kot so zbirke ArrayList , LinkedList , itd. Uporabimo lahko Collections.sort() za razvrščanje matrike po ustvarjanju ArrayList danih elementov matrike.
JAVA
// Using Collections.sort() to sort an array> import> java.util.*;> public> class> Collectionsort> {> > public> static> void> main(String[] args)> > {> > // create an array of string objs> > String domains[] = {> 'Practice'> ,> 'Geeks'> ,> > 'Code'> ,> 'Quiz'> };> > // Here we are making a list named as Collist> > List colList => > new> ArrayList(Arrays.asList(domains));> > // Collection.sort() method is used here> > // to sort the list elements.> > Collections.sort(colList);> > // Let us print the sorted list> > System.out.println(> 'List after the use of'> +> > ' Collection.sort() :
'> +> > colList);> > }> }> |
>
>Izhod
List after the use of Collection.sort() : [Code, Geeks, Practice, Quiz]>
Časovna kompleksnost Arrays.sort() proti Collections.sort():
Arrays.sort() uporablja algoritem Dual-Pivot QuickSort, ki daje časovno zapletenost O(N.log N), ki je običajno hitrejša od tradicionalnih algoritmov Quicksort. Po drugi strani Collections.sort() ustvari matriko elementov seznama, jih razvrsti z uporabo prilagodljivega algoritma Mergesort in ponovi po seznamu, da postavi vsak element na pravilno lokacijo. Tako se za primitivne podatkovne tipe, kot so int, char, double itd., Arrays.sort() izkaže za veliko bolj časovno učinkovitega kot Collections.sort(). Težave, ki vključujejo primitivne podatkovne tipe, je treba poskušati rešiti z uporabo Arrays.sort() za boljšo optimizacijo.
dateformat.format
Spodaj je koda za prikaz razlike:
Java
/*package whatever //do not write package name here */> import> java.io.*;> import> java.util.*;> class> GFG {> > public> static> void> main (String[] args) {> > int> len => 5000000> ;> > > // creating a large test array> > int> [] arr => new> int> [len];> > for> (> int> i = len; i>> 0> ; i--)> > arr[len - i] = i;> > > // creating a large test arraylist> > ArrayList list => new> ArrayList();> > for> (> int> i = len; i>> 0> ; i--)> > list.add(i);> > > // calculating time used by arrays.sort()> > long> startA = System.currentTimeMillis();> > Arrays.sort(arr);> > long> stopA = System.currentTimeMillis();> > > // calculating time used by collections.sort()> > long> startAL = System.currentTimeMillis();> > Collections.sort(list);> > long> stopAL = System.currentTimeMillis();> > > System.out.println(> 'Time taken by Arrays.sort(): '> + (stopA - startA));> > System.out.println(> 'Time taken by Collections.sort(): '> + (stopAL - startAL));> > }> }> // This code is contributed by godcoder28> |
>
>Izhod
Time taken by Arrays.sort(): 29 Time taken by Collections.sort(): 42>
Članek naj bi bil koristen za cenjene Geeke. .