COPY/**
* HashMap的使用
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class Demo2 {
public static void main(String[] args) {
HashMap<Student, String> hashMap=new HashMap<Student, String>();
Student s1=new Student("tang", 36);
Student s2=new Student("yu", 101);
Student s3=new Student("he", 10);
//1.添加元素
hashMap.put(s1, "成都");
hashMap.put(s2, "杭州");
hashMap.put(s3, "郑州");
//添加失败,但会更新值
hashMap.put(s3,"上海");
//添加成功,不过两个属性一模一样;
//注:假如相同属性便认为是同一个对象,怎么修改?
hashMap.put(new Student("he", 10),"上海");
System.out.println(hashMap.toString());
//2.删除元素
hashMap.remove(s3);
System.out.println(hashMap.toString());
//3.遍历
//3.1 使用keySet()遍历
for (Student key : hashMap.keySet()) {
System.out.println(key+" "+hashMap.get(key));
}
//3.2 使用entrySet()遍历
for (Entry<Student, String> entry : hashMap.entrySet()) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
//4.判断
//注:同上
System.out.println(hashMap.containsKey(new Student("he", 10)));
System.out.println(hashMap.containsValue("成都"));
}
}
Map的两种遍历方法
原创
©著作权归作者所有:来自51CTO博客作者Frank___7的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SpringBoot优雅捕捉异常的两种方法
SpringBoot优雅捕捉异常的两种方法
SpringBoot 异常处理 @ControllerAdvice @ExceptionHandler -
java map的两种遍历方式
1.1. 通过key得到value//得到所有的key值 Set keySet = map.keySet(); //根据key值得到value值 for (String k }
javspan idtransmarks java System -
下载mobilenet预训练权重
目录1、人脸识别基本过程2、人脸检测和人脸对齐3、人脸表征4、人脸匹配5、训练参数介绍6、效果展示 7、源码地址 8、总结1、人脸识别基本过程 人脸识别的主要过程分为四部分: &
下载mobilenet预训练权重 人工智能 深度学习 神经网络 目标检测 -
JavaScript编程工具有哪些?老前端的实用工具清单与经验分享
本文介绍常用 JavaScript 编程工具,包括 VS Code、WebStorm、Vite、Webpack、Chrome DevTools
#前端 #javascript #开发语言 #ios #小程序
















