预备知识
什么是数据结构?
组织大量的数据--增删改查
什么是集合类?
Java提供的一套现成的类,实现了常用的数据结构
Java集合框架
(Java Collection Framework):又被称为容器container是定义在java.util包下的一组接口interfaces和其实现类 classes
为什么要实现集合框架?
将多个元素 element 置于一个单元中,用于对这些元素进行快速、便捷的存储 store 、检索
retrieve 、管理 manipulate ,即平时我们俗称的增删查改 CRUD 。
笔试面试题
Java中常用集合类及对应的数据结构
ArrayList:顺序表
LinkedList:链表
TreeSet/TreeMap:二叉搜索树
HashSet/HashMap:哈希表
PriorityQueue:堆(二叉树)
接口interfaces
collection:用来管理和存储一组对象(Objects),这些对象一般称为元素
Set : 元素不能重复,背后隐含着查找/搜索的语义
SortedSet : 一组有序的不能重复的元素
List : 线性结构
Queue : 队列
Deque : 双端队列
Map:键值对(key-value-Pair),根据key来查找value
1Collection接口:
实例化时需要通过一个具体类(例如ArrayList类)来实现此接口
Collection<String> coolection=new ArrayList<>();
1.1Collection常用方法
boolean add(E e)--将元素e放入集合中
collection.add("第一个元素");
void clear()--删除集合中的所有元素
boolean isEmpty()--判断集合是否有空元素
boolean remove(Object e)--如果元素e出现在集合中,删除
int size()--返回集合中元素的个数
Object[] toArray--返回一个装有所有集合元素的数组
//使用toArray()--返回一个装有集合所有元素的数组
Object[] array=collection.toArray();
System.out.println( "装有集合所有元素的数组:"+Arrays.toString(array));
contains()--查询是否包含该元素
//使用contains()方法-----是否包含某元素
boolean ret=collection.contains("第一个元素");
System.out.println("集合是否包含该元素?(true包含,false不包含):"+ret);
代码实现:
package package1127;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class TestCollection {
public static void main(String[] args) {
//泛型语法,集合中保存的是String类型的若干个对象
//本身是ArrayList<String>类型
Collection<String> collection=new ArrayList<>();//创建Collection实例,Collection接口实现的子类
//使用isEmpty()--判断集合是否为空
// 使用size()--返回集合元素的个数
System.out.println("集合是否为空?(true为空,false不为空):"+collection.isEmpty());
System.out.println("集合中元素的个数为:"+collection.size());
//使用add("被添加的元素")--添加一个元素进入集合中
collection.add("第一个元素");
collection.add("第二个元素");
collection.add("第三个元素");
System.out.println("集合是否为空?(true为空,false不为空):"+collection.isEmpty());
System.out.println("集合中元素的个数为:"+collection.size());
//使用toArray()--返回一个装有集合所有元素的数组
Object[] array=collection.toArray();
System.out.println( "装有集合所有元素的数组:"+Arrays.toString(array));
//foreach遍历集合类
System.out.println("foreach遍历集合类:");
for(String s:collection){
System.out.println(s);
}
//使用contains()方法-----是否包含某元素
boolean ret=collection.contains("第一个元素");
System.out.println("集合是否包含该元素?(true包含,false不包含):"+ret);
//remove()---删除方法
System.out.println("删除第二个元素:");
collection.remove("第二个元素");
for(String s:collection){
System.out.println(s);
}
}
}
//输出:
/*
集合是否为空?(true为空,false不为空):true
集合中元素的个数为:0
集合是否为空?(true为空,false不为空):false
集合中元素的个数为:3
装有集合所有元素的数组:[第一个元素, 第二个元素, 第三个元素]
foreach遍历集合类:
第一个元素
第二个元素
第三个元素
集合是否包含该元素?(true包含,false不包含):true
删除第二个元素:
第一个元素
第三个元素
*/
1.2遍历集合类
foreach语法可以和很多类搭配,只要这个类实现了iterable接口,就可以使用
//foreach遍历集合类
System.out.println("foreach遍历集合类:");
for(String s:collection){
System.out.println(s);
}
2Map接口
存的是键值对
Map根据key可以映射到value但是根据value无法映射到key
2.1常用方法:
boolean isEmpty()
int size()
put(key,value)--插入键值对,put方法如果key已经存在,新的value就会覆盖掉旧的value
//使用put("key","value")--插入键值对
map.put("小酒窝","林俊杰");
get(key)--返回value(根据key查找value),key不存在就返回null,无默认value
getOrdefault(key,defaultValue)--返回value,如果不存在key时,有默认value值,为defaultValue
//使用get("key")--根据key查找value
System.out.println(map.get("王妃"));
//key不存在返回defaultValue,key存在返回value
System.out.println(map.getOrDefault("小星星","北方男人"));
package package1127;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
public static void main(String[] args) {
//Map的基本用法
Map<String,String> map=new HashMap<>();//HashMap来实现Map接口,key和value都是String类型
//使用isEnmty()和size()
System.out.println("map是否为空?(true为空,false不为空):"+map.isEmpty());
System.out.println("map中键值对的个数为:"+map.size()+"个");
//使用put("key","value")--插入键值对
map.put("小酒窝","林俊杰");
map.put("王妃","萧敬腾");
map.put("告白气球","周杰伦");
System.out.println("map是否为空?(true为空,false不为空):"+map.isEmpty());
System.out.println("map中键值对的个数为:"+map.size()+"个");
//重点!!
//使用get("key")--根据key查找value
System.out.println(map.get("王妃"));
//key不存在返回defaultValue,key存在返回value
System.out.println(map.getOrDefault("小星星","北方男人"));
}
}
//输出:
/*
map是否为空?(true为空,false不为空):true
map中键值对的个数为:0个
map是否为空?(true为空,false不为空):false
map中键值对的个数为:3个
萧敬腾
北方男人
*/
2.2遍历Map:
2.2.1通过map.entrySet()---推荐使用,容量大
//通过Entry操作获取到键值对
//使用Entry来进行遍历每个Entry就是一个条目,也就是一个键值对
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
}
2.2.2Map.keySet()--普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()){
System.out.println("key= "+ key + " value= " + map.get(key));
}
3实现classes
interface 顺序表 链表 堆 红黑树 哈希表
Set TreeSet HashSet
List ArrayList LinkedList
Queue LinkedList PriorityQueue
Deque LinkedList
Map TreeMap HashMap