1.默认情况下(sort)
public static void main(String[] args) { Listlist = Arrays.asList( "浙江移动", "江苏移动", "浙江电信", "上海移动", "北京移动", "浙江联通"); Collections.sort(list); System.out.println(StringUtils.join(list.toArray(), ",")); }
运行结果:
上海移动,北京移动,江苏移动,浙江电信,浙江移动,浙江联通
2.按汉字拼音升序
public static void main(String[] args) { Listlist = Arrays.asList( "浙江移动", "江苏移动", "浙江电信", "上海移动", "北京移动", "浙江联通"); Collections.sort(list, Collator.getInstance(java.util.Locale.CHINA)); System.out.println(StringUtils.join(list.toArray(), ",")); }
运行结果:
北京移动,江苏移动,上海移动,浙江电信,浙江联通,浙江移动
3.逆序排序(reverse)
public static void main(String[] args) { Listlist = Arrays.asList( "浙江移动", "江苏移动", "浙江电信", "上海移动", "北京移动", "浙江联通"); Collections.sort(list, Collator.getInstance(java.util.Locale.CHINA)); Collections.reverse(list); //反转整个数组 System.out.println(StringUtils.join(list.toArray(), ",")); }
运行结果:
浙江移动,浙江联通,浙江电信,上海移动,江苏移动,北京移动
4.混排--重新洗牌(Shuffling)
public static void main(String[] args) { Listlist = Arrays.asList( "浙江移动", "江苏移动", "浙江电信", "上海移动", "北京移动", "浙江联通"); Collections.shuffle(list); //随机排序 System.out.println(StringUtils.join(list.toArray(), ",")); Collections.shuffle(list); //随机排序 System.out.println(StringUtils.join(list.toArray(), ",")); } 运行结果: 浙江电信,浙江移动,上海移动,江苏移动,浙江联通,北京移动 浙江移动,江苏移动,北京移动,上海移动,浙江电信,浙江联通
5.循环移动(rotate)
public static void main(String[] args) { Listlist = Arrays.asList( "浙江移动", "江苏移动", "浙江电信", "上海移动", "北京移动", "浙江联通"); Collections.rotate(list, 1); //向右移动1 System.out.println(StringUtils.join(list.toArray(), ",")); Collections.rotate(list, -2); //向左移动2 System.out.println(StringUtils.join(list.toArray(), ",")); } 运行结果: 浙江联通,浙江移动,江苏移动,浙江电信,上海移动,北京移动 江苏移动,浙江电信,上海移动,北京移动,浙江联通,浙江移动
6.换位(swap)
public static void main(String[] args) { Listlist = Arrays.asList( "浙江移动", "江苏移动", "浙江电信", "上海移动", "北京移动", "浙江联通"); Collections.swap(list, 0, 1); //向右移动1 System.out.println(StringUtils.join(list.toArray(), ",")); } 运行结果: 江苏移动,浙江移动,浙江电信,上海移动,北京移动,浙江联通