当前位置:科普知识站>IT科技>

java|collections

IT科技 阅读(2.15W)

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

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]

java collections

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]