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.mzph.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…