实例1:
创建 Map<String, Date> 并添加十个表示(姓氏、出生日期)对的条目。 从映射中删除夏季出生的所有人。 提示:在 CodeGym,夏季从 6 月 1 日持续到 8 月 31日。
要求:
•
程序不应该在屏幕上显示文本。
•
程序不得从键盘读取值。
•
createMap() 方法必须创建并返回包含 (String, Date) 元素且包含 10 个条目的 HashMap。
•
removeAllSummerPeople() 方法应从映射中删除夏季出生的所有人。
//创建和初始化HashMap map public static HashMap<String, Date> createMap() throws ParseException { DateFormat df = new SimpleDateFormat("MMMMM d yyyy", Locale.ENGLISH); HashMap<String, Date> map = new HashMap<>(); map.put("Stallone", df.parse("MAY 1 2012")); map.put("Stallone2", df.parse("JUNE 1 2012")); map.put("Stallone3", df.parse("JULY 1 2012")); map.put("Schwarzenegger", df.parse("AUGUST 1 2012")); map.put("Schwarzenegger2", df.parse("SEPTEMBER 1 2012")); map.put("Schwarzenegger3", df.parse("MAY 1 2012")); map.put("Willis", df.parse("JUNE 1 2012")); map.put("Willis2", df.parse("JUNE 1 2012")); map.put("Willis3", df.parse("JUNE 1 2012")); map.put("Stallone4", df.parse("JUNE 1 2012")); return map; } //先复制一个一模一样的HashMap,命名为copy; //再对copy.KeySet进行遍历,调用copy.get()方法获取每一个value值; //getMonth()方法返回的是0~11 HashMap<String, Date> copy = new HashMap<>(map); for (String key : copy.keySet()) { Date date = copy.get(key); int month = date.getMonth() + 1; if (month == 6 || month == 7 || month == 8) { map.remove(key); } }
实例2
创建 Map<String, String> 并添加十个表示(姓氏、名字)对的条目。 检查有多少人具有相同的名字或姓氏。
要求:
•
程序不应该在屏幕上显示文本。
•
程序不得从键盘读取值。
•
createMap() 方法必须创建并返回包含 (String, String) 元素的 HashMap,且该 HashMap 包含表示(姓氏、名字)对的 10 个条目。
•
getSameFirstNameCount() 方法必须返回具有相同名字的人员的数量。
•
getSameLastNameCount() 方法必须返回具有相同姓氏的人员的数量。
package zh.codegym.task.task08.task0815; import java.util.HashMap; /* 人口普查 */ public class Solution { public static HashMap<String, String> createMap() { HashMap<String, String> map = new HashMap<>(); map.put("Smith", "Christopher"); map.put("Johnson", "Daniel"); map.put("Williams", "Jason"); map.put("Jones", "Christopher"); map.put("Brown", "Daniel"); map.put("Davis", "Jason"); map.put("Miller", "Christopher"); map.put("Wilson", "Daniel"); map.put("Anderson", "Jason"); map.put("Jackson", "Christopher"); return map; } public static int getSameFirstNameCount(HashMap<String, String> map, String name) { int count = 0; for (String nameTmp : map.values()) { if (nameTmp.equals(name)) { count++; } } return count; } public static int getSameLastNameCount(HashMap<String, String> map, String lastName) { int count = 0; for (String lastNameTmp : map.keySet()) { if (lastNameTmp.equals(lastName)) { count++; } } return count; } public static void main(String[] args) { } }
实例3
创建 Map<String, Integer> 并添加表示(姓氏、薪水)对的十个条目。 从映射中删除薪水低于 500 的所有人。
要求:
•
程序不应该在屏幕上显示文本。
•
程序不得从键盘读取值。
•
createMap() 方法必须创建并返回包含 (String, Integer) 元素的 HashMap,且该 HashMap 包含表示(姓氏、薪水)对的 10 个条目。
•
removeItemFromMap() 方法必须从映射中删除薪水低于 500 的所有人。
package zh.codegym.task.task08.task0818; import java.util.HashMap; import java.util.Map; /* 只针对富人 */ public class Solution { public static HashMap<String, Integer> createMap() { HashMap<String, Integer> map = new HashMap<>(); map.put("Stallone", 500); map.put("Stallone2", 300); map.put("Stallone3", 100); map.put("Schwarzenegger", 2000); map.put("Schwarzenegger2", 200); map.put("Schwarzenegger3", 600); map.put("Willis", 700); map.put("Willis2", 850); map.put("Willis3", 725); map.put("Stallone4", 530); return map; } public static void removeItemFromMap(HashMap<String, Integer> map) { HashMap<String, Integer> copy = new HashMap<>(map); for (Map.Entry<String, Integer> integerEntry : copy.entrySet()) { if(integerEntry.getValue() < 500) { map.remove(integerEntry.getKey()); } } }
总结
HashMap、HashSet等集和映射涉及到对其中元素的操作:
一般喜欢先复制一个相同的映射或集(将要复制的对象作为参数传入新建立的复制体),再对复制体中的元素进行判定、遍历、与原来的集中元素比较等操作。