集合框架之Map接口
特点:存储一对数据,无序、无下标,键不可重复,值可重复
package com.oop.demo11; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Map接口使用 * 特点:存储键值对;键不能重复;值可以重复;无序 */ public class Demo12 { public static void main(String[] args) { //创建Map集合 Map<String,String> map=new HashMap<>(); //1.添加元素 map.put("cn","中国"); map.put("uk","英国"); map.put("usa","美国"); System.out.println("元素个数:"+map.size()); System.out.println(map.toString()); System.out.println("==========="); //2.删除 map.remove("usa"); System.out.println("删除之后:"+map.size()); System.out.println(map.toString()); System.out.println("=============="); //3.遍历 //3.1使用KeySet(); System.out.println("=====使用KeySet()======"); Set<String> keyset=map.keySet(); for (String key:keyset ) { System.out.println(key+"----"+map.get(key)); } //3.2使用entrySet(); System.out.println("=====使用entrySet()======"); Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String,String> entry:entries ) { System.out.println(entry.getKey()+"----"+entry.getValue()); } //4.判断 System.out.println("=====判断======"); System.out.println(map.containsKey("uk")); System.out.println(map.containsValue("中国")); System.out.println(map.isEmpty()); } }
-
HashMap:重点,面试高频问点
允许用null作为key或是value
package com.oop.demo11; import java.util.HashMap; import java.util.Map; /** * HashMap集合的使用 * 存储结构:哈希表(数组+链表+红黑树) */ public class Demo13 { public static void main(String[] args) { //创建集合 HashMap<Person,String> persons=new HashMap<Person,String>(); //添加元素 Person p1 = new Person("孙悟空",100); Person p2 = new Person("猪八戒",101); Person p3 = new Person("沙和尚",102); persons.put(p1,"北京"); persons.put(p2,"上海"); persons.put(p3,"杭州"); System.out.println("元素个数:"+persons.size()); System.out.println(persons.toString()); System.out.println("============"); //删除元素 persons.remove(p1); System.out.println("元素个数:"+persons.size()); System.out.println(persons.toString()); System.out.println("=====使用keySet()遍历====="); //遍历 //使用keySet()遍历 for (Person key:persons.keySet() ) { System.out.println(key.toString()+"----"+persons.get(key)); } System.out.println("=====使用entrySet()遍历====="); //使用entrySet()遍历 for (Map.Entry<Person,String> entry:persons.entrySet() ) { System.out.println(entry.getKey()+"----"+entry.getValue()); } System.out.println("=====判断====="); System.out.println(persons.containsKey(p2)); System.out.println(persons.containsValue("上海")); System.out.println(persons.isEmpty()); } } package com.oop.demo11; public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } 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; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
2.TreeMap
package com.oop.demo11; import java.util.Map; import java.util.TreeMap; /** * TreeMap的使用 * 存储结构:红黑树 */ public class Demo14 { public static void main(String[] args) { //创建集合 TreeMap<Person, String> treeMap = new TreeMap<>(); //添加元素 Person p1 = new Person("孙悟空",100); Person p2 = new Person("猪八戒",101); Person p3 = new Person("沙和尚",102); treeMap.put(p1,"北京"); treeMap.put(p2,"上海"); treeMap.put(p3,"杭州"); System.out.println(treeMap.size()); System.out.println(treeMap.toString()); System.out.println("----删除元素----"); treeMap.remove(p1); System.out.println(treeMap.size()); System.out.println(treeMap.toString()); System.out.println("----使用keySet----"); for (Person key:treeMap.keySet() ) { System.out.println(key+"----"+treeMap.get(key)); } System.out.println("----使用entrySet----"); for (Map.Entry<Person,String> entry:treeMap.entrySet() ) { System.out.println(entry.getKey()+"----"+entry.getValue()); } System.out.println("----判断----"); System.out.println(treeMap.containsKey(p2)); System.out.println(treeMap.containsValue("杭州")); System.out.println(treeMap.isEmpty()); } } package com.oop.demo11; public class Person implements Comparable<Person> { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } 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; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public int compareTo(Person o) { int n2=this.age-o.getAge(); return n2; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现