首页 > 代码库 > Java Collections的排序之二

Java Collections的排序之二

package test.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Link_T {
    public static void main(String[] args) {
        //创建list
        List list = new ArrayList<>();
        // 添加内容
        int count = 20;
        for (int i = count; i > 0; i--) {
            list.add(i);
        }
        System.out.println(list);
        //输出结果:[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
        
        //排序(根据元素的自然顺序对指定的List集合按升序排序)
        Collections.sort(list);
        
        System.out.println(list);
        //输出结果:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
        
    }
}

 

Java Collections的排序之二