有关集合的内容在帮助文档中位于java.util包下。 |
1、集合概述
- 生活中的集合:人或事物聚集到一起。
- 数学中的集合:由一个或多个确定的元素所构成的整体。
- Java中的集合:Java中的集合是一种工具类,可以存储任意数量的具有共同属性的对象。
- 比如我们要去超市买东西,我们购买的商品的数量是不固定的,显然不能采用数组来存储商品的信息。这时集合便应运而生,集合的长度是可以动态改变的。
- 集合的应用场景:
- 无法预测存储数据的数量;
- 同时存储具有一对一关系的数据(比如同时存储商品的编号和商品的信息,是一一对应的);
- 需要进行数据的增删;
- 数据重复的问题(例如Set是不允许插入重复数据的);
2、集合框架的体系结构
- Java集合框架的体系结构如下图所示:
- 首先,集合框架分为两类:Collection和Map。
- 在Collection当中存储类的对象。
- Map是以键值对的形式来存储信息。
- 在Collection这个接口下有三个子接口:List、Queue和Set。
- List:表示序列。存放的数据要求是有序的,也允许重复。List接口下有个常用的实现类是ArrayList,它可以被看作是长度动态增长的数组。
- Queue:表示队列。存放的数据要求是有序的,也允许重复。Queue下有个实现类LinkedList,它同时也实现了List的接口,表示链表的内容。
- Set:表示集。Set中存放的数据是无序的,并且不允许重复。Set的实现类主要是HashSet,也就是哈希集。
- Map的一个主要实现类是HashMap,就是哈希表的意思,存储以键值对
<Key,Value>
形式表示的数据。
3、List集合
- List是元素有序并且可以重复的集合,称为序列。它是一个接口。
- List可以精确地控制每个元素的插入位置,或删除某个位置的元素。
- List主要有两个实现类ArrayList和LinkedList。两者存储数据的方式是不一样的。
- ArrayList和数组比较相似,只不过它的长度是动态增长的,它和数组一样,都是在内存中连续的存储空间进行存储数据的。
- LinkLIst是链表。它实现了List和Queue两个接口。
3.1、ArrayList概述
- ArrayList底层由数组实现的。
- 并且ArrayList长度是动态变化的,以满足应用程序的需求。
- 在列表的尾部插入或删除数据效率很高(在中间位置插入或删除数据,就要进行大量的数组复制,这样会较多地消耗资源)。
- ArrayList更适合查找和更新元素。
- ArrayList中的元素可以为null。
3.2、使用ArrayList对字符串进行管理
- 1.新建一个名为
CollectionProj
的Java项目,新建一个名为com.cxs.list
的包,在包中新建一个名为ArrayListDemo
类,并勾选主方法。
public class ArrayListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Go");
list.add("Swift");
System.out.println("列表中元素个数为:" + list.size());
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println("-------------------");
System.out.println();
list.remove("C++");
System.out.println("移除C++后的列表元素有:" + list.size());
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
}
}
- 2.运行代码,结果如下。
3.3、使用ArrayList对自定义类进行管理
- 3.在
list
包下新建一个名为Notice
的类,用于编写公告。
public class Notice {
private int id;
private String title;
private String creator;
private Date createTime;
public Notice() {
}
public Notice(int id, String title, String creator, Date createTime) {
super();
this.id = id;
this.title = title;
this.creator = creator;
this.createTime = createTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
- 4.新建一个名为NoticeTest的测试类,并勾选主方法。
public class NoticeTest {
public static void main(String[] args) {
Notice notice1 = new Notice(1, "欢迎来到慕课网!", "管理员", new Date());
Notice notice2 = new Notice(2, "请按时提交作业!", "老师", new Date());
Notice notice3 = new Notice(3, "考勤通知!", "老师", new Date());
ArrayList<Notice> noticeList = new ArrayList<>();
noticeList.add(notice1);
noticeList.add(notice2);
noticeList.add(notice3);
System.out.println("公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println((i + 1) + ":" + noticeList.get(i).getTitle());
}
}
}
- 5.运行代码结果如下。
- 6.修改NoticeList代码,添加以下代码。
System.out.println("---------------------");
Notice notice4 = new Notice(4, "在线编辑器可以使用了!", "管理员", new Date());
noticeList.add(1, notice4);
System.out.println("添加公告后,公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println((i + 1) + ":" + noticeList.get(i).getTitle());
}
System.out.println("---------------------");
noticeList.remove(notice2);
System.out.println("删除公告后,公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println((i + 1) + ":" + noticeList.get(i).getTitle());
}
System.out.println("---------------------");
noticeList.get(1).setTitle("Java在线编辑器可以使用了!");
System.out.println("修改公告后,公告的内容为:");
for (int i = 0; i < noticeList.size(); i++) {
System.out.println((i + 1) + ":" + noticeList.get(i).getTitle());
}
- 7.运行代码,结果如下。
4、Queue集合
4.1、LinkedList概述
- 与ArrayList一样,LinkedList也按照索引位置排序,但它的元素之间是双向链接的。
- 适合快速地插入和删除元素
- LinkedList实现LIst和Queue两个接口
4.2、使用LinkedList对字符串进行管理
4.3、使用LinkedList对自定义类进行管理