美文网首页
8.Collections工具类

8.Collections工具类

作者: 秋笙fine | 来源:发表于2019-02-12 23:40 被阅读0次

了解Collections类的功能。
在Java提供类库的时候考虑到用户使用方便性,所以专门提供了一个集合的工具类,它可以实现List,Set,Map集合的操作。

  • 为集合追加数据:
public static<T> boolean addAll(Collection<?super T> e,T...elements)

范例:

    public static void main(String[] args) throws Exception {
        List<String> all=new ArrayList<String>();
        Collections.addAll(all,"A","B","C","D");
        System.out.println(all);
    }
image.png

转置:reverse

    public static void main(String[] args) throws Exception {
        List<String> all=new ArrayList<String>();
        Collections.addAll(all,"A","B","C","D");
        Collections.reverse(all);
        System.out.println(all);
    }
image.png

面试题:请解释Collection与Collections的区别?

  • Collection是集合操作的接口
  • Collections是集合操作的工具类,可以进行List,Set,Map集合的操作。

总结:

这个类我们不会使用到,但是作为知识点,知道有这个类的存在就好。

相关文章

网友评论

      本文标题:8.Collections工具类

      本文链接:https://www.haomeiwen.com/subject/pvileqtx.html