zoukankan      html  css  js  c++  java
  • java中List 排序

    java中List 排序

    代码编写

    package com.xiang;
    
    import java.util.*;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/11/12 22:57
     */
    
    public class ListSequence {
        /**
         * list 排序
         *
         * @param args
         */
        public static void main(String[] args) {
    //       方法一
            List<Integer> list = new ArrayList<Integer>();
            list.add(8);
            list.add(9);
            list.add(6);
            list.add(8);
            list.add(7);
            list.add(5);
            list.add(0);
            System.out.println("排序前");
            System.out.println(list);
            System.out.println("排序后");
            Collections.sort(list);
            System.out.println(list);
            Collections.addAll(list);
    
            System.out.println("/*************************************************/");
            System.out.println("/                                                 /");
            System.out.println("/*************************************************/");
    
    //        方法二
            List<Integer> listAll = Arrays.asList(10, 11, 16, 9, 7, 5, 3, 0, 7, 88, 18, 28, 2);
            System.out.println("原始数据");
            listAll.forEach(integer -> {
                System.out.print(integer + "	");
            });
    
            System.out.println();
            System.out.println("升序排序");
            Collections.sort(listAll);
            listAll.forEach(integer -> {
                System.out.print(integer + "	");
            });
    
            System.out.println();
            System.out.println("降序排序");
            Collections.reverse(listAll);
            listAll.forEach(integer -> {
                System.out.print(integer + "	");
            });
            //        Collections.addAll() 方法的优点是无需进行数组向集合的转换,可以将数组直接添加到目标集合中,适合十万级左右数据 ;
            System.out.println();
            System.out.println("添加--重复上一数据值");
            Collections.addAll(listAll);
            listAll.forEach(integer -> {
                System.out.print(integer + "	");
            });
    
    //        Collections.shuffle()的作用是对集合进行重新打乱(随机排序)。
            System.out.println();
            System.out.println("Collections.shuffle()的作用是对集合进行重新打乱(随机排序)");
            Collections.shuffle(listAll);
            listAll.forEach(integer -> {
                System.out.print(integer + "	");
            });
    
    
        }
    }
    
    

    运行结果

    排序前
    [8, 9, 6, 8, 7, 5, 0]
    排序后
    [0, 5, 6, 7, 8, 8, 9]
    /*************************************************/
    /                                                 /
    /*************************************************/
    原始数据
    10	11	16	9	7	5	3	0	7	88	18	28	2	
    升序排序
    0	2	3	5	7	7	9	10	11	16	18	28	88	
    降序排序
    88	28	18	16	11	10	9	7	7	5	3	2	0	
    添加--重复上一数据值
    88	28	18	16	11	10	9	7	7	5	3	2	0	
    Collections.shuffle()的作用是对集合进行重新打乱(随机排序)
    0	7	7	88	11	2	9	5	18	10	3	16	28	
    Process finished with exit code 0
    
    
  • 相关阅读:
    启动与指定的文件或协议相关联的默认应用程序
    Windows phone msdn 索引
    34、ShareTarget
    36、UI contrast and settings
    Windows 8下默认管理员登录
    精益创业 Lean Startup
    38、animation
    access2003 基础 1008
    JQuery DOM
    用Javascript实现面向对象编程(封装,抽象,继承,多态)
  • 原文地址:https://www.cnblogs.com/d534/p/15546986.html
Copyright © 2011-2022 走看看