java collections是什么,让我们一起了解一下?
Collections是集合类的一个工具类或帮助类,服务于Java的Collection框架,其中提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作。
Java中Collections有哪些用法?
1、sort(Collection)方法的使用(含义:对集合进行排序)。
示例如下:
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.sort(c);
System.out.println(c);
}
}
运行结果为:[l, o, v, e]
[e, l, o, v]2、reverse()方法的使用(含义:反转集合中元素的顺序)。
示例如下:
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
}
[one, two, three, four, five, six, siven][siven, six, five, four, three, two, one]3、shuffle(Collection)方法的使用(含义:对集合进行随机排序)。
示例如下:
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
}
}
运行结果为:[l, o, v, e]
[l, v, e, o]
[o, v, e, l]以上就是小编今天的分享了,希望可以帮助到大家。