美文网首页
java排序简单实用

java排序简单实用

作者: 微笑_3917 | 来源:发表于2024-08-03 12:00 被阅读0次

    排序函数

    Arrays.sort

    Collections.sort

    排序集合

    TreeSet   TreeMap   

    示例

    public class Test1_1 {

            private static Randomrandom =new Random();

            public static void main(String[] args) {

                    int size =10000;

                    Integer[] a =initArray(size);

                    Arrays.sort(a);

                    Collections.sort(Arrays.asList(a));

                    long starttime =System.currentTimeMillis();

                    TreeSet set =new TreeSet();

                    set.addAll(Arrays.asList(a));

                    a = set.toArray(new Integer[0]);

                    System.out.println("耗时:"+(System.currentTimeMillis()-starttime)+"ms");

    printArray(a);

            }

    private static Integer[] initArray(int size) {

                Integer[] a =new Integer[size];

                for (int i =0; i < size; i++) {

                        a[i] =random.nextInt(1000000000);

                }

                return a;

    }

        private static void printArray(Integer[] a) {

                    for (Integer temp : a) {

                            System.out.println(temp);

                    }

    }

    相关文章

      网友评论

          本文标题:java排序简单实用

          本文链接:https://www.haomeiwen.com/subject/zgezhjtx.html