一、集合框架的基本介绍
1、简介
jdk原码网址:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
集合框架中提供了一个内部类专门操作集合中的元素,这个内部类就是迭代器
数据结构
集合框架的由来:数据多了用对象进行存储,对象多了用集合来进行存储。
而存储数据的方式(数据结构)各有不同,所以存储的容器也就有多种,从而形成了集合框架这一体系。
List 有序 元素可以重复 因为该集合体系有索引
* Arraylist 数组结构 增删慢,查询快 有连续下标 线程不同步 增长因子为1.5 10
* vector 数组结构 增删改查都慢 有连续下标 线程同步 增长因子2 10
list集合的调优
* Linkedlist 链表结构 增删快,查询慢 没有连续下标
* Set 无序 元素不可以重复
2、集合框架思维导图(三大类)
二、 接口Collection ,迭代器
1、Iterator迭代器
package com.qiuwenfan.list;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
①基本显示
public static void main(String[] args) {
Collection c=new ArrayList();
c.add(22);
c.add(23);
c.add(25);
c.add(27);
c.add(30);
// 增强for循环
for (Object obj : c) {
System.out.println(obj);
}
2、集合的remove方法与迭代器的remove方法有什么区别?
①在迭代器或者foreach循环删除的时候容易出现什么常见的问题 it.next()
迭代器是集合所特有的遍历方式
Iterator it=c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
int num=(int) it.next();
if(num % 2 == 0) {
System.out.println(it.next());
System.out.println(num);
}
}
②在迭代器中执行collection.remove方法
Iterator it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
int num=(int) it.next();
if(num % 2 == 0) {
// System.out.println(it.next());
// System.out.println(num);
it.remove();
c.remove(num);
}
}
System.out.println(c);
java.util.ConcurrentModificationException 本质上来说,这是一个并发问题
三、集合框架List(ArrayList特有方法、特有迭代器、具体对象特点、增长因子论证)
1、List:凡是可以操作角标的方法都是该体系所特有的方法
* 增
* Add(index,element)
* Add(index,Collection)
*
* 删
* Remove(index)
*
* 改
* Set(index,element)
*
* 查
* Get(index)
* subList(from,to)
* listIterator()
* index(element)
*
* list集合所特有的迭代器,ListIterator是Iterator的子接口
* 在迭代时,不可以通过集合对象的方法操作集合中的元素;
* 因为会发生并发修改异常(ConcurrentModificationException);
* 所以,在迭代时,只能通过迭代器来操作元素,课时Iterator的方法是有限的,Iterator只提供了判断、
* 取出、删除的操作;
* 如果想要有其他的操作,如添加、修改等,就需要使用其子接口ListIterator,该接口只能list集合的listIterator方法来获取
*/
2、list集合相对于collection集合所特有的方法
package com.qiuwenfan.list;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ListDemo {
public static void main(String[] args) {
List c=new ArrayList();
c.add(22);
c.add(23);
c.add(25);
c.add(27);
c.add(30);
// Iterator it = c.iterator();
// while(it.hasNext()) {
// System.out.println(it.next());
// }
ListIterator it = c.listIterator();
while(it.hasNext()) {
System.out.println(it.next());
}
System.out.println("-------------");
while(it.hasPrevious()) {
System.out.println(it.previous());
}
}
}
输出结果
注意:如果
ListIterator it = c.listIterator();
while(it.hasNext()) {
System.out.println(it.next());
}
没有,下面的也不会打印
3、论证增长因子
① Arraylist 数组结构 增删慢,查询快 有连续下标 线程不同步 增长因子为1.5 10
② vector 数组结构 增删改查都慢 有连续下标 线程同步 增长因子2 10
package com.qiuwenfan.list;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class ListDemo2 {
public static void main(String[] args) {
ArrayList al = new ArrayList<>();
for (int i = 1; i <= 80; i++) {
al.add(i);
System.out.println(i+",");
getLen(al);
}
}
public static void getLen(ArrayList al) {
try {
Field f = al.getClass().getDeclaredField("elementData");
f.setAccessible(true);
Object obj = f.get(al);
Object[] elementData = (Object[]) obj;
System.out.println("当前al容器的底层数组的长度,"+elementData.length);
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
四、集合框架LinkedList
1、链表结构
特有方法
addFirst();
addLast();
获取元素但是不删除元素,如果集合中没有元素,会出现NoSuchElementException
getFirst();
getLast();
获取元素的同时会删除元素,如果集合中没有元素,会出现NoSuchElementException
removeFirst();
removeLast();
在jdk1.6出现了替代方法
offerFirst()
offerLast()
peekFirst();
peekLast();
获取元素的同时会删除元素,如果集合中没有元素,会返回null
pollFirst();
pollLast();
由于是链表结构,当前节点能够记住上一个节点、以及下一个节点,所有有带First、last的方法存在。
2、模拟队列以及堆栈
堆栈:先进后出 子弹夹
队列:先进先出 水管
①堆栈结构
package com.qiuwenfan.list;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
DuiZhan dz = new DuiZhan();
dz.push("a");
dz.push("b");
dz.push("c");
dz.push("d");
dz.push("e");
dz.bianLi();
}
}
/**
* 堆栈的容器
*
*/
class DuiZhan{
private LinkedList ll = new LinkedList<>();
/**
* 往堆栈结构的容器添加元素
* @param obj
*/
public void push(Object obj) {
ll.addFirst(obj);
}
public Object pop() {
return ll.removeFirst();
}
public void bianLi() {
Iterator it = ll.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
②队列结构
package com.qiuwenfan.list;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
Duilie dl = new Duilie();
dl.push("a");
dl.push("b");
dl.push("c");
dl.push("d");
dl.push("e");
dl.bianLi();
}
}
/**
* 队列的容器
*
*/
class Duilie{
private LinkedList ll = new LinkedList<>();
/**
* 往队列结构的容器添加元素
* @param obj
*/
public void push(Object obj) {
ll.addLast(obj);
}
public Object pop() {
return ll.removeFirst();
}
public void bianLi() {
Iterator it = ll.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
五、集合框架ArrayList中的重复元素去重及其底层原理
1、元素去重
判断list 集合中元素是否相同,依据的是元素的equals方法
Contains调用了equals
Remove调用了equals
①字符串去重
list中存储的是字符串,而string的equals方法就是比的字符串值
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList al = new ArrayList<>();
al.add("abc");
al.add("def");
al.add("abc");
al.add("aaa");
al.add("bbb");
/**
* ArrayList al这容器中是有重复元素的?
* 1、建立一个新的容器
* 2、将老的容器遍历取出其中的元素
* 3、如果说这个元素存在于新容器中,那么不再往新容器加入。如果不存在,就加
*
* @param al
* @return
*/
public static ArrayList repeat(ArrayList al) {
ArrayList newAl = new ArrayList<>();
for (Object obj : al) {
if(!newAl.contains(obj)) {
newAl.add(obj);
}
}
return newAl;
}
}
②引用数据类型去重(同姓名同年龄视为同一个人)
思路
1、对人进行描述,将数据封装进对象
2、将对象放入容器
3、去重
引用数据类型被调用了equals方法验证
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList al = new ArrayList<>();
al.add(new Person("aaa",13));
al.add(new Person("bbb",14));
al.add(new Person("abc",17));
al.add(new Person("ccc",23));
al.add(new Person("aaa",13));
ArrayList newAl = repeat(al);
System.out.println(newAl.size());
}
/**
* ArrayList al这容器中是有重复元素的?
* 1、建立一个新的容器
* 2、将老的容器遍历取出其中的元素
* 3、如果说这个元素存在于新容器中,那么不再往新容器加入。如果不存在,就加
*
* @param al
* @return
*/
public static ArrayList repeat(ArrayList al) {
ArrayList newAl = new ArrayList<>();
for (Object obj : al) {
if(!newAl.contains(obj)) {
newAl.add(obj);
}
}
return newAl;
}
}
class Person{
private String name;
private int 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 "Person [name=" + name + ", age=" + age + "]";
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
}
@Override public boolean equals(Object obj) { Person p = (Person) obj; System.out.println(p.name+"——equals——"+this.name); return p.name.equals(this.name) && p.age==this.age; }
}
③底层原理
- 集合collection的contains在调用的时候底层调用容器元素对象的equals方法
- 之前元素对象String
- 元素对象是Object(Person)