排序效率小PK
上个小demo看看
1 import com.google.common.primitives.Chars; 2 3 import java.util.*; 4 5 /** 6 * java中数组转List使用Arrays.asList(T..a)方法,但是该方法不能将基本类型转为List,可以使用Guava工具类 7 * 使用Collection.sort排序,没有TreeSet进行排序效率高 8 */ 9 public class TestCollection { 10 11 public static void main(String[] args) { 12 List list = new ArrayList<>(); 13 String[] strs = {"1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; 14 String ss ="AEBE77B6088AD225CC7C53FCA90A2C8D12F22F881B7EFF5E11E7C1DD531D92EE78A310C4E12A2C9AF45CDC9C8EAB893E5458B275D81474B77188A5762E62E18D34179096E20806B36003EBA37D7F8FE042049496339280E9825A6D623FA2D469A576DA6F6B8F7F7EBC79B669826AEA65CA6C4F7948A449002958307868334568AEEBD7708D66B1AD62576946E8D64A632825D397DE8E52C11E17E9EDC07C1BF9CBDBA30E7468DC67A704EA83F484AE8209EF0B9634C92484C713461C6D563CAE59E68D6E32DF3FF5FA68580EE95B96FB6A9AA422AB792365DE5B920A7FED623F562EB7544F81553D8B6288093308407386577032AD26F5A32A3FF422CB797FDB33ED5AA1F04ED837387481247993F781DC02939A04E1DAE81389526707825B91E0CAFA53D906143C608CABA197AD5CDCCABCCF3CA8902964EA8485582F4EA65CC6746FAB8527427ED370D95BC902106CA900F5E900AAB35761AEF4B3E709FADBB766F42BA100E6220157BAC8EEF1A19DB798E209446535890FE5811DBFC3F9BAC39CE0E0A1AF9DD671275D1E559E3A765A449DCE886C7A33AF045100CB71AAA5C39EBA6CA44307E0583AFECCAC0100FB6E9600C1D38E85C565664DD7B6865CAED1E66A583EB7F5BE43D10F6B50FE57EB82C81A14CF1133062629A1B62D27B024C34E3749ACF9AB85D245F9A433FF16715982FCA8831E4997F94B1CA1AF01334954A3EAC7D5A9908FA4F6E051CB4A7C0B0DEE169845F43553BE31E13F08C555B926D444832E3C74958356266CB6A0F9049C991A651AC50A86AD74303272747DFCFBDDF1F009F75769A63C5432635FCB91BDE7476AB4EA063E3A41F9FF9CE721A2C63337ABBD0D4282D8BAFEE6FA8675B4FD4E5623076A1BD937112EC0B9CA58883FCDCBB08C383545BCACF20421F32E99A4FDB751AE4C7322F8FDBAB26308FB5385ACA1468E2E10A4EC52D0808DA90B86066BACAE71A688A3A0BE9FFE32C7F35F5BE608BC18473308DED21D2D74EE7C2EA9144AA59C3E2F9456EAB58D215278A6857FEFCA7B6D4C80A0B8EA466A36D2227001DB9B7E8BAD569C31055FDE0B177C4C77AE810AC73F5C0E6BC22069C0679CD6DCED05F742EE284E3E2D09B791AACCB237285B31937B6CDF6992882C359902D83F262BEFE3167C1E154055EFF0B5F0BBE92BF705501CE0AB9D8957D50BD57FE49E3E4A47069CC9516DCD165C8F5712645A6F5CBA15F5E2E439F4147069BE0B3E5E0720BEEB271032A2AA5CFC2A2BB612D22CC38DB0242062E8CB5DF4E6D10E54A3EAC7D5A9908FA4F6E051CB4A7C0B3FD1ED31C5053199EA83B4B2E585E03090C967F5BE3397194EA90572CBFE1A23298271482473E9BCD0A41598EB7F4FE81E2F168EAB08C623782CB239D3A773049D8CAF370A477D7B02274824D18DBF42671BAA0401BF486213D94697099FE26F5C7E67483015273F6980C1341601A6CF7613A0F8DB61D46C34BB5F04FD0F9EF6A71A3AE2F545E4973DC5E38A3F679BA6BDA40E38204E2BA355EEAD7AE04C51DE20E59DF87888677DF3A12E97436ADAF567CAFDC73176A51BB2BFA68B6C303C09DF3FCCC9A82CFE0A6D9B68E9EBB5C691B86B5F67B2E04C2669848DBB2F4BCF06B798E209446535890FE5811DBFC3F9BA9024F9C0368162E23C33FF68C7316934"; 15 char[] cs = ss.toCharArray(); 16 list = Chars.asList(cs); 17 List list1 = Chars.asList(cs); 18 System.out.println(list); 19 20 long start = System.currentTimeMillis(); 21 System.out.println(collectionSort(list)); 22 long mid = System.currentTimeMillis(); 23 System.out.println(mid-start);//19 24 Set set = treeSetSort(list1); 25 System.out.println(list1); 26 System.out.println(System.currentTimeMillis()-mid);//2 27 } 28 29 private static List collectionSort(List list){ 30 Collections.sort(list, new Comparator() { 31 32 @Override 33 public int compare(Object a, Object b) { 34 return a.toString().compareTo(b.toString()); 35 } 36 }); 37 return list; 38 } 39 40 41 private static Set treeSetSort(List list){ 42 return new TreeSet(list); 43 } 44 45 }