集合

1.定义

1.集合可以看作是一种容器,用来存储对象信息的容器,并且长度是可变的。

  • 因为对象是可以创建无限个数的,而对象数组是开辟固定长度的存储空间,显然用对象是不合适的

  • 对象数组长度不可变化并且无法保存具有映射关系的数据;集合可以用于存储数量不确定的数据,长度可变,以及可以保存具有映射关系的数据

  • 数组元素既可以是基本类型的值,也可以是对象;集合只能保存对象(比如list集合add(1)方法,是把1变成包装类Integer包装类)

  1. 所有的集合继承关系图(虽然很全,但是我们不是都要掌握,我们掌握工作中常用到的就可以),掌握这个文档的就足够你跟面试官吹逼了。

  1. 如果觉得上面太复杂,看这个简化版如图:

  1. 常用集合图

2.集合的使用

2.1 list集合

  • list接口集合代表一个有序、可重复的集合,集合每个元素都有其对应的顺序索引。list集合默认按照元素的添加顺序设置元素的索引,可以通过索引来访问指定位置的集合元素。

  • 实现list接口的集合常用的有:ArrayList、linkedList、vector子类等,实现list接口有哪些子类具体参考打开层级目录去看,不需要使用都学会。

2.1.1ArrayList

  • ArrayList底层是一个object类型的数组,也是我们最常用于数据遍历的集合.它允许任何符合规则的数据插入设置是null。该object类型的数组集合的默认长度为0,也可以自定义长度。每次所以list.add()方法插入元素时,对容器进行扩容。

  • 如图

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kv2k1oRi-1599571925845)(D:\downfile\QQ\tu1.png)]

  • list.get(i):直接获取数组的指定下标位置的元素,查询快。

    @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; }
    
  • ArrayList是一个有序可扩容长度的数组,所以可以插入有序的可重复的元素.优点是:可以快速通过下标获取到元素,遍历元素和查找元素快;由于插入、删除需要位移,所以插入、删除效率慢。

  • 同时ArrayList是非同步的,是非线程安全的,效率高。

  • 案例1:

    import java.util.ArrayList;
    import java.util.List;public class Test {public static void main(String[] args) {//底层是一个数组,每次插入值的时候判断有没有位置并且扩容保证数组能够无限的插入值List lists = new ArrayList();lists.add(100);lists.add("马云");lists.add(1000.98);lists.add(1,"王健林");lists.add(1,100);System.out.println("集合长度:" + lists.size());System.out.println("是否包含:"+(lists.contains(100)?"有100":"没有100"));System.out.println(lists.get(1));
    //      list.clear();//清空集合for (int i = 0; i <lists.size(); i++) {System.out.println(lists.get(i));}System.out.println("===============================");for (Object object : lists) {System.out.println(object);}}
    }
    
  • 结果

    集合长度:5
    是否包含:有100
    100
    100
    100
    王健林
    马云
    1000.98
    ===============================
    100
    100
    王健林
    马云
    1000.98
    
  • 案例2:添加对象类型

  • student类

    public class Student {private String name;private int age;private char sex;public void showStudent() {System.out.println("你好我是"+name+"年龄:"+age+"性别:"+sex);}public Student() {super();}public Student(String name, int age, char sex) {super();this.name = name;this.age = age;this.sex = sex;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}}
    
  • 测试类

    import java.util.ArrayList;
    import java.util.List;public class Test {public static void main(String[] args) {Student stu1 = new Student("马云",45,'男');Student stu2 = new Student("马化腾",46,'男');Student stu3 = new Student("董明珠",47,'女');Student stu4 = new Student("章泽天",28,'女');List list = new ArrayList();list.add(stu1);list.add(stu2);list.add(stu3);list.add(stu1);list.add(1,stu4);
    //      list.add("张三丰");因为add(odject)类型可以插入任意类型,Student student1 = (Student)list.get(1);student1.showStudent();System.out.println("================================");for (int i = 0; i < list.size(); i++) {Student student = (Student)list.get(i);student.showStudent();}System.out.println("==============================");list.remove(stu1);//根据对象删除list.remove(1);//根据下标删除for (Object object : list) {Student student2 = (Student)object;student2.showStudent();}}
    }
    
  • 结果

    你好我是章泽天年龄:28性别:女
    ================================
    你好我是马云年龄:45性别:男
    你好我是章泽天年龄:28性别:女
    你好我是马化腾年龄:46性别:男
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    ==============================
    你好我是章泽天年龄:28性别:女
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    ========================
    你好我是章泽天年龄:28性别:女
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    
  • 案例3

  • 用泛型约束集合中的元素类型

    import java.util.ArrayList;
    import java.util.List;public class Test {public static void main(String[] args) {Student stu1 = new Student("马云",45,'男');Student stu2 = new Student("马化腾",46,'男');Student stu3 = new Student("董明珠",47,'女');Student stu4 = new Student("章泽天",28,'女');//<Student>泛型 用于约束集合中约束的类型List<Student> list = new ArrayList<Student>();list.add(stu1);list.add(stu2);list.add(stu3);list.add(stu1);list.add(1,stu4);
    //      list.add("张三丰");因为定义集合的时候通过泛型约束了集合的类型是Student,所以只能插入Student类型的数据System.out.println("================================");for (int i = 0; i < list.size(); i++) {Student student =list.get(i);student.showStudent();}System.out.println("==============================");list.remove(stu1);//根据对象删除list.remove(1);//根据下标删除for (Student student : list) {student.showStudent();}}
    }
    
  • 结果

    你好我是章泽天年龄:28性别:女
    ================================
    你好我是马云年龄:45性别:男
    你好我是章泽天年龄:28性别:女
    你好我是马化腾年龄:46性别:男
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    ==============================
    你好我是章泽天年龄:28性别:女
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    ========================
    你好我是章泽天年龄:28性别:女
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    

2.1.2 LinkedList

  • LinkedList的实现机制与ArrayList的实现机制完全不同,ArrayLiat内部以数组形式保存数组,所以查询和遍历元素快,但插入和删除需要位移性能低;LinkedList内部以链表的形式保存集合的元素,所以查询和遍历效率比ArrayList低,插入和删除元素效率高

  • LinkedList.add()源码:

  • 把元素插到集合容器中的同时,将该元素记录在该元素的前、后的位置。所以插入和删除该元素不需要位移

        void linkLast(E e) {final Node<E> l = last;final Node<E> newNode = new Node<>(l, e, null);last = newNode;if (l == null)first = newNode;elsel.next = newNode;size++;modCount++;}
    
  • LinhkedList集合的get(i),是根据下标参数,先将index位置化为两半,如果index小于集合的一半就从第一个往后找直到找到index的值赋给x返回,如果大于就从集合最后的值往前找,直到找到index的值赋给x返回,这样可以减少一部分不必要的遍历,但是还是没有ArrayLiat直接找到下标位置的元素效率高。

        Node<E> node(int index) {// assert isElementIndex(index);if (index < (size >> 1)) {Node<E> x = first;for (int i = 0; i < index; i++)x = x.next;return x;} else {Node<E> x = last;for (int i = size - 1; i > index; i--)x = x.prev;return x;}}
    
  • 案例

    import java.util.LinkedList;
    import java.util.List;import day02.Student;public class Test {public static void main(String[] args) {Student stu1 = new Student("马云",45,'男');Student stu2 = new Student("马化腾",46,'男');Student stu3 = new Student("董明珠",47,'女');Student stu4 = new Student("章泽天",28,'女');LinkedList<Student> list = new LinkedList<Student>();list.add(stu1);list.add(stu2);list.addFirst(stu3);//插入最前面list.addLast(stu4);//插入最后面//获取第一个和最后一个对象list.getFirst().showStudent();list.getLast().showStudent();for (Student student : list) {student.showStudent();}for (int i = 0; i < list.size(); i++) {Student stu = list.get(i);stu.showStudent();}}
    }
    
  • 结果

    你好我是董明珠年龄:47性别:女
    你好我是章泽天年龄:28性别:女
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    你好我是马化腾年龄:46性别:男
    你好我是章泽天年龄:28性别:女
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    你好我是马化腾年龄:46性别:男
    你好我是章泽天年龄:28性别:女
    

2.1.3 vector

  • vector的add()方法的源码

    public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1);      elementData[elementCount++] = e; return true;
    }
    
  • synchronized修饰使方法同步,此集合线程安全。

  • ArrayList和Vector的区别

    • ArrayList集合和vector集合底层数组存储的操作都是一样的。ArrayList非线程安全,效率高;vector是线程安全的。
  • 案例

    public class Test {public static void main(String[] args) {Student stu1 = new Student("马云",45,'男');Student stu2 = new Student("马化腾",46,'男');Student stu3 = new Student("董明珠",47,'女');Student stu4 = new Student("章泽天",28,'女');//因为ArrayLiat是线程不安全的,而vector底层基本和ArrayList一致也是一个数组,并且线程安全Vector<Student> list = new Vector<Student>(); list.add(stu1);list.add(stu2);list.add(stu3);list.add(stu1);for (Student student : list) {student.showStudent();}}
    }
    
  • 结果

    你好我是马云年龄:45性别:男
    你好我是马化腾年龄:46性别:男
    你好我是董明珠年龄:47性别:女
    你好我是马云年龄:45性别:男
    

数据作为方法的参数
Map<String,Student> map = new LinkedHashMap<String,Student>();
map.put(stu1.getName(), stu1);
map.put(stu2.getName(), stu2);
map.put(stu3.getName(), stu3);
map.put(stu4.getName(), stu4);

 //获取到所有的键,是一个无序的set集合Set<String> keys = map.keySet();//创建迭代器,让无序的键排队Iterator<String> it = keys.iterator();while(it.hasNext()) {//判断迭代器容器里面有没有键,如果有就取到下一个键String key = it.next();//根据键获取值Student student = map.get(key);student.showStudent();}System.out.println("=================================");for (String key : keys) {Student student = map.get(key);student.showStudent();}System.out.println("===============================");for (Student student : map.values()) {student.showStudent();}
}

}


* 结果```java
你好我是马云,年龄是:45
你好我是马化腾,年龄是:40
你好我是董明珠,年龄是:47
你好我是章泽天,年龄是:28
=================================
你好我是马云,年龄是:45
你好我是马化腾,年龄是:40
你好我是董明珠,年龄是:47
你好我是章泽天,年龄是:28
===============================
你好我是马云,年龄是:45
你好我是马化腾,年龄是:40
你好我是董明珠,年龄是:47
你好我是章泽天,年龄是:28
10

11.集合之List相关推荐

  1. 11. 集合set类型详解

    python3 set类型的使用 1. 基础知识 (1) 集合(set)是由一个或数个形态各异的大小整体组成的,构成集合的事物或对象称作元素或是成员.集合是一个无序的不重复元素序列. (2)基本功能是 ...

  2. Python自动化开发 - 字符编码、文件和集合

    本节内容 字符编码 文件操作 集合 一.字符编码 1.编码 计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.解决思路:数字与符号建立一对一映射,用不同数字表示不同符号. ASCI ...

  3. 黑马程序员--数组与集合互相转变

    --------- android培训.java培训.期待与您交流! --------- 7 数组与集合的相互转变 7.1 数组转变成集合 1,为什么把数组转变成集合呢?有什么好处呢? 数组转变成集合 ...

  4. Python入门篇-高级数据类型集合(set)和字典(dict)

    Python入门篇-高级数据类型集合(set)和字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.集合(set) 1>.集合的特点 约定set 翻译为集合c ...

  5. c++ 字典_再来瞄一眼字典与集合?

    编程本是逆天行,你若不拼怎么赢? 字典相关 字典是Python语言中唯一的映射类型.映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的关系,通常被认为是可变的哈希表. 字典对象 ...

  6. 初学者python笔记(元组、字典、集合详解)

    文章目录 1. 元组(与列表最大的区别就是能否更改) 2. 本不可修改元组,但这样就可以修改了 3. 字典(dict类):键值对,无序(列表和元组都有序) 4. 字典的循环遍历 5. 特殊方法创建.取 ...

  7. 1.1.1.1校园网_高一数学上册必修1第一章知识点:1.1.1集合的含义与表示

    近日,北京高中生学习公 号 家教老师帮高一生整理了人教版高一数学上册必修1第一章1.11集合的含义与表示知识点,希望大家熟练掌握. 高一数学上册必修1知识点:第一章1.11集合的含义与表示知识点 1. ...

  8. Java进阶,Set集合,Map集合

    Java进阶,Set集合,Map集合 一.Set系列集合 1.Set系列集系概述 Set系列集合特点 无序:存取顺序不一致 不重复:可以去除重复 无索引:没有带索引的方法,所以不能使用普通for循环遍 ...

  9. Kotlin学习(二)—— 基本语法,函数,变量,字符串模板,条件表达式,null,类型检测,for,while,when,区间,集合

    一.基本语法 Kotlin的很多概念跟JAVA是有类似的,所以我应该不会像我的JAVA之旅一样那么的详细,但是不用担心,你会看的很明白的,我也是根据官方的文档来学习的 我们在IDEA中创建一个项目Ko ...

最新文章

  1. 2021清北毕业生去向关键词:进体制、搞教育、国内深造
  2. [实战]MVC5+EF6+MySql企业网盘实战(15)——逻辑重构2
  3. Java中继承thread类与实现Runnable接口的区别
  4. Tensorflow(0)--Tensorboard
  5. MySQL入门之备份与恢复
  6. 统计一个字符串中英文字母、空格、数字和其它字符的个数
  7. 2018年python工作好找吗-未来十年Python的前景会怎样?
  8. CodeForces 292D Connected Components (并查集+YY)
  9. vue改页面顶部浏览器标题栏图标、名称和地址栏详细教程
  10. F2FS的sysfs调整文件系统参数
  11. 我如何零基础转行成为一个自信的前端
  12. 鸿蒙系统跑分,麒麟9000+8GB内存 首发鸿蒙系统华为MatePad Pro2跑分首次曝光
  13. 拼多多店铺怎么布置装修,订单才能暴涨?
  14. 42个5G智慧教育应用场景,告诉你5G将如何改变教育
  15. JDK 8 - computeIfAbsent,computeIfPresent,compute
  16. 秀米svg点击显示另一张图_公众号排版怎么做?点击图片出现另一张图片是怎么弄的?...
  17. python中pygame背景颜色为啥没生效_如何更改pygame中的背景图像?
  18. 【FATE联邦学习】FATE联邦学习使用GPU、指定cuda下标
  19. 【信息系统项目管理师】第十五章 知识产权与法律法规(考点汇总篇)
  20. 第一学历和最高学历哪个更重要?

热门文章

  1. pythonocc安装_PythonOCC开发-如何搭建开发环境和一个创建圆台例子
  2. C++程序设计课程主页-2013级
  3. e3 v3服务器芯片组,最保值的E3-1230 v3遇上锐龙5:结果惊讶!
  4. 【机器学习】懒惰学习
  5. 1.cpt介绍与思科设备的基本配置
  6. Pytorch安装(Anaconda配置虚拟环境)(cpu版)
  7. Waukesha点火模块维修点火器维修740822A
  8. 使用SSD mobilenet训练自己的数据集
  9. 关于PHP中浏览器禁止Cookie后,Session能使用吗?
  10. 最近一直在想如何才能把我的想法变成现实