Java入門第三季——Java中的集合框架(中):MapHashMap

?

?

?

?

?

?

?

?

 1 package com.imooc.collection;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 /**
 7  * 學生類
 8  * @author Administrator
 9  *
10  */
11 public class Student {
12 
13     public String id;
14     
15     public String name;
16     
17     public Set<Course> courses;
18 
19     public Student(String id, String name) {
20         this.id = id;
21         this.name = name;
22         this.courses = new HashSet<Course>();
23     }
24 }

?

 1 package com.imooc.collection;
 2 
 3 /**
 4  * 課程類
 5  * @author Administrator
 6  *
 7  */
 8 public class Course {
 9 
10     public String id;
11     
12     public String name;
13     
14     public Course(String id, String name) {
15         this.id = id ;
16         
17         this.name = name;
18     }
19     
20     public Course() {
21         
22     }
23 }

?

  1 package com.imooc.collection;
  2 
  3 import java.util.HashMap;
  4 import java.util.Map;
  5 import java.util.Map.Entry;
  6 import java.util.Scanner;
  7 import java.util.Set;
  8 
  9 public class MapTest {
 10 
 11     /**
 12      * 用來承裝學生類型對象
 13      */
 14     public Map<String, Student> students;
 15 
 16     /**
 17      * 在構造器中初始化students屬性
 18      */
 19     public MapTest() {
 20         this.students = new HashMap<String, Student>();
 21     }
 22 
 23     /**
 24      * 測試添加:輸入學生ID,判斷是否被占用 若未被占用,則輸入姓名,創建新學生對象,并且 添加到students中
 25      */
 26     public void testPut() {
 27         // 創建一個Scanner對象,用來獲取輸入的學生ID和姓名
 28         Scanner console = new Scanner(System.in);
 29         int i = 0;
 30         while (i < 3) {
 31             System.out.println("請輸入學生ID:");
 32             String ID = console.next();//獲取從鍵盤輸入的ID字符串
 33             // 判斷該ID是否被占用
 34             Student st = students.get(ID);//獲取該鍵對應的value值
 35             if (st == null) {
 36                 // 提示輸入學生姓名
 37                 System.out.println("請輸入學生姓名:");
 38                 String name = console.next();//取得鍵盤輸入的學生姓名的字符串
 39                 // 創建新的學生對象
 40                 Student newStudent = new Student(ID, name);
 41                 // 通過調用students的put方法,添加ID-學生映射
 42                 students.put(ID, newStudent);
 43                 System.out.println("成功添加學生:" + students.get(ID).name);
 44                 i++;
 45             } else {
 46                 System.out.println("該學生ID已被占用!");
 47                 continue;
 48             }
 49         }
 50     }
 51 
 52     /**
 53      * 測試Map的keySet方法,返回集合的方法
 54      * 通過keySet和get方法去遍歷Map中的每個value
 55      */
 56     public void testKeySet() {
 57         // 通過keySet方法,返回Map中的所有“鍵”的Set集合
 58         Set<String> keySet = students.keySet();
 59         // 取得students的容量
 60         System.out.println("總共有:" + students.size() + "個學生!");
 61         // 遍歷keySet,取得每一個鍵,再調用get方法取得每個鍵對應的value
 62         for (String stuId : keySet) {
 63             Student st = students.get(stuId);
 64             if (st != null)
 65                 System.out.println("學生:" + st.name);
 66         }
 67     }
 68 
 69     /**
 70      * 測試刪除Map中的映射
 71      */
 72     public void testRemove() {
 73         // 獲取從鍵盤輸入的待刪除學生ID字符串
 74         Scanner console = new Scanner(System.in);
 75         while (true) {
 76             // 提示輸入待刪除的學生的ID
 77             System.out.println("請輸入要刪除的學生ID!");
 78             String ID = console.next();
 79             // 判斷該ID是否有對應的學生對象
 80             Student st = students.get(ID);
 81             if (st == null) {
 82                 // 提示輸入的ID并不存在
 83                 System.out.println("該ID不存在!");
 84                 continue;
 85             }
 86             students.remove(ID);
 87             System.out.println("成功刪除學生:" + st.name);
 88             break;
 89         }
 90     }
 91 
 92     /**
 93      * 通過entrySet方法來遍歷Map
 94      */
 95     public void testEntrySet() {
 96         // 通過entrySet方法,返回Map中的所有鍵值對
 97         Set<Entry<String, Student>> entrySet = students.entrySet();
 98         for (Entry<String, Student> entry : entrySet) {
 99             System.out.println("取得鍵:" + entry.getKey());
100             System.out.println("對應的值為:" + entry.getValue().name);
101         }
102     }
103 
104     /**
105      * 利用put方法修改Map中的已有映射
106      */
107     public void testModify() {
108         // 提示輸入要修改的學生ID
109         System.out.println("請輸入要修改的學生ID:");
110         // 創建一個Scanner對象,去獲取從鍵盤上輸入的學生ID字符串
111         Scanner console = new Scanner(System.in);
112         while (true) {
113             // 取得從鍵盤輸入的學生ID
114             String stuID = console.next();
115             // 從students中查找該學生ID對應的學生對象
116             Student student = students.get(stuID);
117             if (student == null) {
118                 System.out.println("該ID不存在!請重新輸入!");
119                 continue;
120             }
121             // 提示當前對應的學生對象的姓名
122             System.out.println("當前該學生ID,所對應的學生為:" + student.name);
123             // 提示輸入新的學生姓名,來修改已有的映射
124             System.out.println("請輸入新的學生姓名:");
125             String name = console.next();
126             Student newStudent = new Student(stuID, name);
127             students.put(stuID, newStudent);
128             System.out.println("修改成功!");
129             break;
130         }
131     }
132 
133     /**
134      * @param args
135      */
136     public static void main(String[] args) {
137         MapTest mt = new MapTest();
138         mt.testPut();
139         mt.testKeySet();
140         // mt.testRemove();
141         // mt.testEntrySet();
142         // mt.testModify();
143         // mt.testEntrySet();
144 
145     }
146 
147 }

?

轉載于:https://www.cnblogs.com/songsongblue/p/9771671.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388250.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388250.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388250.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【譯】 WebSocket 協議第八章——錯誤處理(Error Handling)

概述 本文為 WebSocket 協議的第八章&#xff0c;本文翻譯的主要內容為 WebSocket 錯誤處理相關內容。 錯誤處理&#xff08;協議正文&#xff09; 8.1 處理 UTF-8 數據錯誤 當終端按照 UTF-8 的格式來解析一個字節流&#xff0c;但是發現這個字節流不是 UTF-8 編碼&#xff0c…

升級xcode5.1 iOS 6.0后以前的橫屏項目 變為了豎屏

升級xcode5.1 iOS 6.0后以前的橫屏項目 變為了豎屏&#xff0c;以下為解決辦法&#xff1a; 在AppDelegate 的初始化方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中 將 [window addSubview: viewCon…

動漫數據推薦系統

Simple, TfidfVectorizer and CountVectorizer recommendation system for beginner.簡單的TfidfVectorizer和CountVectorizer推薦系統&#xff0c;適用于初學者。 目標 (The Goal) Recommendation system is widely use in many industries to suggest items to customers. F…

Wait Event SQL*Net more data to client

oracle 官方給的說法是 C.3.152 SQL*Net more data to client The server process is sending more data/messages to the client. The previous operation to the client was also a send. Wait Time: The actual time it took for the send to complete 意味著server process…

1.3求根之牛頓迭代法

目錄 目錄前言&#xff08;一&#xff09;牛頓迭代法的分析1.定義2.條件3.思想4.誤差&#xff08;二&#xff09;代碼實現1.算法流程圖2.源代碼&#xff08;三&#xff09;案例演示1.求解&#xff1a;\(f(x)x^3-x-10\)2.求解&#xff1a;\(f(x)x^2-1150\)3.求解&#xff1a;\(f…

libzbar.a armv7

楊航最近在學IOS&#xfeff;&#xfeff; http://download.csdn.net/download/lzwxyz/5546365 我現在用的是這個&#xff1a;http://www.federicocappelli.net/2012/10/05/zbar-library-for-iphone-5-armv7s/ 點它的HERE開始下載 下載的libzbar.a庫&#xff0c;如何查看 …

Alex Hanna博士:Google道德AI小組研究員

Alex Hanna博士是社會學家和研究科學家&#xff0c;致力于Google的機器學習公平性和道德AI。 (Dr. Alex Hanna is a sociologist and research scientist working on machine learning fairness and ethical AI at Google.) Before that, she was an Assistant Professor at th…

三位對我影響最深的老師

我感覺&#xff0c;教過我的老師們&#xff0c;不論他們技術的好壞對我都是有些許影響的。但是讓人印象最深的好像只有寥寥幾位。 第一位就是小學六年級下冊教過我的語文老師。他是臨時從一個貧困小學調任過來的&#xff0c;不怎么管班級&#xff0c;班里同學都在背地里說他不會…

安全開發 | 如何讓Django框架中的CSRF_Token的值每次請求都不一樣

前言 用過Django 進行開發的同學都知道&#xff0c;Django框架天然支持對CSRF攻擊的防護&#xff0c;因為其內置了一個名為CsrfViewMiddleware的中間件&#xff0c;其基于Cookie方式的防護原理&#xff0c;相比基于session的方式&#xff0c;更適合目前前后端分離的業務場景&am…

UNITY3D 腦袋頂血頂名

&#xfeff;&#xfeff;楊航最近在學Unity3D&#xfeff;&#xfeff; using UnityEngine; using System.Collections; public class NPC : MonoBehaviour { //主攝像機對象 public Camera camera; //NPC名稱 private string name "我是doud…

一個項目的整個測試流程

最近一直在進行接口自動化的測試工作&#xff0c;同時對于一個項目的整個測試流程進行了梳理&#xff0c;希望能對你有用~~~ 需求分析&#xff1a; 整體流程圖&#xff1a; 需求提取 -> 需求分析 -> 需求評審 -> 更新后的測試需求跟蹤xmind 分析流程&#xff1a; 1. 需…

python度量學習_Python的差異度量

python度量學習Hi folks, welcome back to my new edition of the blog, thank you so much for your love and support, I hope you all are doing well. In today’s learning, we will try to understand about variance and the measures involved in it. Although the blo…

多個攝像機之間的切換

楊航最近在學Unity3D&#xfeff;&#xfeff; Unity3D入門 第捌章: 多個攝像機之間的切換 內容描述&#xff1a;這章&#xff0c;我們來學習一下同個場景中多個攝像機怎么切換。 接著我們創建一個空對象 GameObject -> Create Empty 命名為CamearController&#xff0…

Kubernetes的共享GPU集群調度

問題背景 全球主要的容器集群服務廠商的Kubernetes服務都提供了Nvidia GPU容器調度能力&#xff0c;但是通常都是將一個GPU卡分配給一個容器。這可以實現比較好的隔離性&#xff0c;確保使用GPU的應用不會被其他應用影響&#xff1b;對于深度學習模型訓練的場景非常適合&#x…

django-celery定時任務以及異步任務and服務器部署并且運行全部過程

Celery 應用Celery之前&#xff0c;我想大家都已經了解了&#xff0c;什么是Celery&#xff0c;Celery可以做什么&#xff0c;等等一些關于Celery的問題&#xff0c;在這里我就不一一解釋了。 應用之前&#xff0c;要確保環境中添加了Celery包。 pip install celery pip instal…

網頁視頻15分鐘自動暫停_在15分鐘內學習網頁爬取

網頁視頻15分鐘自動暫停什么是網頁抓取&#xff1f; (What is Web Scraping?) Web scraping, also known as web data extraction, is the process of retrieving or “scraping” data from a website. This information is collected and then exported into a format that …

Unity3D面試ABC

Unity3D面試ABC 楊航最近在學Unity3D&#xfeff;&#xfeff; 最先執行的方法是&#xff1a; 1、&#xff08;激活時的初始化代碼&#xff09;Awake&#xff0c;2、Start、3、Update【FixUpdate、LateUpdate】、4、&#xff08;渲染模塊&#xff09;OnGUI、5、再向后&#xff…

前嗅ForeSpider教程:創建模板

今天&#xff0c;小編為大家帶來的教程是&#xff1a;如何在前嗅ForeSpider中創建模板。主要內容有&#xff1a;模板的概念&#xff0c;模板的配置方式&#xff0c;模板的高級選項&#xff0c;具體內容如下&#xff1a; 一&#xff0c;模板的概念 模板列表的層級相當于網頁跳轉…

2.PHP利用PDO連接方式連接mysql數據庫

代碼如下 <?php$serverName "這里填IP地址";$dbName "這里填數據庫名";$userName "這里填用戶名&#xff08;默認為root&#xff09;";$password "";/*密碼默認不用填*/try { $conn new PDO("mysql:host$serverName;…

django 性能優化_優化Django管理員

django 性能優化Managing data from the Django administration interface should be fast and easy, especially when we have a lot of data to manage.從Django管理界面管理數據應該快速簡便&#xff0c;尤其是當我們要管理大量數據時。 To improve that process and to ma…