• 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
    
    
  • 相关阅读:
    java导入导出下载Excel,xls文件(带下拉框)
    java操作poi生成excel.xlsx(设置下拉框)下载本地和前端下载
    layui文件上传中如何先判断后再弹出文件选择框
    下载excle文件之工具
    eclipse 配置JDK
    layer实现关闭弹出层刷新父界面功能详解
    兼容ie9以下支持媒体查询和html5
    layui学习地址
    MATLAB拟合正态分布
    Matlab文件操作
  • 原文地址:https://www.cnblogs.com/d534/p/15546986.html
Copyright © 2020-2023  润新知