Day13-Java集合框架
Java集合框架
概念:对象的容器,定义了对对象常用的操作,可实现数组的功能
和数组的区别:
- 数组长度固定,集合长度不固定
- 数组存储基础和引用类型,集合只能存储引用类型

Collection
根接口,代表一组任意类型的对象,部分类似List,部分类似Set
Collection.sort(x'x)
对xx集合进行一个排序

1.基础使用方式
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
public class Main {
public static void main(String[] args) throws Exception {
Collection collection = new ArrayList(); //接口无法实例化,所以实例化collection的实现类
System.out.println(collection.size());
System.out.println(collection);
System.out.println("----------------------------");
//1.增加集合元素
collection.add("苹果");
collection.add("香蕉");
collection.add("橘子");
System.out.println(collection.size());
System.out.println(collection);
System.out.println("----------------------------");
//2.删除集合元素
collection.remove("香蕉");
//collection.clear(); 清空元素
System.out.println(collection.size());
System.out.println(collection);
System.out.println("----------------------------");
//3.遍历元素
//3.1增强型for循环
for(Object arr:collection){
System.out.println(arr);
}
System.out.println("----------------------------");
//3.2iterator接口
Iterator iterator = collection.iterator();
while (iterator.hasNext()){ //hasNext()判断是否还有下一个元素
String s = (String)iterator.next(); //强制转型 next()获取元素
//iterator.remove(s); 删除元素
}
System.out.println(collection.size());
System.out.println(collection);
System.out.println("----------------------------");
//4.判断
System.out.println(collection.contains("苹果"));
}
}
2.添加对象作为元素
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
public class Main {
public static void main(String[] args) throws Exception {
Collection collection1 = new ArrayList();
Student s1 = new Student("陈琛琛",18);
Student s2 = new Student("李四",18);
Student s3 = new Student("王五",18);
Student s4 = new Student("人",18);
//1.增加集合元素
collection1.add(s1);
collection1.add(s2);
collection1.add(s3);
collection1.add(s4);
collection1.add(s4);
System.out.println(collection1.size());
System.out.println(collection1.toString());
System.out.println("----------------------------");
//2.删除集合元素
collection1.remove(s1);
//collection.clear(); 清空元素
System.out.println(collection1.size());
System.out.println(collection1);
System.out.println("----------------------------");
//3.遍历元素
//3.1增强型for循环
for(Object arr:collection1){
Student student = (Student)arr;
System.out.println(student);
}
System.out.println("----------------------------");
//3.2iterator接口
Iterator iterator1 = collection1.iterator();
while (iterator1.hasNext()){ //hasNext()判断是否还有下一个元素
Student s = (Student) iterator1.next(); //强制转型 next()获取元素
//iterator.remove(s); 删除元素
}
System.out.println(collection1.size());
System.out.println(collection1);
System.out.println("----------------------------");
//4.判断
System.out.println(collection1.contains(s1));
}
}
List
List接口继承collection接口,允许重复,维护元素插入顺序
1.基础应用
import java.util.*;
public class ListT {
public static void main(String[] args) {
List list = new ArrayList();
//1.增加元素
list.add("小米");
list.add("华为");
list.add("苹果");
list.add("小米");
list.add("华为");
list.add("苹果");
System.out.println(list.size());
System.out.println(list.toString());
System.out.println("------------------------------");
//2.删除元素
list.remove("苹果"); //只会删除一个 removeAll才是删除全部
list.remove(0);
System.out.println(list.size());
System.out.println(list.toString());
System.out.println("------------------------------");
//3.遍历
//3.1for循环
for (int i = 0; i <list.size() ; i++) {
System.out.println(list.get(i)); //get获取当前下标元素
}
System.out.println("------------------------------");
//3.2增强型for循环
for(Object obj:list){
System.out.println(obj);
}
System.out.println("------------------------------");
//3.3迭代器
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("------------------------------");
//3.4列表迭代器
ListIterator lit = list.listIterator();
while (lit.hasNext()){
System.out.println(lit.next());
}
System.out.println("------------------------------");
while (lit.hasPrevious()){
System.out.println(lit.previous());
}
System.out.println("------------------------------");
//4.判断
System.out.println(list.contains("苹果"));
//5.获取位置
System.out.println(list.indexOf("华为"));
}
}
2.存储引用对象
同collection
注意点
-
add的数据为基本数据类型时,自动装箱
-
remove基本数据类型时,应当将其转化为object或者相对应的引用类型
-
List s = list.subList(1,3); //s为list中下标为1-2的集合
有序、有下标、元素可重复
常用实现类
Array List(重点)
数据结构实现,查询快,增删慢、线程不安全(如有1000个元素,在第一个的位置增加一个元素,则要移动全部元素)
//不同点
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
ListIterator lit = list.listIterator();
while (lit.hasNext()){
System.out.println(lit.next());
}
System.out.println("------------------------------");
while (lit.hasPrevious()){
System.out.println(lit.previous());
}
System.out.println("------------------------------");
重点:
- 初始容量为0
- 添加一个元素容量为10
- 从第十一个元素时,需要扩容,每次扩容为1.5倍
Vector
数据结构实现,查询快,增删慢、线程安全
//不同点
Enumeration en = vector.elements();
while(en.hasMoreElements()){
System.out.println(en.nextElement());
}
Linked List
(双向链表)链表结构实现,增删快,查询慢(如要查询最后一个元素,单链表需要遍历全部后才找到)
import java.util.*;
public class LinkedListT {
public static void main(String[] args) throws Exception {
LinkedList linkedList = new LinkedList(); //接口无法实例化,所以实例化collection的实现类
System.out.println(linkedList.size());
System.out.println(linkedList);
System.out.println("----------------------------");
//1.增加集合元素
linkedList.add("苹果");
linkedList.add("香蕉");
linkedList.add("橘子");
System.out.println(linkedList.size());
System.out.println(linkedList);
System.out.println("----------------------------");
//2.删除集合元素
linkedList.remove("香蕉");
//collection.clear(); 清空元素
System.out.println(linkedList.size());
System.out.println(linkedList);
System.out.println("----------------------------");
//3.遍历元素
//3.1增强型for循环
for(Object arr:linkedList){
System.out.println(arr);
}
System.out.println("----------------------------");
//3.2iterator接口
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){ //hasNext()判断是否还有下一个元素
String s = (String)iterator.next(); //强制转型 next()获取元素
//iterator.remove(s); 删除元素
}
ListIterator it = linkedList.listIterator();
while (it.hasPrevious()){
System.out.println(it.previous());
}
System.out.println(linkedList.size());
System.out.println(linkedList);
System.out.println("----------------------------");
//4.判断
System.out.println(linkedList.contains("苹果"));
}
}
Set
Set接口继承collection接口,集合元素不重复
无序:添加数据的顺序和打印的顺序无关
不重复:元素重复添加不实现
package Set;
import java.util.*;
public class SetT {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
//1.增加元素
set.add("小米");
set.add("华为");
set.add("苹果");
System.out.println(set.toString()); //输出顺序与增加顺序无关
System.out.println("-------------------------");
//2.删除元素
set.remove("华为"); //因为无序,所以无法使用下标删除
System.out.println(set.toString());
System.out.println("-------------------------");
//3.遍历
//3.1增强型for循环
for(String s:set){
System.out.println(s);
}
System.out.println("-------------------------");
//3.2迭代器
Iterator it = set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("-------------------------");
//4.查询
System.out.println(set.contains("小米"));
}
}
Set实现类
HashSet
- 基于HashCode计算元素存放位置
- 当存入元素的哈希码相同时,将会调用equals方法,确认为True,无法存入
package Set;
import java.util.*;
public class HashSetT {
public static void main(String[] args) {
HashSet<Student> hashSet = new HashSet<>();
Student s1 = new Student("陈琛琛",18);
Student s2 = new Student("李四",18);
Student s3 = new Student("王五",18);
Student s4 = new Student("人",18);
//1.增加元素
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
hashSet.add(s4);
hashSet.add(s4); //无法添加进去,因为s4存放的引用,再次添加,引用相同,会当成是上一行代码的重复
hashSet.add(new Student("人",18)); //新建了一个对象,引用不同,所以可以存储进去
//可以通过重写equals方法,来进行判断,当值相同时不添加进去,可以实现针对性添加元素
System.out.println(hashSet.toString());
System.out.println("----------------");
//2.删除元素
hashSet.remove(s1);
hashSet.remove(new Student("人",18)); //重写后可以实现删除
//3.遍历
//3.1增强for
for(Student s:hashSet){
System.out.println(s);
}
System.out.println("----------------");
//3.2迭代器
Iterator it = hashSet.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("----------------");
//4.判断
System.out.println(hashSet.contains("小米"));
}
}
注意点:
- 重写中使用了31,31是质数,减少哈希冲突
- 31提高了效率,31*i可以当成i左移5位-i
TreeSet
- 基于排列顺序实现元素不重复
- 实现了SortedSet接口,对集合元素自动排序
- 元素对象的类型必须实现Comparable接口,指定排序规则
- 通过CompareTo方法确定是否为重复元素
package Set;
import java.util.*;
public class TreeSetTT {
public static void main(String[] args) {
TreeSet<Student> treeSet = new TreeSet<>(); //需要告诉红黑树比较的对象,此时先按姓名,再按年龄比
Student s1 = new Student("陈琛琛",18);
Student s2 = new Student("李四",18);
Student s3 = new Student("王五",18);
Student s4 = new Student("人",18);
//1.增加元素
treeSet.add(s1);
treeSet.add(s2);
treeSet.add(s3);
treeSet.add(s4);
System.out.println(treeSet.toString());
System.out.println("-----------------------------------");
//2.删除元素
treeSet.remove(s1);
System.out.println(treeSet.toString());
System.out.println("-----------------------------------");
//3.遍历
//3.1增强型for循环
for(Student a : treeSet){
System.out.println(a);
}
System.out.println("-----------------------------------");
//3.2迭代器
Iterator<Student> it = treeSet.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("-----------------------------------");
//4.比较
System.out.println(treeSet.contains(s1));
}
}
定制比较器
package Set;
import java.util.*;
public class TreeSetTT {
public static void main(String[] args) {
//定制比较器
TreeSet<Student> treeSet1 = 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;
}
});
//1.增加元素
treeSet1.add(s1);
treeSet1.add(s2);
treeSet1.add(s3);
treeSet1.add(s4);
System.out.println(treeSet1.toString());
}
}
Collections工具类
集合工具类,定义了除了存取外的方法
sort
Collections.sort(xx);
shuffle()
Collections.shuffle(xx);
binarySearch
Collections.binarySearch(数组,想查询的内容);
reverse 反转
Collections.reverse(xx);
自由转换
集合转换为数组
Integer[] arr = list.toArray(new Integer[n]);
数组转化为集合
String[] name = {"张三","李四","王五"}
List<String> list = Arrays.asList(name);
注意点
-
n小于集合长度,则数组长度为集合长度,n大于集合长度,数组长度为集合长度
-
由于设计问题,转换后的集合无法进行删除与添加数据
-
数组转换为集合时,应当提前装箱
Map
特点:
1.用于存储任意键值对(key-value)
2.键:无序、无下标、不允许重复
3.值:无序、无下标、允许重复
package Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapT {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
//1.添加元素
map.put("cn","zhongguo");
map.put("usa","美国");
map.put("eu","欧洲");
map.put("cn","中国"); //当键相同时,将会更新value值
System.out.println(map.toString());
System.out.println("---------------------------------------");
//2.删除元素
map.remove("eu"); //键为唯一的,所以通过键来删除对应的键值对
System.out.println(map.toString());
System.out.println("---------------------------------------");
//3.遍历
//3.1使用keySet方法
Set<String> stringSet = map.keySet(); //可以得到一个Set集合,集合中只有key
for(String key:stringSet){ //可以直接写(String key : map.keySet())
System.out.println(key+":"+map.get(key)); //通过get方法,输出对应key的value
}
System.out.println("---------------------------------------");
//3.2使用entrySet()方法
Set<Map.Entry<String,String>> entries = map.entrySet(); //效率更高,set集合中保存的是Map.Entry映射对
for(Map.Entry<String,String> entry : entries){ //可以直接写(Map.Entry<String,String> entry : map.entrySet())
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("---------------------------------------");
//4.判断
System.out.println(map.containsKey("cs"));
System.out.println(map.containsValue("中国"));
}
}
常用类的实现
加载因子:如0.75,即当容量存储达到75%时,容器开始扩容
HashMap
默认容量为16,加载因子为0.75,可以使用空的键值
- 刚创建的hashMap为空,节省空间
- 添加第一个元素,容量变成16
- 元素个数超过阈值(16*0.75),扩容后原来的2倍,减少调整元素个数
- 插入元素为尾插入
- 当数组长度超过64,链表长度超过8,链表则会变成红黑树的形式
package Map;
import java.util.HashMap;
import java.util.Map;
/**
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class HashMapT {
public static void main(String[] args) {
HashMap<Student,String> hashMap = new HashMap<>();
Student s1 = new Student("陈琛琛",18);
Student s2 = new Student("李四",18);
Student s3 = new Student("王五",18);
Student s4 = new Student("人",18);
//1.添加元素
hashMap.put(s1,"广州");
hashMap.put(s2,"北京");
hashMap.put(s3,"深圳");
hashMap.put(s4,"上海");
hashMap.put(new Student("人",18),"特殊"); //如果不重写hashcode和equals方法,可以添加
System.out.println(hashMap.toString());
System.out.println("-----------------------------------------------------------------------------------------------------------");
//2.删除元素
hashMap.remove(s4);
System.out.println(hashMap.toString());
System.out.println("-----------------------------------------------------------------------------------------------------------");
//3.遍历
//3.1keySet
for(Student s : hashMap.keySet()){
System.out.println(s.toString()+":"+hashMap.get(s));
}
System.out.println("-----------------------------------------------------------------------------------------------------------");
//3.1entry
for(Map.Entry<Student,String> s : hashMap.entrySet()){
System.out.println(s.getKey()+":"+s.getValue());
}
//4.判断
System.out.println(hashMap.containsKey(s1));
System.out.println(hashMap.containsValue("特殊"));
}
}
HashTable
线程安全,运行速率较慢,不允许使用空的键值,基本不用
TreeMap
实现了SortMap接口(Map子接口),可以对key自动排序
要求元素实现Comparable接口
TreeSet实际上是使用了TreeMap
package Map;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapT {
public static void main(String[] args) {
TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
Student s1 = new Student("陈琛琛",18);
Student s2 = new Student("李四",17);
Student s3 = new Student("王五",15);
Student s4 = new Student("人",19);
//1.增加元素
treeMap.put(s1,"上海");
treeMap.put(s2,"上海");
treeMap.put(s3,"上海");
treeMap.put(s4,"上海");
//2.删除元素
treeMap.remove(s1);
//3.遍历
for(Student s : treeMap.keySet()){
System.out.println(s.toString()+":"+treeMap.get(s));
}
System.out.println("------------------------------------------------");
for(Map.Entry<Student,String> s : treeMap.entrySet()){
System.out.println(s.getKey()+":"+s.getValue());
}
System.out.println("------------------------------------------------");
//4.判断
System.out.println(treeMap.containsKey(s1));
}
}

浙公网安备 33010602011771号