集合
一:集合框架
对象的容器,实现了对象常用的操作,类似数组功能。
二:集合和数组的区别
-
数组长度固定,集合长度不固定
-
数组可以存储基本类型和引用类型,集合只能存储引用类型
三:位置:java.util.*
四:Collection父类接口:
-
Collection表示一组纯数据,它有三个子接口,Set、List、Queue。
-
Set接口:不允许存放重复的元素、唯一、无序。
-
Map接口:不是Collection接口的子接口,它是单独的接口。
-
Map表示一组key-value对
-
-
特点:代表一组任意类型对象,无序,无下标,不能重复
Collection collection=new ArrayList();
// 1.添加元素
collection.add("蜀国");
collection.add("自挂");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
// 2.删除元素
// collection.remove("蜀国");
// System.out.println("删除之后:"+collection);
// 3.遍历元素[重点]
//增强for循环遍历
for (Object object : collection) {
System.out.println(object);
}
//迭代器遍历
Iterator iterator=collection.iterator();
//判断是否遍历到最后一个元素,遍历到了显示true
iterator.hashCode();
//获得元素
iterator.next();
//迭代器
Iterator iterator = list.iterator();
while (iterator.hasNext()){
String o = (String) iterator.next();
System.out.println(o);
}
//判断是否有这个元素
System.out.println(list.contains("水果"));
public static void main(String[] args) {
//新建Collection对象
Collection collection = new ArrayList<>();
Student s1=new Student("李永辉",20);
Student s2=new Student("代雅鑫",20);
Student s3=new Student("菲菲",20);
//添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//删除
collection.remove(s3);
System.out.println("元素个数:"+collection.size());
System.out.println("增强for循环===========");
for (Object obj:collection) {
Student student= (Student) obj;
System.out.println(student.toString());
}
System.out.println("迭代器===========");
Iterator iterator = collection.iterator();
while (iterator.hasNext()){
Student next = (Student) iterator.next();
System.out.println(next);
}
}
五:ArrayList
源码分析:
-
默认容量DEFAULT_CAPACITY = 10;
-
注意:如果没有向集合中添加任何元素,容量为0;
-
-
存放元素的数组elementData;
-
实际的元素个数size;
-
add()添加元素
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } //核心代码,给数组扩容 private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }
特点:
-
数据可以重复、有序;它在内存中是一个线性结构(数组也是线性结构)。它擅长遍历和查找速度很快、但是修改和删除原始不擅长。
代码实现:
-
创建学生类
/**
* 学生类
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
//判断是否是一个对象
if (this==o){
return true;
}
//判断是否为空
if (o==null){
return false;
}
//判断是否为Student类型
if (o instanceof Student){
Student s=(Student)o;
//比较属性
if (this.name.equals(s.getName())&&this.age==s.getAge()){
return true;
}
//不满足返回否
return true;
}
return false;
}
}
-
创建测试类
public static void main(String[] args) {
//创建集合
ArrayList list=new ArrayList();
//添加元素
Student s1=new Student("李永辉",20);
Student s2=new Student("代雅鑫",20);
Student s3=new Student("菲菲",20);
//添加数据
list.add(s1);
list.add(s2);
list.add(s3);
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
System.out.println("=============");
//删除元素
// list.remove(new Student("李永辉",20));
// System.out.println("删除之后:"+list.size());
//迭代器遍历
Iterator iterator=list.iterator();
while (iterator.hasNext()){
Student student = (Student) iterator.next();
System.out.println(student.toString());
}
//列表迭代器
ListIterator lit= list.listIterator();
System.out.println("----------列表迭代器----------");
while (lit.hasNext()){
Student student = (Student) lit.next();
System.out.println(student.toString());
}
System.out.println("----------列表迭代器逆序----------");
while (lit.hasPrevious()){
Student student = (Student) lit.previous();
System.out.println(student.toString());
}
//判断
System.out.println(list.contains(new Student("菲菲",20)));
//判断这个集合是空的吗
System.out.println(list.isEmpty());
//查询
System.out.println(list.indexOf(s1));
}
六:Vector
-
数组结构实现,查询快,增删慢
-
运行效率慢,线程安全
public static void main(String[] args) { //创建集合 Vector vector = new Vector<>(); //1.添加元素 vector.add("超美"); vector.add("我"); vector.add("是美"); System.out.println("元素个数:"+vector.size()); // //删除 // vector.remove(0); // vector.remove("超美"); // //删除所有 // vector.clear(); //2.遍历使用枚举器 Enumeration elements = vector.elements(); while (elements.hasMoreElements()){ String s= (String) elements.nextElement(); System.out.println(s); } System.out.println("================"); //增强for for (Object obj: vector) { String s= (String) obj; System.out.println(s.toString()); } //3.判断 System.out.println(vector.contains("我")); }
七:LinkedList
特点:链表结构实现,增删快,查询慢。
-
用上面那个Student学生类
-
创建测试类
public static void main(String[] args) { //创建集合 LinkedList linkedList = new LinkedList<>(); Student s1=new Student("李永辉",20); Student s2=new Student("代雅鑫",20); Student s3=new Student("菲菲",20); linkedList.add(s1); linkedList.add(s2); linkedList.add(s3); System.out.println("=====for====="); //1.for循环遍历 for (int i = 0; i < linkedList.size(); i++) { System.out.println(linkedList.get(i)); } System.out.println("=====增强for====="); for (Object obj:linkedList) { Student student= (Student) obj; System.out.println(student.toString()); } System.out.println("=====迭代器====="); ListIterator listIterator = linkedList.listIterator(); while (listIterator.hasNext()){ Student student = (Student) listIterator.next(); System.out.println(student.toString()); } }