List和Collection接口

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。List和Collection接口,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

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

List

Set

ArrayList

Vector

LinkList

HashSet

TreeSet

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()

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/181130.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!