Collection与Collections

Posted Lqc_javaEngineer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Collection与Collections相关的知识,希望对你有一定的参考价值。

package com.itheima.testinterfacess;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class AddingGroups

public static void main(String[] args) 
    Integer [] moreInts = 6,7,8,9,10; 
    /**
       public static <T> List<T> asList(T... a) 
            return new ArrayList<>(a);
       
     */
    List<Integer> asList = Arrays.asList(moreInts);
    //ArrayList()有一个含参构造方法:public ArrayList(Collection<? extends E> c)里面含有Collection的实现类就可以了
    Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
    //把整个集合添加进来
    collection.addAll(asList);

    for (Integer integer : collection) 
        System.out.print(integer);
    

    System.out.println("------------------------------------------");
    Collections.addAll(collection, 11,12,13,14,15);
    for (Integer integer : collection) 
        System.out.print(integer);
    
    System.out.println("------------------------------------------");
    Collections.addAll(collection, moreInts);
    for (Integer integer : collection) 
        System.out.print(integer);

    


输出结果:
12345678910——————————————
123456789101112131415——————————————
123456789101112131415678910

以上是关于Collection与Collections的主要内容,如果未能解决你的问题,请参考以下文章

java中Collection与Collections的区别

java类库 collection与collections (转)

Collection与Collections的区别

Collection包结构,与Collections的区别

Collection与Collections的区别

Java中Collection和Collections的区别