この記事では、Javaが実装したアレイ控除および並べ替え操作について説明します。次のように、参照のために共有してください。
Javaが配列の重複排除と並べ替え操作を実装するJavaのデモンストレーションです
記事に記載されているソースコードの例は、JDK1.6+、junit4.8.2に基づいています
java.util.Arrays.sort()
ソートソートint[] 、 long[] 、 short[] 、 char[] 、 byte[] 、 float[] 、 double[] 、 Object[]をサポート
参照サンプルコードスニペットは次のとおりです
// int arrayを宣言し、int [] intarry = {5,4,7,8,2,0,1,9,3,6,10}; // int array arrays.sort(intarry)をソートする;Junitテストクラスのソースコード:
パッケージcom.gjnote.test.array; Import java.util.array; Import org.junit.test; public class testarrayssort {// int arrayを宣言し、int [] intarryを初期化し、{5,4,7,8,2,0,1,9,3,6,6,10}; arrays.sort(intarry); for(int i = 0; i <intarry.length; i ++){system.out.println(intarry [i]);} system.out.println(arrays.tostring(intarring));}}}}コンソール出力
0
1
2
3
4
5
6
7
8
9
10
[0、1、2、3、4、5、6、7、8、9、10]
java.util.Collections.sort()
オブジェクトの比較は、内部compare方法を実装することによって実現されます
サンプルコードスニペットは次のとおりです
/*** collections.sort(list、comparator(){});*リスト配列をソートするための推奨方法*/public void collectionssortelement1(list list){collections.sort(list、new Comparator(){@overridepublic int Compare(string o1、string o2){//実際の並べ替えの順序付けの順序を調整します。 (o2).compareto(o1);}});}Javaはリストの重複排除を実装しています
方法1:ループトラバーサルに使用して、リスト内の重複要素を削除する
コードスニペットは次のとおりです
List Templist = new ArrayList(); //(String String:OriginAllist){if(!Templist.Contains(String)){Templist.Add(String);}}の元のリストから重複要素を削除します方法2:設定を使用して重複排除します
コードスニペットは次のとおりです
// set set element and demuplicate set set = new hashset(originallist); list templist = new ArrayList(set);
方法3:ツリーセットを使用して、重複要素を削除します
treeset troeset = new Treeset(OriginAllist); listTemplist = new ArrayList(); Templist.Addall(Treeset); // Treesetデフォルトのソートは昇順です。実際の状況に応じて逆順序が必要かどうかを追加します。 Collections.Reverse(Templist);
Javaは、ソート後にリストの控除を実装します
Junitテストリストの重複排除とソースコードのソート
パッケージcom.gjnote.test.array; Import java.util.arraylist; Import java.util.collections; Import java.util.comparator; Import java.util.hashset; Import java.util.list; Import Java.util.set; Import Java.util.util.junit. org.junit.test;/*** test class*list array de-heavy element sort ** @version 1.0*@author www.gjnote.com **/public class testlistarraysort {private listoriginallist = null; {originalallist.add( "element" + i); // repement elementif(i%2 == 0){olignallist.add( "element" + i);}}/*** output list element*/private void outputlist(list list){for(int i = 0; i <list.isize(); i + +) {system.out.println(list.get(i));}}/*** collections.sort(list、comparator(){});*推奨並べ替え方法*/private void collectionssortelement(list list){long start = system.currenttimemillis(); collections.sort(list、new comparitator(){@string publis int()string int() {//実際の並べ替えに従って比較オブジェクトの順序を調整する必要があるO2.comPareto(O1);}}); // outputList(templist); system.out.println( "collections.sort:" +(system.currenttimemillis() - start); "ms"); sort*/@@testpublic void testforloopremovereepeatelement(){system.out.println( "testforloopremoverepeatelement"); long start = system.currenttimemillis(); list templist = new arraylist(); //(string string:olignallist){if(!templist.contains(string)){templist.add(}} // sort collectssortelement(templist); // outpotlist(outputlist); loop to raverse listと削除要素を削除します。 * collections.sort(list、comparator(){})を使用します。 sort **/@testpublic void testRemoverePeatelement(){System.out.println( "testSetRemoverePeatelement"); long start = system.currenttimemillis(); // first(理論的価値:最初に並べ替え、次に重複排除は後で並べ替えよりも効率的になります)collectsSortelement(originallist); // setは、設定要素の独自性を使用します。並べ替えで実行時間を試してください//collectionssortelement(templist);//Outputlist(templist(templist(templint(templint.println("collections.sortソート、設定を使用します。 sort*/@testpublic void testtreeseTremoverepeatelement(){system.out.println( "testtreesetremoverepeatelement"); long start = system.currenttimemillis(); treesettreeSet = new Treeset(OriginAllist); listTemplist = new ArrayList(); Templist.Addall(Treeset); // Default Sortは昇順です。実際の状況に応じて並べ替える必要があるかどうかを追加します。 collections.reverse(templist); // outputlist(templist); system.out.println( "ツリーセットで並べ替え、重複要素を削除する:" +(system.currenttimemillis() - start) + "ms");}@testpublic void testmethods(){// outputlist(originalist); testsetRemoverePeatelement(); // 14MstestTreeTreeTremoverePeatelement(); // 20ms // testforloopremoverepeatelement(); // 2525ms}}}}TestSetRemoverEepEatelement()コンソールを実行して、結果を出力します
testsetRemovereepeatelement
Collections.Sort:8ms
collections.sortソート、設定を使用してdeduplicate:14msに使用します
TestTreeTreeTremoverePeatelement()コンソールを実行して、結果を出力します
testTreeStremovereepeatelement
ツリーセットで並べ替え、重複要素を削除します:20ms
TestForLoopreMoverePeatelement()コンソールを実行して、結果を出力します
test forloopremovereepeatelement
Collections.Sort:7ms
for loopを使用してリストするには、重複要素を削除します:2525ms
PS:参照のためのいくつかの関連ツールを次に示します。
オンライン削除ツール:
http://tools.vevb.com/code/quchong
オンラインテキストの繰り返しツール:
http://tools.vevb.com/aideddesign/txt_quchong
オンラインアニメーションデモンストレーション挿入/選択/バブル/マージ/ヒル/クイックソートアルゴリズムプロセスツール:
http://tools.vevb.com/aideddesign/paixu_ys
Javaアルゴリズムの詳細については、このサイトに興味のある読者は、「Javaデータ構造とアルゴリズムのチュートリアル」、「Java操作DOMノードのヒントの要約」、「Javaファイルの要約およびディレクトリ操作のヒント」、「Java Cache操作のヒントの要約」というトピックを見ることができます。
この記事がみんなのJavaプログラミングに役立つことを願っています。