集合框架
1、集合的概念
1.1 什么是集合
对象的容器,实现了对 对象常用的操作,类似数组功能
1.2 集合与数组的区别
- 数组的长度固定,集合长度不固定
- 数组可以存储基本类型和引用类型,集合只能存储引用类型
2、Collection接口
2.1 体系图
2.2 Collection接口使用
- 特点:代表一组任意类型的对象,无序、无下标、不能重复
- 方法
1.boolean add(Object obj) //添加一个对象
2.boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中
3.void clear() //清空此集合中的所有对象
4.boolean contains(Object obj) //检查此集合中是否包含obj对象
5.boolean equals(Object obj) //比较此集合是否与指定对象相等
6.boolean isEmpty() //判断此集合是否为空
7.boolean remove(Object o) //在此集合移除o对象
8.int size() //返回此集合中的元素个数
9.Object[] toArray() //将此集合转换为数组
保存字符串方式
//Collection
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList();
//添加元素
collection.add("苹果");
collection.add("西瓜");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
//删除元素
// collection.remove("西瓜");
// collection.clear();清空
System.out.println(collection);
//遍历元素
//1.增强for循环
for (Object o:collection){
System.out.println(o);
}
//2.迭代器(专门用来遍历集合的方式)
//hasNext()判断是否有没有下一个元素
// next()获取下一个元素
// remove()删除当前元素
Iterator it = collection.iterator();
while (it.hasNext()){
String object = (String)it.next();
System.out.println(object);
// collection.remove(object);迭代器中不允许使用collection的remove方法
// it.remove();使用此方式可行
}
System.out.println(collection);
//判断
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
}
保存对象方式
public static void main(String[] args) {
Collection collection = new ArrayList();
Student s1 = new Student("马超",18);
Student s2 = new Student("关羽",25);
Student s3 = new Student("刘备",18);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println(collection.toString());
collection.remove(s3);
//clear() 只是将集合中的数据删除了,并没有删除对象
// collection.clear();
System.out.println(collection.toString());
for (Object o :collection) {
Student s = (Student) o;
System.out.println(s.toString());
}
System.out.println(collection.contains(s1));
}
3、List接口与实现类
3.1 List接口
- 特点:有序、有下标、元素可以重复
- 在List集合中添加数字类型数据时,自动装箱
- 内置方法实现
//List使用
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList<>();
//添加元素
list.add("苹果");
list.add("香蕉");
//可以指定位置添加值
list.add(1,"西瓜");
list.add(3,"榴莲");
System.out.println(list.toString());
//删除元素
// list.remove("苹果");
// list.remove(1);
System.out.println(list.toString());
//遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//迭代器
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//列表迭代器,和Iterator的区别
//listIterator可以向前或向后遍历、添加、删除、修改操作
ListIterator lit = list.listIterator();
//从前往后
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
//从后往前
while (lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//判断
System.out.println(list.contains(1));
System.out.println(list.isEmpty());
//获取元素位置
System.out.println(list.indexOf("榴莲"));
//返回子集合
List subList = list.subList(1,3);
System.out.println(subList.toString());
}
3.2 List实现类
-
ArrayList
1.数组结构实现,查询快、增删慢
2.JDK1.2版本,运行效率快、线程不安全 -
Vector
1.数组结构实现,查询快、增删慢
2.JDK1.0版本,运行效率慢、线程安全 -
LinkedList
1.链表结构实现,增删快,查询慢
ArrayList使用
默认容量大小为10
elementData:存放元素的数组
//ArrayList
public static void main(String[] args) {
//创建集合
ArrayList arrayList = new ArrayList<>();
//添加元素
Student s1 = new Student("马超",19);
Student s2 = new Student("关羽",26);
Student s3 = new Student("刘备",17);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println(arrayList.toString());
//删除元素
// arrayList.remove(s1);
// System.out.println(arrayList.toString());
//遍历
//迭代器
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
Student s = (Student)iterator.next();
System.out.println(s.toString());
}
//列表迭代器
ListIterator lit = arrayList.listIterator();
while (lit.hasPrevious()){
Student s = (Student)lit.previous();
System.out.println(s.toString());
}
//判断
System.out.println(arrayList.contains(s1));
//查找
System.out.println(arrayList.indexOf(s2));
}
Vector 使用与 LinkedList使用基本同上
3.3 ArrayList与LinkedList区别
4、泛型和工具类
4.1 泛型概述
- java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
- 常见形式有泛型类,泛型接口,泛型方法
- 语法
<T,…> T称为类型占位符,表示一种引用类型 - 好处
1.提高代码的重用性
2.防止类型转换异常,提高代码安全性
4.2 泛型类
定义泛型类
//泛型类
public class MyGeneric<T> {
//使用泛型T
//创建变量
T t;
//添加方法
public void show(T t){
System.out.println(t);
}
//泛型作为方法返回值
public T getT(){
return t;
}
}
main中调用
public static void main(String[] args) {
//使用自定义的泛型类
MyGeneric<String> myGeneric = new MyGeneric<String>();
myGeneric.t = "hello1";
myGeneric.show("hello");
String s = myGeneric.getT();
System.out.println(s);
}
4.3 泛型接口
//泛型接口
public interface MyInter<T> {
String name = "张三";
//不能泛型静态常量
T server(T t);
}
通过实现类来确定泛型(T)的类型
public class MyInterImpl implements MyInter<String>{
@Override
public String server(String s) {
System.out.println(s);
return s;
}
}
4.4 泛型方法
public class MyGenMethod {
public void show(){
System.out.println("普通方法");
}
// 泛型方法
public <T> void show1(T t){
System.out.println("泛型方法");
}
}
4.5 泛型集合
- 概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致
- 特点
1.编译时即可检查,而非运行时抛出异常
2.访问时,不必类型转换(拆箱)
3.不同泛型之间引用不能相互赋值,泛型不存在多态
public static void main(String[] args) {
//创建集合对象时确定了元素类型
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
for (Object o : arrayList){
System.out.println(o);
}
ArrayList<Student> arrayList1 = new ArrayList<>();
Student s1 = new Student("马超",18);
Student s2 = new Student("关羽",25);
Student s3 = new Student("刘备",18);
arrayList1.add(s1);
arrayList1.add(s2);
arrayList1.add(s3);
Iterator<Student> s = arrayList1.iterator();
while (s.hasNext()){
Student sn = s.next();
System.out.println(sn.toString());
}
}
5、Set接口与实现类
5.1 set接口介绍
- 特点:无序、无下标、元素不可重复
- 方法:全部继承子Collection中的方法
5.2 set接口实现
public static void main(String[] args) {
//创建集合
Set<String> set = new HashSet<>();
//添加数据
set.add("橘子");
set.add("苹果");
set.add("香蕉");
set.add("香蕉");
System.out.println(set.toString());
//删除数据
set.remove("香蕉");
System.out.println(set.toString());
//遍历
for(String s :set){
System.out.println(s);
}
//判断
System.out.println(set.contains("苹果"));
}
5.3 set实现类
- HashSet
1.基于HashCode实现元素不重复
2.当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
public static void main(String[] args) {
//HashSet集合
//存储结构:哈希表(数组+链表+红黑树)
HashSet<String> hashSet = new HashSet<>();
//添加元素
hashSet.add("刘德华");
hashSet.add("张国伟");
hashSet.add("周润发");
hashSet.add("吴亦凡");
System.out.println(hashSet.toString());
//删除数据
hashSet.remove("张国伟");
System.out.println(hashSet.toString());
//遍历
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(hashSet.contains("吴亦凡"));
}
- TreeSet
1.基于排列顺序实现元素不重复
2.实现了SortedSet接口,对集合元素自动排序
3.元素对象的类型必须实现Comparable接口,指定排序规则
4.通过CompareTo方法确定是否为重复元素
//TreeSet的使用(红黑树)
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>();
//添加元素
treeSet.add("xyz");
treeSet.add("abc");
treeSet.add("hello");
//输出排序后的结果
System.out.println(treeSet.toString());//[abc, hello, xyz]
//删除
treeSet.remove("abc");
System.out.println(treeSet.toString());
//遍历
for (String s : treeSet) {
System.out.println(s);
}
//判断
System.out.println(treeSet.contains("xyz"));
}
- comparator接口实现
//comparator:实现定制比较(比较器)
public static void main(String[] args) {
TreeSet<Student> s = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1 = o1.getAge() - o2.getAge();
int n2 = o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
Student s1 = new Student("xyz",20);
Student s2 = new Student("hello",22);
Student s3 = new Student("zhangsan",25);
Student s4 = new Student("lisi",25);
s.add(s1);
s.add(s2);
s.add(s3);
s.add(s4);
System.out.println(s.toString());
}
6、Map集合
6.1 Map父接口
- 特点:存储一对数据(Key-Value),无序、无下标,键不可重复,值可重复
- 常见使用方法实现
6.2 实现类HashMap
- HashMap刚创建时,table是null,为了节省空间,当添加第一个元素时,table容量调整为16
- 当元素个数大于阈值(16*0.75=12)时,会进行扩容,扩容后大小为原来的2倍。目的是减少调整元素的个数。
- jdk1.8 当每个链表长度大于8,并且元素个数大于等于64时,会调整为红黑树,目的提高效率
- jdk1.8 当链表长度小于6时,调整成链表
- jdk1.8以前,链表时头插入,jdk1.8以后是尾插入
常用方法及实现均与前部分代码相同,注意灵活变通