java基础之HashMap练习题

发布时间:2024年01月09日

应用了 外循环,自循环,Lambda表达式,keySet()方法,values()方法,Entry对象 ,entrySet()方法

  • Map集合框架

1:(Map)关于下列Map 接口中常见的方法:

①put 方法表示放入一个键值对,如果键已存在则????修改值 ??????????,如果键不存在则??????新增一条键值对 ????????;

②remove 方法接受??????一个键的 ????????个参数,表示??删除对应的键值对 ????????????;

③get 方法表示?????获取该键的value ????????,get 方法的参数表示??????key值 ????????,返回值表示???value值 ??????????? ;

④要想获得Map 中所有的键,应该使用方法 ?????keyset ?????????,该方法返回值类型为????Set集合 ??????????;

⑤要想获得Map 中所有的值,应该使用方法 ?????values ?????????,该方法返回值类型为???????Collection ???????

2:(Map)关于HashMap 和 Hashtable 的区别描述正确的是(B)

A.HashMap 是实现 Map 接口,Hashtable 是实现 Table 接口

B.HashMap 性能优于Hashtable,且允许空值和空键

C.HashMap 是线程安全的

D.以上描述都不正确

3:(Map)编程:创建一个银行用户 BankUser 类,用户信息为用户 id、用户名 userName、余额 balance;

①利用 HashMap 存储用户信息:id 作为键,用户作为值,创建多个用户对象进行存储

② 遍历打印输出所有银行的用户名和对应的余额

③键盘输入一个用户名,检测是否存在此用户,存在-打印用户信息;不存在-打印查无此人

④ 打印输出余额大于 200000 的用户信息

public class TestBankUser {
????public static void main(String[] args) {
????????BankUser b1=new BankUser("001","张三",8090);
????????BankUser b2=new BankUser("002","李四",298500);
????????BankUser b3=new BankUser("003","王五",9632);
????????Map<String,BankUser> map=new HashMap<>();
????????map.put(b1.getId(),b1);
????????map.put(b2.getId(),b2);
????????map.put(b3.getId(),b3);
????????map.forEach((k,v)-> System.out.println(v.getName()+" "+v.getBalance()));
????????System.out.println();

????????Scanner sc=new Scanner(System.in);
????????System.out.print("请输入一个用户名:");
????????String str=sc.next();
????????boolean boo=false;
????????for (BankUser b: map.values()){
????????????if (str.equals(b.getName())){
????????????????System.out.println("存在此用户!");
????????????????System.out.println(b);
????????????????boo=true;
????????????}
????????}
????????if (!boo){
????????????System.out.println("此用户不存在!");
????????}
????????System.out.println();

????????map.forEach((k,v)->{
????????????if (v.getBalance()>200000){
????????????????System.out.println(v);
????????????}
????????});

????}
}

4. (Map)编程:利用 Map,完成下面的功能:

从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该年没有举办世界

杯,则输出:没有举办世界杯。

附录(往下滑):截止 2009 年,历届世界杯冠军、世界杯冠军以及对应的夺冠年份

package com.by.honework5;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class WorldCup {
????private Integer session;
????private String vactor;

????public WorldCup() {
????}

????public WorldCup(Integer session, String vactor) {
????????this.session = session;
????????this.vactor = vactor;
????}

????public Integer getSession() {
????????return session;
????}

????public void setSession(Integer session) {
????????this.session = session;
????}

????public String getVactor() {
????????return vactor;
????}

????public void setVactor(String vactor) {
????????this.vactor = vactor;
????}

????public int hashCode(){
????????return this.session.hashCode()+this.vactor.hashCode();
????}

????public boolean equals(Object o){
????????if (this==o){
????????????return true;
????????}
????????if (this.getClass()!=o.getClass()&&o==null){
????????????return false;
????????}
????????WorldCup w=(WorldCup) o;
????????return this.session.equals(w.session)&&this.vactor.equals(w.vactor);
????}

????@Override
????public String toString() {
????????return "WorldCup{" +
????????????????"session=" + session +
????????????????", vactor='" + vactor + '\'' +
????????????????'}';
????}
}


class Test4{
????public static void main(String[] args) {
????????Scanner sc=new Scanner(System.in);
????????System.out.println("请输入年份");
????????int years=sc.nextInt();
????????Map<Integer,WorldCup> map=new LinkedHashMap<>();
????????map.put(2006,new WorldCup(18,"意大利"));
????????map.put(2002,new WorldCup(17,"意大利"));
????????map.put(1998,new WorldCup(16,"意大利"));
????????map.put(1994,new WorldCup(15,"意大利"));
????????map.put(1990,new WorldCup(14,"意大利"));
????????map.put(1986,new WorldCup(13,"意大利"));
????????map.put(1982,new WorldCup(12,"意大利"));
????????map.put(1978,new WorldCup(11,"意大利"));
????????map.put(1974,new WorldCup(10,"意大利"));
????????map.put(1970,new WorldCup(9,"意大利"));
????????map.put(1966,new WorldCup(8,"意大利"));
????????map.put(1962,new WorldCup(7,"意大利"));
????????map.put(1958,new WorldCup(6,"意大利"));
????????map.put(1954,new WorldCup(5,"意大利"));
????????map.put(1950,new WorldCup(4,"意大利"));
????????map.put(1938,new WorldCup(3,"意大利"));
????????map.put(1934,new WorldCup(2,"意大利"));
????????map.put(1930,new WorldCup(1,"意大利"));
????????Set<Integer> set=map.keySet();
????????boolean boo=false;
????????for (Integer n:set
?????????????) {
????????????if (n.equals(years)){
????????????????System.out.println(map.get(n)+" "+n);
????????????????boo=true;
????????????}
????????}
????????if (!boo){
????????????System.out.println("没有举办世界杯");
????????}

????}
}

5. (Map)在上一题目的基础上,增加如下功能:

读入一支球队的名字,输出该球队夺冠的年份列表。

例如:

①读入“巴西”,应当输出 1958、1962、1970、1994、2002

② 读入“荷兰”,应当输出”没有获得过世界杯”。

package com.by.honework5;

import java.util.*;
import java.util.function.BiConsumer;

public class WorldCup {
????private Integer session;
????private String vactor;

????public WorldCup() {
????}

????public WorldCup(Integer session, String vactor) {
????????this.session = session;
????????this.vactor = vactor;
????}

????public Integer getSession() {
????????return session;
????}

????public void setSession(Integer session) {
????????this.session = session;
????}

????public String getVactor() {
????????return vactor;
????}

????public void setVactor(String vactor) {
????????this.vactor = vactor;
????}

????public int hashCode(){
????????return this.session.hashCode()+this.vactor.hashCode();
????}

????public boolean equals(Object o){
????????if (this==o){
????????????return true;
????????}
????????if (this.getClass()!=o.getClass()&&o==null){
????????????return false;
????????}
????????WorldCup w=(WorldCup) o;
????????return this.session.equals(w.session)&&this.vactor.equals(w.vactor);
????}

????@Override
????public String toString() {
????????return "WorldCup{" +
????????????????"session=" + session +
????????????????", vactor='" + vactor + '\'' +
????????????????'}';
????}
}


class Test4{
????public static void main(String[] args) {
????????Scanner sc=new Scanner(System.in);
????????System.out.println("请输入年份");
????????int years=sc.nextInt();
????????Map<Integer,WorldCup> map=new LinkedHashMap<>();
????????map.put(2006,new WorldCup(18,"意大利"));
????????map.put(2002,new WorldCup(17,"巴西"));
????????map.put(1998,new WorldCup(16,"法国"));
????????map.put(1994,new WorldCup(15,"巴西"));
????????map.put(1990,new WorldCup(14,"德国"));
????????map.put(1986,new WorldCup(13,"阿根廷"));
????????map.put(1982,new WorldCup(12,"意大利"));
????????map.put(1978,new WorldCup(11,"阿根廷"));
????????map.put(1974,new WorldCup(10,"德国"));
????????map.put(1970,new WorldCup(9,"巴西"));
????????map.put(1966,new WorldCup(8,"英格兰"));
????????map.put(1962,new WorldCup(7,"巴西"));
????????map.put(1958,new WorldCup(6,"巴西"));
????????map.put(1954,new WorldCup(5,"德国"));
????????map.put(1950,new WorldCup(4,"乌拉圭"));
????????map.put(1938,new WorldCup(3,"意大利"));
????????map.put(1934,new WorldCup(2,"意大利"));
????????map.put(1930,new WorldCup(1,"乌拉圭"));
????????Set<Integer> set=map.keySet();
????????boolean boo=false;
????????for (Integer n:set
?????????????) {
????????????if (n.equals(years)){
????????????????System.out.println(map.get(n)+" "+n);
????????????????boo=true;
????????????}
????????}
????????if (!boo){
????????????System.out.println("没有举办世界杯");
????????}


????????System.out.println();
????????System.out.println("请输入球队名称:");
????????String name=sc.next();
????????boolean boo3=false;
????????map.forEach(new BiConsumer<Integer, WorldCup>() {
????????????@Override
????????????public void accept(Integer integer, WorldCup worldCup) {
????????????????if (worldCup.getVactor().equals(name)){
????????????????????System.out.println(integer);
????????????????????boo=true;
????????????????}
????????????}
????????});
????????if (!boo){
????????????System.out.println("不存在");
????????}
??????
????????
????????/*Collection<WorldCup> coll=map.values();
????????boolean boo1=false;
????????for (WorldCup w:coll){
????????????if (w .getVactor().equals(name)){
????????????????System.out.println(w);
????????????????map.forEach(new BiConsumer<Integer, WorldCup>() {
????????????????????@Override
????????????????????public void accept(Integer integer, WorldCup worldCup) {
????????????????????????if(map.containsValue(w)){
????????????????????????????System.out.println(integer);
????????????????????????}
????????????????????}
????????????????});
????????????}
????????}
????????if (!boo1){
????????????System.out.println("没有获得世界杯");
????????}*/



????}
}

6.(Map)编程:已知某学校的教学课程内容安排如下:

完成下列要求:

①使用 Map,以老师的名字作为键,教授的课程名作为值,表示上述课程安排

②增加了一位新老师 Allen 教 JDBC

③ Lucy 改为教 CoreJava

④ 遍历 Map,输出所有的老师及老师教授的课程

⑤ 利用 Map,输出所有教 JSP 的老师

⑥ 统计教授 CoreJava 和 JDBC 老师的人数

public class TestTeacher {
????public static void main(String[] args) {
????????Map<String,String> map=new HashMap<>();
????????map.put("Tom","CoreJava");
????????map.put("John","Oracle");
????????map.put("Susan","Oracle");
????????map.put("Jerry","JDBC");
????????map.put("Jim","Unix");
????????map.put("Kevin","JSP");
????????map.put("Lucy","JSP");
????????map.put("Allen","JDBC");
????????map.put("Lucy","CoreJava");
????????map.forEach((k,v)-> System.out.println(k+" "+v));
????????System.out.println("所有教JSP的老师:");
????????map.forEach((k,v)->{
????????????if (v.equals("JSP")){
????????????????System.out.println(k);
????????????}
????????});
????????int sum=0;
????????int q=0;
????????Collection<String> coll=map.values();
????????for (String s1:coll){
????????????if (s1.equals("CoreJava")){
????????????????sum++;
????????????}
????????????if (s1.equals("JDBC")){
????????????????q++;
????????????}
????????}
????????System.out.println("教授CoreJava的老师的人数:"+sum);
????????System.out.println("教授JDBC的老师的人数:"+q);
????}
}

  1. (Map) 给定一个字符串,请输出该字符串由哪些字符组成,每个字符出现几次?
public class TestString {
????public static void main(String[] args) {
????????//字符:key,次数:value ?键值对遍历
????????Scanner sc=new Scanner(System.in);
????????System.out.println("请输入一个字符串:");
????????String str=sc.next();

????????Map<String,Integer> map=new HashMap<>();
????????for (int i=0;i<str.length();i++){
????????????char c=str.charAt(i);
????????????String key=" "+c;
????????????if (map.containsKey(key)){
????????????????int value=map.get(key);
????????????????map.put(key,value+1);
????????????}else {
????????????????map.put(key,1);
????????????}
????????}
????????//键值对遍历
????????Set<Map.Entry<String,Integer>> set=map.entrySet();
????????set.forEach(entry-> System.out.println("字符:"+entry.getKey()+",次数:"+entry.getValue()));
????}
}

  1. 利用Map集合存储以下信息: key-学号 , value-姓名

1000 - 张三

1001 - 李四

1002 - 胡八一

1003 - 张一山

要求:

① 打印输出所有的信息,打印格式要求: 学号-1000,姓名-张三 。

② 输入一个学号,查找是否存在,存在,则输出对应的姓名,否则输出"查无此人!"

③ 打印输出所有姓张的学号。

public class Test8 {
????public static void main(String[] args) {
????????Map<String, String> map=new HashMap<>();
????????map.put("1000","张三");
????????map.put("1001","李四");
????????map.put("1002","胡八一");
????????map.put("1003","张一山");

????????map.forEach((k,v)-> System.out.println("学号-"+k+",姓名-"+v));
????????System.out.println();

????????Scanner sc=new Scanner(System.in);
????????System.out.print("请输入一个学号:");
????????String str=sc.next();
????????boolean boo=false;
????????for (String b: map.keySet()){
????????????if (str.equals(b)){
????????????????System.out.println("存在!");
????????????????System.out.println(map.get(b));
????????????????boo=true;
????????????}
????????}
????????if (!boo){
????????????System.out.println("查无此人!");
????????}
????????System.out.println();

????????map.forEach((k,v)->{
????????????if (v.charAt(0)=='张'){
????????????????System.out.println(k);
????????????}
????????});
????}
}

  1. class Worker{

private String name;

private int age;

private double salary;

//补全构造方法、get/set方法、toString、hashCode、equals方法

???}

????????????????????????????????????????

???要求:

① 利用Map集合存储以下信息:

key-id(Integer) -- value-工人对象(Worker)

1 -- (张三,27,10000) ?

2 -- (李四,23,7500) ?

3 -- (王五,25,6000) ?

...

?? ② 遍历输出集合中所有的信息:id-工人信息

? ③ 输入一个id,查询是否存在,存在则输出该工人的信息,否则打印输出"查无此人!"

? ④ 统计工人的平均工资。

??? ⑤ 输入一个id,删除此员工.

??? ⑥ 输出工人工资大于7000的id号。 ?

??? ⑦ 输入一个员工姓名,查询集合中是否存在该员工,存在-将此员工的工资在原有的基础上加2000,

??????否则打印"不存在此姓名员工".

public class TestWorker {
????public static void main(String[] args) {
????????Map<Integer,Worker> map=new HashMap<>();
????????map.put(1,new Worker("张三",27,10000));
????????map.put(2,new Worker("李四",23,7500));
????????map.put(3,new Worker("王五",25,6000));
????????map.forEach((k,v)-> System.out.println(k+"-"+v));
????????Scanner sc=new Scanner(System.in);
????????System.out.print("请输入一个id:");
????????Integer str=sc.nextInt();
????????boolean boo=false;
????????for (Integer b: map.keySet()){
????????????if (str.equals(b)){
????????????????System.out.println("存在!");
????????????????System.out.println(map.get(b));
????????????????boo=true;
????????????}
????????}
????????if (!boo){
????????????System.out.println("查无此人!");
????????}
????????System.out.println();

????????double sum=0;
????????Collection<Worker> coll=map.values();
????????for (Worker w:coll){
????????????sum+=w.getSalary();
????????}
????????System.out.println("平均工资为:"+sum/ map.size());
????????System.out.println();

????????System.out.print("请输入一个id:");
????????Integer str2=sc.nextInt();
????????map.remove(str2);
????????System.out.println();

????????map.forEach((k,v)->{
????????????if (v.getSalary()>7000){
????????????????System.out.println("大于7000的id:"+k);
????????????}
????????});
????????System.out.println();

????????System.out.print("请输入一个员工姓名:");
????????String str3=sc.next();
????????boolean boo2=false;
????????for (Worker b: map.values()){
????????????if (str3.equals(b.getName())){
????????????????System.out.println("存在!");
????????????????System.out.println("此员工的工资在原有的基础上加2000后为:");
????????????????System.out.println(b.getSalary()+2000);
????????????????boo2=true;
????????????}
????????}
????????if (!boo2){
????????????System.out.println("不存在此姓名员工!");
????????}

????}
}

10. class Student(){

private String name;

private Integer age;

private boolean sex; //true-男

private Double score;

//省略无参数、有参数的构造方法...

//省略get/set

}

1班学生:

name:zhangsan ? age:18 ??...

???????????name:lisi ????? age:21 ??...

name:wangwu ??? age:25 ??... ?

????????2班学生:

name:Tom ????? age:20 ??...

???????????name:John ? age:22 ??...

name:Lucy ????? age:26 ??... ?

????????3班学生:

name:rc ??????? age:19 ??...

???????????name:mq ??????? age:23 ??...

name:Lucy ????? age:28 ??... ?

????????完成以下要求:

① 利用Map,以班级号作为键(String类型),全班的学生作为值,描述以上所有班级信息。

② 将Map中班级对应的班级学生信息输出。

③ 打印输出每一个班级的平均分数.

④ 统计所有班级的女生的人数,并打印输出.

public class TestStudent10 {
????public static void main(String[] args) {
????????Map<String, List<Student10>> map=new HashMap<>();
????????List<Student10> list=new ArrayList<>();
????????List<Student10> list2=new ArrayList<>();
????????List<Student10> list3=new ArrayList<>();
????????list.add(new Student10("zhangsan",18,true,78.8));
????????list.add(new Student10("lisi",21,false,99.8));
????????list.add(new Student10("wangwu",25,false,95.8));
????????list2.add(new Student10("Tom",20,true,68.8));
????????list2.add(new Student10("John",22,true,97.8));
????????list2.add(new Student10("Lucy",26,false,94.8));
????????list3.add(new Student10("rc",19,true,86.8));
????????list3.add(new Student10("mq",23,true,88.8));
????????list3.add(new Student10("Lucy",28,false,98.8));
????????map.put("1班",list);
????????map.put("1班",list);
????????map.put("1班",list);
????????map.put("2班",list2);
????????map.put("2班",list2);
????????map.put("2班",list2);
????????map.put("3班",list3);
????????map.put("3班",list3);
????????map.put("3班",list3);
????????map.forEach((k,v)-> System.out.println(k+" "+v));
????????System.out.println();

????????Set<Map.Entry<String,List<Student10>>> set=map.entrySet();
????????for (Map.Entry<String,List<Student10>> aa:set){
????????????double sum=0;//班级总分
????????????if (aa.getKey().equals("1班")){
????????????????List<Student10> value=aa.getValue();
????????????????for (Student10 stu:value){
????????????????????sum+=stu.getScore();
????????????????}
????????????????System.out.println("1班平均成绩:"+sum/ map.size());
????????????}
????????????if (aa.getKey().equals("2班")){
????????????????List<Student10> value=aa.getValue();
????????????????for (Student10 stu:value){
????????????????????sum+=stu.getScore();
????????????????}
????????????????System.out.println("2班平均成绩:"+sum/ map.size());
????????????}
????????????if (aa.getKey().equals("3班")){
????????????????List<Student10> value=aa.getValue();
????????????????for (Student10 stu:value){
????????????????????sum+=stu.getScore();
????????????????}
????????????????System.out.println("3班平均成绩:"+sum/ map.size());
????????????}
????????}
????????System.out.println();

????????int g=0;//女生数量
????????Collection<List<Student10>> ss=map.values();
????????for (List<Student10> ww:ss){
????????????for(Student10 stu:ww){
????????????????if (stu.isSex()==false){
????????????????????g++;
????????????????}
????????????}
????????}
????????System.out.println("所有女生人数:"+g);

????}
}

附录:

文章来源:https://blog.csdn.net/zhzjn/article/details/135476125
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。