List

List到字符串的转换

List.toString

public class Test {
   
    public static void main(String[] args) {
   
      List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list.toString());
    }
}

运行结果为:[1, 2, 3]

数组到List的转换

Arrays.asList(T… a)

List到数组的转换

Object[] toArray();

T[] toArray(T[] a)

public class Test {
   
    public static void main(String[] args) {
   
      List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        Object[] objects = list.toArray();
        System.out.println(Arrays.toString(objects));
    }
}

运行结果为:[1, 2, 3]

public class Test {
   
    public static void main(String[] args) {
   
        // List->数组
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("word");
        list.add("javase");
        String[] strings = new String[(list.size())];
        String[] arr = list.toArray(strings);
        System.out.println(arr.length);
        System.out.println(arr == strings);


        // 数组->List

        // 自动装箱
        List<Integer> integer = Arrays.asList(1, 2, 3, 4, 5, 6);
        System.out.println(integer.size());
        for (String i : list) {
   
            // 自动拆箱
            System.out.println(i);
        }
    }
}

运行结果为:

3
true
6
hello
word
javase

集合类

  • 什么是集合类

    面向对象语言中所有的事物都是以对象的形式体现出来的,所以为了方便多个对象的操作,Java就提供了集合类,集合类也是一种容器。

  • 数组和集合类的区别

    数组长度固定

    集合类长度可变

数组中可以存储基本数据类型,也可以存储引用数据类型

集合只能存储引用类型
数组只能存储一种数据类型

集合可以存储多种类型的引用类型对象
集合和数组可以互相转换

集合转数组->toArray(T a) 数组转集合要用到asList(T a)

  • 集合类的特点

    集合只用于存储对象,集合的长度是可以自动调节的

    集合可以存储不同引用类型的对象

Collection接口

boolean add(E e)

在集合尾部添加一个元素,判断指定的元素是否添加成功

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();
        System.out.println(collection.add("hello"));
        System.out.println(collection.add("world"));
        System.out.println(collection.add("java"));
        System.out.println(collection);
    }
}

运行结果为:

true
true
true
[hello, world, java]

boolean addAll(Collection c)

把集合collection的元素全部复制粘贴到原集合里,但是collection集合本身不变

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();
        collection.add("hello");
        collection.add("world");
        Collection collection1 = new ArrayList();
        
        collection1.add("JavaSe");
        
        collection.addAll(collection1);
        
        System.out.println(collection);
        System.out.println(collection1);
    }
}

运行结果为:

[hello, world, JavaSe]
[JavaSe]

boolean remove(Object o)

删除第一个与指定元素相等的元素

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();
        collection.add("hello");
        collection.add("world");
        collection.add("hello");

        System.out.println(collection);
        System.out.println(collection.size());

        collection.remove("hello");
        System.out.println(collection);
        System.out.println(collection.size());

        collection.remove("hello");
        System.out.println(collection);
        System.out.println(collection.size());
    }
}

运行结果:

[hello, world, hello]
3
[world, hello]
2
[world]
1

void clear()

清空集合

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();
        collection.add("hello");
        collection.add("world");
        System.out.println(collection);
        System.out.println(collection.size());

        collection.clear();

        System.out.println(collection);
        System.out.println(collection.size());
    }
}

运行结果:

[hello, world]
2
[]
0

boolean removeAll(Collection c)

从原集合中删除所有的collection集合元素,原理是遍历集合中的元素,如果该元素属于collection集合,那么就把他从原来集合中删除。原集合中的重复元素,如果属于collection集合,那么就会被全部删掉,不仅仅是删除一个

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();

        collection.add("hello");
        collection.add("world");
        
        Collection collection1 = new ArrayList();
        collection1.addAll(collection);
        collection1.add("666");
        collection1.add("hello");
        System.out.println(collection1);
        collection1.removeAll(collection);
        
        System.out.println(collection1);
    }
}

运行结果为:

[hello, world, 666, hello]
[666]

boolean retainAll(Collection c)

可以看做是把两个集合的交集放到原集合中,如果原集合发生了改变,则返回true

大概过程是:遍历原集合的元素,如果该元素也属于collection集合,就放回原集合,否则就舍弃

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/** * @description: * @Author 刘志敏 * @Date 2021/1/28 17:36 */
public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();

        collection.add("hello");
        collection.add("world");
        collection.add("java");
        collection.add("hello");

        Collection collection1 = new ArrayList();
        collection1.add("hello");
        collection1.add("world");
        System.out.println(collection);
        // collection中的java将会被舍弃
        System.out.println(collection.retainAll(collection1));
        System.out.println(collection);
        System.out.println("111111111111111111");
        collection1.add("hello");
        collection1.add("world");
        collection1.add("java");
        System.out.println(collection);
        System.out.println(collection.retainAll(collection1));
        System.out.println(collection);


    }
}

运行结果为:

[hello, world, java, hello]
true
[hello, world, hello]

111111111111111111

[[hello, world, hello]
false
[hello, world, hello]

boolean contains(Object o)

判断集合是否包含指定的元素

遍历原集合,查询是否有元素和指定的元素相等,方法内部用到了equals方法

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();

        collection.add("hello");
        collection.add("world");

        System.out.println(collection.contains("hello"));
        System.out.println(collection.contains("111"));

    }
}

运行结果:

true
false

boolean containsAll(Collection c)

判断collection集合是否是原集合的子集,collection集合中所有元素是否都属于原集合

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();

        collection.add("hello");
        collection.add("world");
        collection.add("java");

        Collection collection1 = new ArrayList();

        collection1.add("hello");
        collection1.add("world");

        System.out.println(collection.containsAll(collection1));
        collection1.add("666");
        System.out.println(collection.containsAll(collection1));

    }
}

运行结果为:

true
false

boolean isEmpty()

判断集合是否为空集合,为空则返回true

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();

        collection.add("hello");
        collection.add("world");
        collection.add("java");

        System.out.println(collection.isEmpty());
        // 清空集合
        collection.clear();
        System.out.println(collection.isEmpty());
    }
}

运行结果

false
true

int size()

返回集合的长度

public class Test {
   
    public static void main(String[] args) {
   
        Collection collection = new ArrayList();

        collection.add("hello");
        collection.add("world");
        collection.add("java");

        System.out.println(collection.size());
    }
}

运行结果:

3

Object[] toArray()

把集合转成数组,可以实现集合的遍历,基本不会使用此方法,不能通过数组修改集合,因为两个容器没有关联