欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java集合-Collections工具类

程序员文章站 2024-01-18 19:23:40
...

Java Collections工具类

  • java.util.Collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for(int i = 0; i < 5; i++){
            list.add("x" + i);
        }
        System.out.println(list); //[x0, x1, x2, x3, x4]

        Collections.shuffle(list);
        System.out.println("随机排序:" + list); 

        Collections.reverse(list);
        System.out.println("逆序:" + list);

        Collections.sort(list);
        System.out.println("顺序:" + list);

        System.out.println(Collections.binarySearch(list, "x2")); //2
        
        Collections.fill(list,"something");
        System.out.println(list);  //[something, something, something, something, something]
    }
}
相关标签: JavaSE java