Java の Collections.sort() と例
java.util.Collections.sort() メソッドは java.util.Collections クラスに存在します。指定されたファイル内に存在する要素をソートするために使用されます。 リスト コレクションの昇順。それは次のように機能します java.util.Arrays.sort() このメソッドは、配列の要素だけでなく、リンクされたリスト、キュー、その他多くの要素を並べ替えることができるため、メソッドよりも優れています。
public static void sort(List myList) myList : A List type object we want to sort. This method doesn't return anything
例:
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'} ArrayList を昇順に並べ替える
ジャワ
// 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);> > }> }> |
出力
List after the use of Collection.sort() : [Dear, Friends, Geeks For Geeks, Is, Superb]
時間計算量 : Collections.sort() の時間計算量が O(nlog(n)) であるため、O(N log N)。
補助スペース :O(1)
ArrayList を降順に並べ替える
ジャワ
// 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);> > }> }> |
出力
List after the use of Collection.sort() : [Superb, Is, Geeks For Geeks, Friends, Dear]
時間計算量: Collections.sort() の時間計算量として O(N log N) は O(nlog(n)) です。
補助スペース: ○(1)
ユーザー定義の基準に従って ArrayList を並べ替えます。 使用できます コンパレータインターフェース この目的のために。
ジャワ
// 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)); } }> |
出力
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 121 cccc jaipur 131 aaaa nyc
配列.sort() vs Collections.sort() Arrays.sort は、プリミティブ データ型の配列でも機能します。 コレクション .sort() はオブジェクトのコレクションに対して機能します。 配列リスト 、 リンクリスト 指定された配列項目の ArrayList を作成した後、Collections.sort() を使用して配列を並べ替えることができます。
ジャワ
// 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);> > }> }> |
出力
List after the use of Collection.sort() : [Code, Geeks, Practice, Quiz]
Arrays.sort() と Collections.sort() の時間計算量:
Arrays.sort() はデュアル ピボット クイックソート アルゴリズムを使用しており、時間計算量は O(N.log N) で、通常は従来のクイックソート アルゴリズムよりも高速です。一方、Collections.sort() はリスト要素の配列を作成し、適応型マージソート アルゴリズムを使用してそれらを並べ替え、リストを反復処理して各要素を正しい位置に配置します。したがって、int、char、double などのプリミティブ データ型の場合、Arrays.sort() は Collections.sort() よりも時間効率がはるかに高いことがわかります。プリミティブ データ型に関連する問題は、最適化を高めるために Arrays.sort() を使用して解決する必要があります。
以下は違いを示すコードです。
ジャワ
/*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> |
出力
Time taken by Arrays.sort(): 29 Time taken by Collections.sort(): 42
この記事は、尊敬されるオタクにとって役立つことを願っています。 。