Java案例之HashMap集合存儲學(xué)生對象并遍歷
一、需求:創(chuàng)建一個HashMap集合,鍵是學(xué)號(String),值是學(xué)生對象(Student),存儲三個鍵值對元素,并遍歷
分析:
- 1.定義學(xué)生類
- 2.創(chuàng)建
HashMap集合對象 - 3.創(chuàng)建學(xué)生對象
- 4把學(xué)生添加到集合中
- 5.遍歷集合
public class StudentDemo {
? public static void main(String[] args) {
? ? ? //創(chuàng)建Map集合對象
? ? ? Map<String,Student> m=new HashMap<String,Student>();
? ? ? //添加鍵值對
? ? ? m.put("01",new Student("張三"));
? ? ? m.put("04",new Student("趙六"));
? ? ? m.put("02",new Student("李四"));
? ? ? m.put("03",new Student("王五"));
? ? ? //遍歷集合
? ? ? Set<Map.Entry<String,Student>> s= m.entrySet();
? ? ? //遍歷
? ? ? for (Map.Entry<String,Student> ss:s){
? ? ? ? ? //根據(jù)鍵值對對象獲取值和key
? ? ? ? ? String key=ss.getKey();
? ? ? ? ? Student value=ss.getValue();
? ? ? ? ? System.out.println(key+","+value.getName());
? ? ? }
? ? ? System.out.println("------------------------");
? ? ? //方式二,通過鍵找值
? ? ? Set<String> m1=m.keySet();
? ? ? for (String key :m1){
? ? ? ? ? ? Student student =m.get(key);
? ? ? ? ? System.out.println(key+","+student.getName());
? ? ? }
? }
}二、需求:創(chuàng)建一個HashMap集合,鍵是學(xué)生對象(Student),值是地址(String),存儲三個鍵值對元素,并遍歷分析:
- 1.定義學(xué)生類
- 2.創(chuàng)建
HashMap集合對象 - 3.創(chuàng)建學(xué)生對象,并把學(xué)生對象當(dāng)作鍵值添加到集合
- 4把地址字符串添加到集合中
- 5.為了保證數(shù)據(jù)的唯一性,需要在學(xué)生類中重寫
hashCode及equals方法 - 6.遍歷集合
public class StudentDemo {
? public static void main(String[] args) {
? ? ? //創(chuàng)建集合對象
? ? ? Map<Student,String> m=new HashMap<Student,String>();
? ? ? //添加鍵值對
? ? ? m.put(new Student("張三",18),"上海");
? ? ? m.put(new Student("李四",19),"北京");
? ? ? m.put(new Student("王五",20),"上海");
? ? ? m.put(new Student("王五",20),"海南");
? ? ? //方式一
? ? ? //獲取所有鍵值對的集合
? ? ? Set<Map.Entry<Student,String>> s=m.entrySet();
? ? ? //方式一、遍歷
? ? ? for (Map.Entry<Student,String> mm:s){
? ? ? ? ? //通過鍵值對獲取對應(yīng)的值與鍵
? ? ? ? ? Student key=mm.getKey();
? ? ? ? ? String value=mm.getValue();
? ? ? ? ? System.out.println(key.getName()+","+key.getAge()+value);
? ? ? }
? ? ? System.out.println("---------------------------------");
? ? ? //方式二
? ? ? Set<Student> key=m.keySet();
? ? ? for (Student s1:key){
? ? ? ? ? String value=m.get(s1);
? ? ? ? ? System.out.println(s1.getName()+","+s1.getAge()+","+value);
? ? ? }
? }
}到此這篇關(guān)于Java案例之HashMap集合存儲學(xué)生對象并遍歷的文章就介紹到這了,更多相關(guān)HashMap存儲對象并遍歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis在sqlite中無法讀寫byte[]類問題的解決辦法
這篇文章主要給大家介紹了關(guān)于Mybatis在sqlite中無法讀寫byte[]類問題的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java虛擬機內(nèi)存分配與回收策略問題精細(xì)解讀
Java技術(shù)體系中所提倡的自動內(nèi)存管理最終可以歸結(jié)為自動化地解決了兩個問題:給對象分配內(nèi)存以及回收分配給對象的內(nèi)存,本文讓我們來詳細(xì)了解2021-11-11
在安卓系統(tǒng)中插入表情到光標(biāo)位置的代碼詳解
這篇文章主要介紹了在安卓系統(tǒng)中插入表情到光標(biāo)位置的代碼詳解,利用Java代碼在EditText控件中實現(xiàn),需要的朋友可以參考下2015-07-07
Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧分享
Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
解決BeanUtils.copyProperties無法成功封裝的問題
這篇文章主要介紹了解決BeanUtils.copyProperties無法成功封裝的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Springboot結(jié)合rabbitmq實現(xiàn)的死信隊列
為了保證訂單業(yè)務(wù)的消息數(shù)據(jù)不丟失,需要使用到RabbitMQ的死信隊列機制,本文主要介紹了Springboot結(jié)合rabbitmq實現(xiàn)的死信隊列,具有一定的參考價值,感興趣的可以了解一下2023-09-09

