s集合(Collection)
集合是一个容器 可存储不同类型的元素,但必须是引用类型,集合长度可变;集合只能存储引用类型
集合和数组区别
集合和数组区别
1)长度
数组的长度固定
集合的长度可变
2)存储数据的区别
数组:可以存储基本数据类型也可以存储引用数据类型
集合只能存储引用数据类型
3)存储元素的区别
数组:存储的元素数据类型必须一致
集合:如果没有明确<泛型>,可以存储任意的引用类型包括Object
Collection:单例集合顶层次的根接口,不能new,没有直接子实现类,通过他子接口的子实现类来实例化!
泛型<>:集合里面创建的时候,如果不带泛型可能程序安全问题;带了泛型,模拟数组创建的时候,明确了数据类型
element
Tybe
Key键
Value
基本功能:
集合转数组
例子: String[] str = i.toArray(new String[0]);
带参的toArray方法
添加元素
boolean add(E e):添加元素
删除元素
boolean remove(Object o):删除指定的元素
void clear():暴力删除:清空集合
判断功能
boolean contains(Object o):是否包含指定的元素
boolean isEmpty():判断集合是否为空
例子
package CollectionDemo1;
import java.util.ArrayList;
import java.util.Collection;
public class Test9 {
public static void main(String[] args) {
Collection<String>c=new ArrayList<>();//Collection不能直接new 通过多态实现他的子类接口
System.out.println(c);//[]
//boolean add(E e):添加元素
c.add("你好");
c.add("世界");
c.add("再见");
System.out.println(c);//[你好, 世界, 再见]
System.out.println("----------------");
//boolean remove(Object o):删除指定的元素
System.out.println(c.remove("你好"));//true
System.out.println(c);//[世界, 再见]
System.out.println("----------------");
//boolean contains(Object o):是否包含指定的元素
//boolean isEmpty():判断集合是否为空
System.out.println(c.contains("世界"));//true 因为集合确实存在"世界"这个元素
System.out.println(c.isEmpty());//false 因为集合不为空
System.out.println("----------------");
//c.clear();暴力删除
c.clear();
System.out.println(c);//[]删除了所有元素
}
}
迭代器
Iterator<E> iterator():Collection专有迭代器(遍历集合) (过渡)
Iterator迭代器接口:
boolean hasNext(): 判断迭代器里面是否下一个可以遍历的元素
E next():获取迭代器的下一个元素 (和迭代器中的类型一致)
开发中---集合遍历---->都用jdk5以后新特性:增强for循环 代替上面迭代器
Collection集合高级功能:
Object[] toArray():集合的传统方式遍历 将集合转换成对象数组(以后不会用的)
例子老师:
public class CollectionDemo2 {
public static void main(String[] args) {
//使用Collection存储String字符串并进行遍历---使用Collection迭代器
Collection<String> c = new ArrayList<>() ;
//添加String
c.add("android") ;
c.add("JavaEE") ;
c.add("mysql") ;
c.add("hadoop") ;
//c.add(100) ;
//获取Collection的迭代器
//Iterator<E> iterator()
Iterator<String> it = c.iterator();
//E next() //E---类型需要和泛型的类型一致的
/*//第一次取
Object object = it.next();
String s = (String) object;
System.out.println(s);
//第二次取
Object object2 = it.next();
System.out.println((String)object2);
//第三次取
Object object3 = it.next();
System.out.println((String)object3);*/
//第一次取
/* if(it.hasNext()){
String next = it.next();
System.out.println(next);
}
//第二次取
if(it.hasNext()){
System.out.println(it.next());
}
//第三次取
if(it.hasNext()){
System.out.println(it.next());
}
if(it.hasNext()){
//第四次取
System.out.println(it.next());
}
if(it.hasNext()){
//第五次取
System.out.println(it.next());
}*/
//while循环
while(it.hasNext()){
String s = it.next();
System.out.println(s+"---"+s.length());
}
//第五次取
//System.out.println(it.next());//NoSuchElementException:没有元素了
}
}
迭代器的原理
师说:
迭代器:帮助我们做集合遍历的工具。
Collection集合元素的通用获取方式: 在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来;继续再判断,如果还有就再取出来。一直到把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。集合中把这种取元素的方式描述在Iterator接口中。
修饰语和类型 | 方法 | 描述 |
---|---|---|
boolean | hasNext() | 判断集合中还有没有可以被取出的元素,如果有返回true |
E | next() | 取出集合中的下一个元素 |
增强for替代迭代器
Jdk5新特性:
<泛型>,增强for,可变参数,静态导入(导入到方法级别:方法必须静态)...
增强for:
是在集合/数组中存储的引用类型中使用居多,代替集合迭代器的,简化书写代码
for(集合中存储的数据类型 变量名:集合对象){
使用这个变量名即可
}
注意
要使用增强for遍历集合,集合对象不能null;
防止空指针异常,在使用增强for之前,对集合进行非空判断
Collection<Student>,使用ArrayList具体的子实现类,保证Student对象唯一!
去重(学生的成员信息相同,则是同一个人)
ArrayList集合的contains()方法依赖于Object的equals方法,底层实现的"==",比较的是地址值是否相同,下面9个学生都new出来的
地址值不相同,都添加到集合中
在ArrayList存储自定义类型,要保证元素唯一,必须重写equals和hashCode方法
例子师说:
public class Test3 {
public static void main(String[] args) {
Collection<Student> c = new ArrayList<>() ;
//创建学生对象
Student s1 = new Student("曹操",45) ;
Student s2 = new Student("周瑜",20) ;
Student s3 = new Student("刘备",35) ;
Student s4 = new Student("关羽",32) ;
Student s5 = new Student("张飞",30) ;
Student s6 = new Student("张飞",30) ;
Student s7 = new Student("高圆圆",44) ;
Student s8 = new Student("高圆圆",44) ;
Student s9 = new Student("高圆圆",35) ;
//添加
c.add(s1) ;
c.add(s2) ;
c.add(s3) ;
c.add(s4) ;
c.add(s5) ;
c.add(s6) ;
c.add(s7) ;
c.add(s8) ;
c.add(s9) ;
//新建空的集合
Collection<Student> newColl = new ArrayList<>() ;
//遍历以前的集合
for(Student s:c){
//新集合中判断,如果包含这个学生对象,就添加新集合中
if(!newColl.contains(s)){
newColl.add(s) ;
}
}
//遍历新集合
for(Student student:newColl){
System.out.println(student.getName()+"---"+student.getAge());
}
}
}
并发修改异常
需求:
有一个Collection集合,存储一些不重复的String元素,如果现在里面有"world",需要给集合中添加一个新的"JavaEE"元素!
分析:
1)创建Collection,模拟存储一些字符串,里面就包含"world"
2)获取Collection集合迭代器,遍历元素,
3)判断,如果这里面存在"world",然后添加"JavaEE"
出现了异常"ConcurrentModificationException":并发修改异常
什么叫并发:在某个时间点上同时进行!
在集合这块出现ConcurrentModificationException原因:
当使用迭代器去遍历元素的时候,同时不能使用集合对象去操作(修改或添加...)元素,就会出现这个异常!
因为,迭代器在遍历元素的时候,集合添加一个新的元素,迭代器不知道!
解决方案:
Collection没有直接操作----->子接口List特有功能(ListIterator:列表迭代器/List集合普通for+size()+get(int index))
1)集合遍历,集合添加
2)迭代器遍历,迭代器遍历
解决办法1:List的列表迭代器解决
import java.util.*;
//"ConcurrentModificationException":并发修改异常
用List的迭代器
public class Test7 {
public static void main(String[] args) {
// Collection<String>cs=new ArrayList<>();//这种 不行
List<String>ls=new ArrayList<>();
ls.add("并发");
ls.add("修改");
ls.add("异常");
ls.add("ConcurrentModificationException");
ls.add("解决办法");
ListIterator<String>is=ls.listIterator();
//使用集合迭代器添加集合元素
while(is.hasNext()){
String s=is.next();
if(s.equals("ConcurrentModificationException")){
is.add("新来的");
}
}
System.out.println(ls);
}
}
//[并发, 修改, 异常, ConcurrentModificationException, 新来的, 解决办法]
解决办法2:List中增强For解决
package ConcurrentModificationException;
import java.util.ArrayList;
import java.util.List;
public class Test8 {
public static void main(String[] args) {
// Collection<String>cs=new ArrayList<>();//这种 不行
List<String> ls = new ArrayList<>();
ls.add("并发");
ls.add("修改");
ls.add("异常");
ls.add("ConcurrentModificationException");
ls.add("解决办法");
for(int i=0;i<ls.size();i++){
String s=ls.get(i);
if(s.equals("ConcurrentModificationException")){
ls.add("New");
}
}
System.out.println(ls);
}
}
去重方法
package Test_CollectionIteratorQuChong;
//去重
import java.util.ArrayList;
import java.util.Collection;
public class Test3 {
public static void main(String[] args) {
Collection<String>S=new ArrayList<>();
S.add("hello");
S.add("hello");
S.add("big");
S.add("hello");
S.add("world");
S.add("size");
S.add("world");
Collection<String>S2= new ArrayList<>();
for (String ss : S) {
if(!S2.contains(ss)){//contains是判断是否有这个元素的方法
S2.add(ss);
}
}
for (String ss : S2) {
System.out.println(ss);
}
}
}
List集合:
特点–List集合是Collection集合的子接口,它的集合遍历方式
元素有序(存储和取出一致) 元素可以重复
1) Collection的Object[] toArray()
2)Collectionde Iterator iterator()
3)使用size()+存储的数据类型get(int index) 通过角标获取元素内容:普通迭代器
4)ListIterator<E>listIterator()List集合专有遍历方式:列表迭代器
5)增强for(推荐!)
例子:
需求:
* 三个Java班级:每一个班级看成ArrayList
* 三个班级组成一个大的集合ArrayList<ArrayList<Student>>,
* 第一个班级存储
* 高圆圆 22
* 赵又廷 25
* 第二个班级存储
* 张三丰 50
* 令狐冲 35
* 第三个班级存储
* 孙悟空 32
* 唐僧 40
*
* 去遍历大的集合
ArrayList<ArrayList<Student>>,打印学生信息
package Test4;
import java.util.ArrayList;
class Student{
private String name;
private int age;
public Student(){}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Test4 {
public static void main(String[] args) {
ArrayList<ArrayList<Student>> bigArray=new ArrayList<>();
ArrayList<Student> s = new ArrayList<>();
Student s1 = new Student("高圆圆", 22);
Student s2 = new Student("赵又廷", 25);
s.add(s1);
s.add(s2);
bigArray.add(s);
ArrayList<Student> ss = new ArrayList<>();
Student s3 = new Student("张三丰", 50);
Student s4 = new Student("令狐冲", 35);
ss.add(s3);
ss.add(s4);
bigArray.add(ss);
ArrayList<Student> sss = new ArrayList<>();
Student s5 = new Student("孙悟空", 32);
Student s6 = new Student("唐僧", 40);
sss.add(s5);
sss.add(s6);
bigArray.add(sss);
//遍历
System.out.println("学生信息是:\t");
System.out.println("姓名\t\t年龄");
for(ArrayList<Student> array:bigArray){
for(Student student:array){
System.out.println(student.getName()+" \t"+student.getAge());
}
}
}
}
遍历方式例子
public class ListDemo {
public static void main(String[] args) {
//创建List集合
List<String> list = new ArrayList<>() ;
list.add("hello") ;
list.add("world") ;
list.add("JavaEE") ;
list.add("android") ;
list.add("ios") ;
//3)4)5)特有方式
//使用size()+ 存储的数据类型 get(int index)通过角标获取元素内容 :普通for循环格式
for(int x = 0 ; x < list.size() ; x++){
//通过x角标值获取元素内容
String s = list.get(x);
System.out.println(s);
}
System.out.println("--------------------------------------------------") ;
//ListIterator<E> listIterator()List集合的专有遍历方式 :列表迭代器
ListIterator<String> lit = list.listIterator();//获取列表迭代器
//ListIterator--->接口提供一些方法
//boolean hasNext():判断是否有下个元素
//E next():获取下一个元素
while(lit.hasNext()){
String s = lit.next();
System.out.println(s);
}
System.out.println("--------------------------------");
//列表迭代器提供了一些功能,进行逆序遍历(前提必须有上面的正向遍历)
//boolean hasPrevious():判断是有上一个元素
//E previous():获取上一个元素
while(lit.hasPrevious()){
String previous = lit.previous();
System.out.println(previous);
}
System.out.println("---------------------------------------------------") ;
//增强for (推荐)
for(String s:list){
System.out.println(s) ;
}
}
}
List三个子实现类的特点
实际需求:单线程使用List完成一些事情,没有明确规定具体使用哪个实现类,默认使用ArrayList
默认ArrayList
底层数据结构是数组--查询快,增删慢!(数组,通过整数索引查询)
元素查找:顺序查找,二分搜索,哈希查找
线程角度:
线程不安全的类,不同步但是效率高!
底层扩容机制1.5倍
普普通通的遍历
import java.util.ArrayList;
public class Test6 {
public static void main(String[] args) {
ArrayList<String> as=new ArrayList<>();
as.add("这是");
as.add("ArrayList");
as.add("的例子");
as.add("遍历");
for(String s2:as){
System.out.print(s2);
}
}
}
并发修改异常的解决.
使用List集合解决并发修改异常
List有特有方式:
size()获取集合长度 结合 Object get(int index)通过索引值获取对应的内容
特有添加元素的方式:
void add(int index,E element):在指定位置处,添加指定的元素!
使用Collection<String>,如果Collection集合中存储有"world"元素,给集合中新添加一个元素--->
出现ConcurrentModificationException 出现并发修改异常的原因:使用迭代器去遍历元素的时候,不能同时集合操作元素;
解决方案:
1)集合遍历,集合添加
2)迭代器遍历,迭代器添加元素
List继承Collection--->Iterator是Collection的专有迭代器,没有提供添加功能,而
List集合专有遍历方式ListIterator:列表迭代器
列表迭代器解决
import java.util.*;
//"ConcurrentModificationException":并发修改异常
用List的迭代器
public class Test7 {
public static void main(String[] args) {
// Collection<String>cs=new ArrayList<>();//这种 不行
List<String>ls=new ArrayList<>();
ls.add("并发");
ls.add("修改");
ls.add("异常");
ls.add("ConcurrentModificationException");
ls.add("解决办法");
ListIterator<String>is=ls.listIterator();
//使用集合迭代器添加集合元素
while(is.hasNext()){
String s=is.next();
if(s.equals("ConcurrentModificationException")){
is.add("新来的");
}
}
System.out.println(ls);
//使用集合迭代器删除集合元素
while(is.hasNext()){
String s=is.next();
if(s.equals("ConcurrentModificationException")){
is.remove();
}
}
System.out.println(ls);
//使用集合迭代器修改集合元素
while(is.hasNext()){
String s=is.next();
if(s.equals("ConcurrentModificationException")){
is.remove();
is.add("修改的");
}
}
System.out.println(ls);
}
}
//[并发, 修改, 异常, ConcurrentModificationException, 新来的, 解决办法]
List增强For解决
package ConcurrentModificationException;
import java.util.ArrayList;
import java.util.List;
public class Test8 {
public static void main(String[] args) {
// Collection<String>cs=new ArrayList<>();//这种 不行
List<String> ls = new ArrayList<>();
ls.add("并发");
ls.add("修改");
ls.add("异常");
ls.add("ConcurrentModificationException");
ls.add("解决办法");
//普通for循环增加元素
for(int i=0;i<ls.size();i++){
String s=ls.get(i);
if(s.equals("ConcurrentModificationException")){
ls.add("New");
}
}
System.out.println(ls);
//普通for循环删除元素
for(int i=0;i<ls.size();i++){
String s=ls.get(i);
if(s.equals("ConcurrentModificationException")){
ls.remove(s);
i--;
}
}
//普通for循环修改元素
for(int i=0;i<ls.size();i++){
String s=ls.get(i);
if(s.equals("ConcurrentModificationException")){
ls.remove(s);
ls.add("新修改的");
}
}
}
}
Vector
底层数据结构是数组--查询快,增删慢!
线程角度:
线程安全的类,同步但是效率低!多线程环境下使用居多,单线程环境使用ArrayList
特有功能
遍历方式:Collection的Object[] toArray()
Collection的Iterator iterator()
List接口提供的 size()+get(int index)
List接口提供的ListIterator listiterator()
特有遍历方式
public Enumeration<E> elements()--->相当于集合的迭代器
Enumeration接口:枚举组件
boolean hasMoreElements()---->相当于boolean hasNext()
E nextElement()---->相当于E next()
public E elementAt(int index)--->相当于List的get(int index)+size方法相结合
增强for
添加:
public void addElement(E obj)--->相当于List的add()
public E elementAt(int index)--->相当于List的get(int index)
例子
import java.util.Enumeration;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
//创建Vector集合
Vector<String> v = new Vector<>() ;
//public void addElement(E obj)--->相当于List的add()
v.addElement("hello");
v.addElement("world");
v.addElement("javaee");
v.addElement("mysql");
// 遍历
//public E elementAt(int index)--->相当于List的get(int index)+size方法相结合
for(int x = 0 ; x < v.size() ; x++){
String s = v.elementAt(x);
System.out.println(s);
}
System.out.println("----------------------------------------") ;
/**
* public Enumeration<E> elements()--->相当于集合的迭代器
* Enumeration接口:枚举组件
* boolean hasMoreElements()---->相当于boolean hasNext()
* E nextElement() ---->相当于E next()
*/
/* for(Enumeration<String> en = v.elements(); en.hasMoreElements();){
System.out.println(en.nextElement());
}*/
Enumeration<String> en = v.elements() ;
while(en.hasMoreElements()){
String s = en.nextElement();
System.out.println(s);
}
System.out.println("-----------------------------------") ;
for(String s:v){
System.out.println(s);
}
}
}
LinkedList
底层数据是链表,查询慢,增删快
线程角度:
线程不安全!不同步,效率高!
特有功能
LinkedList的特有方法:
public void addFirst(E e) :在链表开头插入元素
public void addLast(E e) :在链表末尾插入元素
public E removeFirst(): 删除链表开头的第一个元素并返回该元素
public E removeLast()从此列表中删除并返回最后一个元素。
public E getFirst():返回链表头元素
public E getLast():返回链表链尾元素
例子
public class LinkedListDemo {
public static void main(String[] args) {
//创建LinkedList集合
LinkedList<String> link = new LinkedList<>() ;
//添加元素
link.addFirst("hello") ;
link.addFirst("world") ;
link.addFirst("javaee") ;
/* for(String s:link){
System.out.println(s);
}*/
System.out.println(link);
//删除
System.out.println(link.removeFirst()) ;
System.out.println(link);
//获取
System.out.println(link.getFirst());
System.out.println(link.getLast());
}
}
小作业
需求:
利用LinkedList模拟栈的结构特点,先进后出
自定义类--->里面使用到LinkedList的特有功能
方法add()--->linkedList的addFirst()
get()--->获取元素: LinkedList的removeFirst()删除并返回
自定义一个方法isEmpty()--->判断LinkedList是否为空
在定义一个测试类---完成上面类的功能测试
Set集合:
元素唯一的,无序(存储和取出不一致),不能保证迭代次序,但可以是唯一!
HashSet<E> : 存储和取出,保证元素唯一!
---底层是HashMap实例(哈希表)
TreeSet<E> : 存储和取出,同时需要进行排序的取出!
---底层是依赖TreeMap实例(红黑树结构)
Set的两个子实现类
HashSet
使用HashSet存储一些学生(姓名,年龄),让你保证学生对象唯一!
自定义类型必须重写HashCode和equals方法,保证自定义类的对象唯一!
例子
public class HashSetDemo {
public static void main(String[] args) {
//创建HashSet集合对象
HashSet<String> hs = new HashSet<>() ;
hs.add("hello") ;
//System.out.println("hello".hashCode());
hs.add("hello") ;
hs.add("hello") ;
hs.add("world") ;
hs.add("world") ;
hs.add("world") ;
hs.add("JavaEE") ;
hs.add("JavaEE") ;
//遍历
for(String s:hs){
System.out.println(s);
}
}
}
例子2
public class Student {
private String name ;
private int age ;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = 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 "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name.equals(student.name);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + age;
return result;
}
}
public class HashSetTest {
public static void main(String[] args) {
HashSet<Student> hs = new HashSet<>() ;
//创建学生对象
Student s1 = new Student("高圆圆",44) ;
Student s2 = new Student("高圆圆",44) ;
Student s3 = new Student("高圆圆",35) ;
Student s4 = new Student("文章",35) ;
Student s5 = new Student("文章",35) ;
Student s6 = new Student("王宝强",30) ;
Student s7 = new Student("王宝强",30) ;
//添加
hs.add(s1) ;
hs.add(s2) ;
hs.add(s3) ;
hs.add(s4) ;
hs.add(s5) ;
hs.add(s6) ;
hs.add(s7) ;
//遍历
for(Student s:hs){
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
//可以自动去重
TreeSet
两种排序方式
1自然排序
自然排序:
TreeSet<E>() 无参构造方法
public TreeSet():使用自然排序,当前里面存储的类型必须实现Comparable
要点:必须重写Comparable~!
例子
public class Student implements Comparable<Student>{
private String name ;
private int age ;
public Student() {
}
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;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写
@Override
public int compareTo(Student s) { //存储到每一个学生对象
//主要排序条件:
// 需要按照学生的年龄从小到大排序!
int num = this.age - s.age ;
//int num = s.age - this.age ;
//年龄相同,比较姓名的内容是否相同
int num2 =(num==0)?(this.name.compareTo(s.name)):num ;
return num2;
}
}
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>() ; //无参构造方法
Student s1 = new Student("gaoyuanyuan",35) ;
Student s2 = new Student("gaoyuanyuan",35) ;
Student s3 = new Student("gaoyuanyuan",25) ;
Student s4 = new Student("wenzhang",38) ;
Student s5 = new Student("mayili",45) ;
Student s6 = new Student("yaodi",20) ;
Student s7 = new Student("baoqiang",32) ;
Student s8 = new Student("mabaoguo",65) ;
Student s9 = new Student("zhangsanfeng",65) ;
ts.add(s1) ;
ts.add(s2) ;
ts.add(s3) ;
ts.add(s4) ;
ts.add(s5) ;
ts.add(s6) ;
ts.add(s7) ;
ts.add(s8) ;
ts.add(s9) ;
//遍历
for(Student s:ts){
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
//自然(自动排序):
2比较器排序
比较器排序:
创建TreeSet对象的时候传递Comparator的实现类对象,重写compare方法,根据返回值进行排序。
在使用的时候,默认使用自然排序,当自然排序不满足现在的需求时,使用比较器排序
Map集合 <K, V>两个子实现类
将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值
Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构值针对键有效,跟值无关
Collection集合的数据结构是针对元素有效
常用功能
V put(K key,V value) 添加键值对
V remove(Object key) 删除Map的键,返回被删除键对应的值
void clear() 清空Map
boolean containsKey(Object key) 判断是否包含指定的键
boolean containsValue(Object value) 判断是否包含指定的值
boolean isEmpty() 判断map是否为空
int size() 获取map的键值对元素
遍历功能
方式1:根据键找值
获取所有键的集合
遍历键的集合,获取到每一个键
根据键找值
方式2:
根据键值对对象找键和值
获取所有键值对对象的集合
遍历键值对对象的集合,获取到每一个键值对对象
根据键值对对象找键和值
1:HashMap<K, V>
例子
2:TreeMap<K, V>
例子