0/100发布文章 博文管理我的博客退出 Trash Temp list集合框架 list集合框架 list集合框架 list集合框架 Set Set集合 Set集合 Set集合 Set集合 Set集合 sql安装 sql安装 集合框架List集合 添加摘要 一、了解什么是集合框架(Collection)
集合框架的由来:数据多了用对象进行存储,对象多了用集合来进行存储,而存储数据的方式(数据结构)各有不同,所以存储的容器也就有多种,从而形成了集合框架这一体系。
在集合框架中提供了一个内部类专门操作集合中的元素,这个内部类就是迭代器。
整个集合框架大体上可以分为三种:
线性集合类型(List 接口:ArrayList、LinkedList…) 、
集合类型(Set 接口:HashSet、TreeSet…)、
映射类型(Map 接口:HashMap、TreeMap…)。
注意:Conllection接口不是Map集合的父接口!
Collection集合框架的图片
二、Collection之List接口
迭代器遍历
List l = new ArrayList();
l.add(22);
l.add(23);
l.add(26);
l.add(28);
l.add(55);
Iterator it = l.iterator();
while (it.hasNext()) {
/System.out.println(it.next());
}
第二种
下标遍历
List l = new ArrayList();
l.add(22);
l.add(23);
l.add(26);
l.add(28);
l.add(55);
for (int i = 0; i <l.size(); i++) {
// System.out.println(l.get(i));
// }
第三种forech遍历
List l = new ArrayList();
l.add(22);
l.add(23);
l.add(26);
l.add(28);
l.add(55);
for (Object object : l) {
System.out.println(object);
}
2、Iterator与ListIterator的区别:
list集合所特有的迭代器,ListIterator是Iterator的子接口
在迭代时,不可以通过集合对象的方法操作集合中的元素;
因为会发生并发修改异常(ConcurrentModificationException);
List的三个子类的特点:
①Arraylist:
数组结构 增删慢,查询快 有连续下标 线程不同步 增长因子为1.5 10
② Vector:
数组结构 增删改查都慢 有连续下标 线程同步 增长因子2 10
Vector相对ArrayList查询慢(线程安全的)
Vector相对LinkedList增删慢(数组结构)
③Linkedlist:
链表结构 增删快,查询慢 没有连续下标
ArrayList 如何进行性能调优?,如下图
public class listdome2 {
public static void main(String[] args) {
ArrayList al = new ArrayList<>(80);
for (int i = 0; i < 79; i++) {
al.add(i);
System.out.println(i+"---");
getLength(al);//从这里我们可以得到arraylist底层数组的长度为多少 增长又为多少
}
//如果我们需要对arraylist进行性能调优的话,直接对底层数组的初始长度进行扩大 new ArrayList<>(80);
}
public static void getLength(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 (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
- 使用LinkedList完成堆栈和队列
队列(先进先出):
public class LinkedListDemo {
public static void main(String[] args) {
// DuiZhan dz=new DuiZhan();//堆栈
Duilie dz=new Duilie();
dz.push("1");
dz.push("2");
dz.push("3");
dz.push("4");
dz.push("5");
dz.bianLi();
System.out.println("取前------取后");
dz.bianLi();
dz.pop();
}
}
class DuiZhan{
private LinkedList ll=new LinkedList<>();
/**
* 往堆栈的容器里面添加元素
* @param obj
*/
public void push(Object obj) {
ll.addFirst(obj);
}
public void bianLi() {
Iterator it=ll.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
public Object pop() {
return ll.removeFirst();
}
}
自定义对象去重
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList a=new ArrayList<>();
// a.add("wu");
// a.add("liang");
// a.add("wu");
// a.add("li");
// a.add("hei");
a.add(new Person("wu", 19));
a.add(new Person("li", 20));
a.add(new Person("liang", 20));
a.add(new Person("hei", 20));
a.add(new Person("wu", 19));
ArrayList newAl=repeat(a);
System.out.println(newAl.size());
}
/**
* Arraylist al这个容器是有重复元素的?
* 1.建立一个新的容器
* 2.将老的容器便利取出其中的元素
* 3.如果说这个元素存在于新容器中,那么不再往新容器加入,如果不存在,就加
* @param a
* @return
*/
public static ArrayList repeat(ArrayList a) {
ArrayList newAl=new ArrayList<>();
for (Object object : a) {
if(!newAl.contains(object)) {
newAl.add(object);
}
}
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) {
this.name = name;
this.age = age;
}
public Person() {
super();
}
@Override
public boolean equals(Object obj) {
Person p=(Person) obj;
// TODO Auto-generated method stub
System.out.println(p.name+"---equals----"+this.name);
return p.name.equals(this.name) && p.age==this.age;
}
}