package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /*1.请简述Map 的特点 2.请简述HashMap的特点 3.请简述LinkedHashMap的特点 4.使用代码依次完成: a).将如下键值对信息存入Map集合中: "黄晓明", "Baby" "邓超", "孙俪" "李晨", "范冰冰" "大黑牛", "范冰冰" b).将"李晨"对应的值替换成"白百合" c).获取"大黑牛"对应的值,如果是"范冰冰"就删除以"大黑牛"为键的元素 5.使用代码完成:往一个Map集合中添加若干元素.获取Map中的所有key, 并使用增强for和迭代器遍历输出每个key 6.使用代码完成:往一个Map集合中添加若干元素.获取Map集合中所有的键值对(Entry)对象, 并使用增强for和迭代器遍历输出每个key和value 7.使用代码完成:将学员的信息作为元素存入HashMap中, 提示:1.每位学生(姓名,年龄)都有自己的家庭住址 2.将学生对象和家庭住址存储到map集合中。学生作为键, 家庭住址作为值。 3.学生姓名相同并且年龄相同视为同一名学生*/ public class Demo1 { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String,String>(); map.put("黄晓明", "Baby"); map.put("邓超", "孙俪"); String str1=map.put("李晨", "范冰冰"); map.put("大黑牛", "范冰冰"); System.out.println(map); //如果键值相同,直接put就是替换 String str2=map.put("李晨", "白百合"); System.out.println(map); System.out.println(str1+"..."+str2); String str3=map.get("大黑牛"); if("范冰冰".equals(str3)) { map.remove("大黑牛"); } System.out.println(map); System.out.println("==========================================="); //keySet使用增强for遍历 Set<String> keys = map.keySet(); for(String key:keys) { System.out.println(key+"..."+map.get(key)); } //keySet使用iterator遍历 System.out.println("==========================================="); Set<String> set =map.keySet(); Iterator<String> it=set.iterator(); while(it.hasNext()) { String name = it.next(); System.out.println(name+"..."+map.get(name)); } //entrySet使用增强for遍历 System.out.println("==========================================="); Set<Map.Entry<String,String>> entry = map.entrySet(); for(Map.Entry<String, String> key:entry) { System.out.println(key.getKey()+"..."+key.getValue()); } //entrySet使用iterator遍历 System.out.println("==========================================="); Set<Map.Entry<String,String>> set1 =map.entrySet(); Iterator<Map.Entry<String,String>> it1=set1.iterator(); while(it1.hasNext()) { Map.Entry<String,String> entry1= it1.next(); System.out.println(entry1.getKey()+"..."+entry1.getValue()); } HashMap<Student, String> students = new HashMap<Student,String>(); students.put(new Student("zhangsan",20), "北京"); students.put(new Student("lisi",21), "上海"); students.put(new Student("wangwu",22), "广州"); students.put(new Student("zhaoliu",23), "深圳"); students.put(new Student("zhaoliu",23), "香港"); for(Student stu:students.keySet()) { System.out.println(stu.toString()+"..."+students.get(stu)); } } } class Student{ private String name; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Student(String name, int age, String address) { super(); this.name = name; this.age = age; this.address = address; } public Student(String name, int age) { super(); this.name = name; this.age = age; } public Student() { super(); // TODO Auto-generated constructor stub } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((address == null) ? 0 : address.hashCode()); result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (address == null) { if (other.address != null) return false; } else if (!address.equals(other.address)) return false; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", address=" + address + "]"; } }
原文地址:https://www.cnblogs.com/qingyundian/p/8495079.html
时间: 2024-11-07 15:04:27