随笔 - 346  文章 - 38  评论 - 13  阅读 - 50万

[Java] Collections的简单运用

复制代码
package test.collections;

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

public class Test01 {
    public static void main(String[] args){
        //Set<String> songs = new HashSet<String>();
        List<String> songs = new ArrayList<String>();
        Collections.addAll(songs, "冷雨夜","海阔天空","love in the rain","海洋");
        //排序
        Collections.sort(songs);
        //随机排序
        Collections.shuffle(songs);
        //倒序
        Collections.reverse(songs);
        //交换顺序
        Collections.swap(songs, 2, 3);
        
        printList(songs);
    }
    public static void printList(Collection<String> c){
        for(String str:c){
            System.out.println(str);
        }
    }
}
复制代码

 

posted on   晏过留痕  阅读(138)  评论(0编辑  收藏  举报
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示