279/280Map集合遍历键找值方式和HashMap储存自定义类型键值
2022/7/7 23:20:19
本文主要是介绍279/280Map集合遍历键找值方式和HashMap储存自定义类型键值,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Map集合遍历键值对方式
Map集合遍历的第二种方式:使用Entry对象遍历
Map集合中的方法:
Set<Map.Entry<K,V>> entrySet()返回此映射中包含的映射关系的 Set 视图。
实现步骤:
1.使用Map集合中的方法entrySet( ),把Map集合中多个Entry对象取出来,存储到一个set集合中
2.遍历set集合,获取每一个Entry对象
3.使用Entry对象中的方法getKey ()和getvalue()获取键与值
public static void main(String[] args) { //创建Map集合对象 Map<String ,Integer> map = new HashMap<>(); map.put("梁志超",168); map.put("卢本伟",165); map.put("六舅",178); //1.使用Map集合中的方法entrySet(),把Nap集合中多个Entry对象取出来,存储到一个set集合中 Set<Map.Entry<String,Integer>> set = map.entrySet(); //72.遍历set集合,获取每一个Entry对象 //使用迭代器遍历set集合 Iterator<Map.Entry<String,Integer>> it = set.iterator( ); while(it.hasNext()) { Map.Entry<String, Integer> entry = it.next(); //3.使用Entry对象中的方法getKey()和getvalue( )获取键与值 String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key + "=" + value); } }
//Map集合遍历键值对方式 public class MapDemo2 { public static void main(String[] args) { //创建Map集合对象 HashMap<Integer, String> map = new HashMap<Integer, String>(); //添加元素 map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); map.put(4, "d"); //获取键值对集 Set<Entry<Integer, String>> entrys = map.entrySet(); //增强for方式 遍历键值对集 得到每一个键值对 for(Entry<Integer, String> entry : entrys) { //通过键值对获取键 Integer key = entry.getKey(); //通过键值对获取值 String value = entry.getValue(); System.out.println(key + ":" + value); } Iterator it = entrys.iterator(); //迭代器方式 遍历键值对对象集 得到每一个键值对对象 while(it.hasNext()) { //通过键值对对象获取键值对 Entry<Integer, String> entry = (Entry<Integer, String>)it.next(); //通过键值对获取键 Integer key = entry.getKey(); //通过键值对获取值 String value = entry.getValue(); System.out.println(key + "=" + value); } } }
HashMap存储自定义类型键值
练习∶每位学生(姓名,年龄)都有自己的家庭住址。那么,既然有对应关系,则将学生对象和家庭住址存储到map集合中。学生作为键,家庭住址作为值。
HashMap存储自定义类型键值key : Herson类型
person类就必须重hashcode方法和equals方法,以保证key唯一value:String类型
可以重复
public static void main(String[] args) { // showe1(); show2(); } /* HashMap存储自定义类型键值key : String类型 String类重写hashcode方法和equals方法,可以保证key唯一value :Person类型 vatue可以重复(同名同年龄的人视为同一个) */ private static void showe1() { //创建HashMap集合 HashMap<String,Person> map = new HashMap<>(); //往集合中添加元素 map.put("河北", new Person("张三", 18)); map.put("北京" , new Person("李四", 19)); map.put("上海" , new Person("王五", 20)); map.put("南京" ,new Person("赵六", 18)); //使用keyset加增强for遍历Map集合 Set<String> set = map. keySet(); for (String key : set){ Person value = map.get(key ) ; System.out.println( key+"-- >"+value); } } private static void show2(){ //创建HashMap集合 HashMap<Person, String> map = new HashMap<>(); //往集合中添加元素 map.put(new Person("赛亚人",20),"超级塞亚人"); map.put(new Person("六舅",20),"豆腐乳"); map.put(new Person("乐迪",20),"乐迪加速"); map.put(new Person("卢本伟",20),"伞兵一号卢本伟"); //使用entrySet和增强for遍历Map集合 Set<Map.Entry<Person, String>> entries = map.entrySet(); for (Map.Entry<Person,String> entry: entries){ Person key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"-->"+value); } }
这篇关于279/280Map集合遍历键找值方式和HashMap储存自定义类型键值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?