[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); } } }
------------------------------------------------------------------
Always put yourself in the other's shoes.If you feel that it hurts you,it probably hurts others,too.------------------------------------------------------------------
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步