集合的概念
集合是包含多个对象的简单对象,所包含的对象称为元素。集合里面可以包含任意多个对象,数量可以变化;同时对对象的类型也没有限制,也就是说集合里面的所有对象的类型可以相同,也可以不同。
集合框架中各接口的特点:
1:Collection接口 : 是一组允许重复的对象。
2:Set接口: 继承Collection,无序但不允许重复。
3:List接口 :继承Collection,有序但允许重复,并引入位置下标。
4:Map接口: 既不继承Set也不继承Collection,是键值对。
List 的集合
List是一个继承于Collection的接口,即List是集合中的一种。List是有序的队列,可重复的集合,List中的每一个元素都有一个索引;第一个元素的索引值是0,往后的元素的索引值依次+1。
List接口中增加了一些根据索引操作元素的方法:
void add(int index,E element ) //在列表的指定位置插入该元素
boolean addAll(int index,Collection c) //将集合c包含的所有元素都插入到List集合的index处。
Object get(int index) // 返回集合index索引出的元素。
List:凡是可以操作角标的方法都是该体系所特有的方法
// 增
Add(index,element)
Add(index,Collection)
// 删
Remove(index)
// 改
Set(index,element)
// 查
Get(index)
subList(from,to)
listIterator()
index(element)
List 的 三种遍历方式
for (Object obj : al) {
System.out.println(obj);
}
for (int i = 0; i < al.size(); i++) {
System.out.println(al.get(i));
}
/*Iterator it = al.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
*/
Iterator it = al.iterator();
while(it.hasNext()) {
int obj = (Integer)it.next();
//删除容器中所有元素 java.util.ConcurrentModificationException,出现并发问题
//al.remove(obj);
if(obj % 2 == 0) {
System.out.println(it.next());
}
}
集合框架Vector
vector 数组结构 增删改查都慢 有连续下标 线程同步
public class VectorDemo {
public static void main(String[] args) {
Vector v = new Vector<>();
v.add(22);
v.add(24);
v.add(25);
v.add(26);
v.add(27);
Enumeration elements = v.elements();
while(elements.hasMoreElements()) {
System.out.println(elements.nextElement());
}
}
}
集合框架LinkedList
以链表结构存储数据 增删快,查询慢 没有连续下标
特有的方法
addFirst();
addLast();
获取元素但是不删除元素,如果集合中没有元素,会出现NoSuchElementException
getFirst();
getLast();
获取元素的同时会删除元素,如果集合中没有元素,会出现NoSuchElementException
removeFirst();
removeLast();
在jdk1.6出现了替代方法
offerFirst()
offerLast()
peekFirst();
peekLast();
获取元素的同时会删除元素,如果集合中没有元素,会返回null
pollFirst();
pollLast();
由于是链表结构,当前节点能够记住上一个节点、以及下一个节点,所有有带First、last的方法存在。
使用LinkedList完成一个堆栈 (堆栈先进后出)
package com.Test;
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<>();
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());
}
}
}
结果:
使用LinkedList完成一个队列(队列先进先出)
package com.Test;
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<>();
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中的重复元素去重及其底层原理
去除 list 集合中重复元素
package com.Test;
import java.util.ArrayList;
import org.apache.jasper.tagplugins.jstl.core.Remove;
public class ArrayListRepeatDemo {
public static void main(String[] args) {
ArrayList al = new ArrayList<>();
/*al.add("zs");
al.add("ls");
al.add("ww");
al.add("mz");
al.add("zs");*/
al.add(new Person("zs", "18"));
al.add(new Person("ls", "15"));
al.add(new Person("ww", "26"));
al.add(new Person("mz", "24"));
// al.add(new Person("zs", "18"));
al.remove(new Person("zs", "18"));
// ArrayList newAll = repeatlist(al);
System.out.println(al.size());
// System.out.println(newAll.size());
}
private static ArrayList repeatlist(ArrayList al) {
// TODO Auto-generated method stub
ArrayList newAll = new ArrayList<>();
for (Object obj : al) {
if(!newAll.contains(obj)) {
newAll.add(obj);
}
}
return newAll;
}
}
class Person {
private String pname;
private String age;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person [pname=" + pname + ", age=" + age + "]";
}
public Person() {
}
public Person(String pname, String age) {
this.pname = pname;
this.age = age;
}
// 重写 equals 方法
@Override
public boolean equals(Object obj) {
if(obj instanceof Person) {
Person p = (Person)obj;
System.out.println(this.getPname() +"-----equals-----" +p.getPname());
return this.getPname().equals(p.getPname()) && this.getAge() == p.getAge();
}
return false;
}
}
论证增长因子
增长因子为1.5 10
package com.Test;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) throws Exception {
ArrayList al = new ArrayList<>(50);
for (int i = 1; i < 60; i++) {
al.add(i);
System.out.print(i+",");
getLen(al);
}
}
public static void getLen(ArrayList al) throws Exception {
Field field = al.getClass().getDeclaredField("elementData");
field.setAccessible(true);
Object obj = field.get(al);
Object[] elementData = (Object[])obj;
System.out.println("当前容器的长度:" +elementData.length);
}
}
** ArrayList,Vector主要区别为以下几点**
- Arraylist 数组结构 增删慢,查询快 有连续下标 线程不同步 增长因子为1.5 10
- vector 数组结构 增删改查都慢 有连续下标 线程同步 增长因子2 10
- Vector多了一个public Vector(int initialCapacity, int capacityIncrement)构造器,可以设置容 量增长,ArrayList是没有的。
Collection集合的remove方法 与 迭代器的remove方法的区别
Collection的remove适合于非遍历情况下的删除,直接调用底层自己实现的remove方法,但是因为对底层实现的不了解,所以当其在遍历情况下,将导致异常或者BUG,比如在ArrayList中,直接删除元素后,所有之后的元素会向前移一位,如果不考虑清楚,则可能会遗漏若干个对象的判断,而Iterator的remove方法是由底层各个实现专门为遍历删除时提供的方法,其的安全性,准确性,效率都高于Collection的remove()方法。
ArrayList与Array的区别?
Array是数组:长度在数组申明时候,就已经确定了;存放的元素也确定了; ArrayList:长度是可变的;存放元素随意