文章目录
一. list集合的特点
- 1.1 list集合本身就是一个容器,具有增删改查的特点。
- 1.2 有序性:元素添加的顺序即为元素输出的顺序。
- 1.3 可重复性。
- 1.4list的两个子类 ArrayList与LinkedList的区别:
ArrayList的数据结构是数组,查询修改快,增加删除慢
LinkedList的数据结构是链表,查询修改慢,增加删除快。
二. list集合的遍历方式
2.1 for循环通过下标遍历
2.2 通过foreach遍历
2.3 通过迭代器遍历
三. 用LinkedList完成一个堆栈容器
堆栈:就是实现集合元素先存进去的后输出,后存进去的先输出。(先进先出)
四. list调优(增长因子)
元素存储在集合的过程
长度增加为原来的1.5倍
1+0.5,0.5是增长的因子
五. list去重原理
通过equals方法判断是否相同来去除重复项。
package com.xissl.collection01;
import java.util.ArrayList;
import java.util.List;
/**
* list底层对象去重原理
* @author xissl
*
*/
public class Demo04 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new student(1, "yh"));
list.add(new student(2, "nb"));
list.add(new student(3, "lj"));
List Newlist = new ArrayList<>();
for (Object object : list) {
if(!Newlist.contains(object)) {
Newlist.add(object);
}
}
System.out.println("去重前的list元素"+list);
System.out.println("去重后的list元素"+Newlist);
}
}
class student{
private int id;
private String name;
public student() {
// TODO Auto-generated constructor stub
}
public student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "student [id=" + id + ", name=" + name + "]";
}
@Override
public boolean equals(Object obj) {
System.out.println("重写了equals方法");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
student other = (student) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}