一、集合框架
想到集合,首先想到的应该是数据的存储格式
Collection
|--List
|--Vector
|--ArrayList
|--LinkedList
|--Set
|--HashSet
|--TreeSet
Map
|--Hashtable
|--Properties
|--HashMap
|--TreeMap
集合存储的是对象,长度可变。数组只能存储基本数据类型,长度不可变。
集合中存储的都是对象的引用,操作就是直接操作集合的。
不同的结合存储的数据结构是不一样的。
Collection
(一)List
有序,可重复
1.Vector
(1)底层是数组,同步,被ArrayList代替了
(2)使用方式:枚举类进行迭代,枚举类是容器,和迭代器一样,但是被迭代器代替了。
import java.util.Enumeration;
import java.util.Vector;
public class Test{
public static void main(String[] args) {
Vector<User> v = new Vector<User>();
v.add(new User("张三",21));
v.add(new User("李四",22));
v.add(new User("王五",23));
Enumeration<User> ele = v.elements();
while(ele.hasMoreElements()){
User user = ele.nextElement();
System.out.println(user.getName());
}
}
}
2.ArrayList
(1)底层是数组,只要是有索引下标的,就是它
(2)特点:查询块,不同步,多线程使用的时候加锁就可以
3.LinkedList
(1)底层是链表,可以操作头尾
(2)特点:查询慢,增删改快,不同步
4.知识点
(1)contains,remove根据equals方法判断,所以集合中的类可以重写equals方法
(2)一般做查询做的多,所以ArrayList使用频率最高
(二)Set
无序,不可重复
1.HashSet
(1)底层是哈希表,元素不允许重复,判断依据是hashCode和equals方法,哈希表中存储的数据是按照一定的算法进行存储的
(2)保证唯一性就需要集合中的对象重写hashCode和equals。
(3)基本类型包装类、String,Date、File重写了hashCode和equals和compareTo方法
(4)一般对象都要重写hashCode和equals方法
2.TreeSet
(1)有序的,底层是二叉树
(2)保证集合中对象有序,唯一性的方法:集合中的对象实现comparable接口,重写compareTo方法,让对象具有可比较性
(3)实现排序的两种方式:
方式1:集合中对象实现comparable接口
方式2:不修改源码,定义比较器,在构造集合时作为参数中传入。(可用于修改比较规则,推荐)
(4)控制排序顺序:compareTo的不同结果可以控制正序,倒序
(5)集合与泛型经常一起使用,可以编译期检查,更加安全。
举例:@Override
public int hashCode() {
return this.name.hashCode()+age*60;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof User)){
return false;
}
User uu = (User)obj;
return this.name.equals(uu.getName());
}
@Override
public int compareTo(User o) {
//注意:比较的是这个对象的信息和你传进来的对象的信息
if(this.age>o.getAge()){
return 1;
}else if(this.age==o.getAge()){
return this.name.compareTo(o.getName());//次要条件排序
}else{
return -1;
}
}
Collection集合的迭代器:
(1)迭代器Iterator是Collection的集合的共性的抽取。
(2)操作集合A
在使用迭代器操作集合的时候,就不能使用这个集合的方法同时进行操作,相当于这个集合是共享资源,有两个线程在同时操作,集合是没有加锁的,所以会发生并发异常。
public static void main(String[] args) {
List<User> list = Test.getList();
Iterator<User> it = list.iterator();
while(it.hasNext()){
User u = it.next();
if("张三".equals(u.getName())){
it.remove();//不能使用list.remove()
}
}
}
知识点:
(1)直接修改集合,因为指向的是同一个地址
public static void main(String[] args) {
List<User> list = Test.getList();
Iterator<User> it = list.iterator();
while(it.hasNext()){
User u = it.next();
if("张三".equals(u.getName())){
//因为u指向的就是集合指向的对象,所以这里可以直接进行修改
//可以直接进行修改,不用重新再放进去,可以直接就进行修改了
u.setName("张张");
}
}
}
(2)重写hashCode、equals、compareTo的时候条件:主要条件,次要条件。可以使用现有的类。
String、包装类重写了hashCode,equals,compareTo方法
class MyCom implements Comparator<User>{
//先按照姓名排序,然后按照age排序
@Override
public int compare(User o1, User o2) {
int num = o1.getName().compareTo(o2.getName());//使用String
if(num == 0)
return new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));//使用Integer
return num;
}
}
(3)保证集合中对象有序的两种方式
(1)让User具备可比较性,实现Comparable
TreeMap<User> t = new TreeMap<User>();
t.add(new User(...));
t.add(new User(...));
(2)重新写一个比较器,不用在User中实现比较接口(这也可以用于改变原来的比较数据方式)
MyCom implements Comparator<User>
TreeMap<User> t = new TreeMap<User>(new MyCom());
t.add(new User(...));
t.add(new User(...));
Map
(1)Map不在Collection框架中
(2)key-value形式,键是唯一的。所以键的对象需要重写hashCode和equals方法。
(3)取出元素的两种方式:(1)keySet键的集合(2)entrySet键值的集合。然后分别进行迭代。
1.Hashtable
(1)底层是哈希表,同步的,不允许null-null,因为加锁了,所以慢,效率低
(2)有一个子类:Properties(系统属性类)
2.HashMap
底层是哈希表,不同步的,允许null-null,因为没有加锁了,所以快,效率高
3.TreeMap
(1)底层是二叉树,不同步,对键进行排序
(2)保证集合中对象是有序的办法:集合中对象具备比较性,实现comparable接口
(3)改变比较规则的办法:定义一个比较器,在创建集合的时候传入
举例://默认的先比较年龄,再比较姓名
public int compareTo(Student s) {
int num = new Integer(this.getAge()).compareTo(new Integer(s.getAge()));
if(num == 0)
return this.getName().compareTo(s.getName());
return num;
}//现在想修改TreeMap集合中的Student中的排序规则,不能修改原来的代码就应该实现Comparator接口
class StudentCom implements Comparator<Student>{
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num == 0)
return new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
return num;
}
}
(*)加入TreeMap中的key原来有一个顺序,觉得不合适,就重新建立一个比较器
public class Test2{
public static void main(String[] args) {
//比较下面这个不太比较规则下的运行结果。
//TreeMap<Student,String> tree = new TreeMap<Student,String>();
TreeMap<Student,String> tree = new TreeMap<Student,String>(new StudentCom());
tree.put(new Student("lisi1",20),"nanjing");
tree.put(new Student("lisi2",24),"beijing");
tree.put(new Student("lisi3",27),"shanghai");
tree.put(new Student("aisi3",27),"shanghai");
tree.put(new Student("lisi4",16),"guangzhou");
Set<Map.Entry<Student,String>> set = tree.entrySet();
Iterator<Map.Entry<Student,String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Student,String> li = it.next();
Student student = li.getKey();
String add = li.getValue();
System.out.println(student.getName()+"-----"+student.getAge());
}
}
}
class Student implements Comparable<Student>{
String name;
int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
//默认的先比较年龄,再比较姓名
public int compareTo(Student s) {
int num = new Integer(this.getAge()).compareTo(new Integer(s.getAge()));
if(num == 0)
return this.getName().compareTo(s.getName());
return num;
}
}
//现在想修改TreeMap集合中的Student中的排序规则,不能修改原来的代码就应该实现Comparator接口
class StudentCom implements Comparator<Student>{
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num == 0)
return new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
return num;
}
}
二、工具类Collections
方法都是static的
(1)对List二分搜索
(2)同步加锁
(3)排序
a.对List使用自然排序,也可以自己指定比较器,使用新的比较规则
b.随机排序List
c.对集合逆序(1)自然逆序(2)指明新的比较器进行逆序
(4)交换List集合中的数据