本文共 4841 字,大约阅读时间需要 16 分钟。
在现实生活中,我们经常会看到如IP地址与主机名、身份证号与个人等一一对应的映射关系。Java为此提供了专门的集合类——java.util.Map接口,用以存储这种键值对的对象。
通过查看Map接口描述,可以发现其与Collection接口的主要区别在于存储形式。Collection中的集合元素是孤立存在的,而Map中的集合元素则是成对存在的,每个元素由键与值两部分组成,通过键可以定位对应的值。Collection称为单列集合,Map称为双列集合。需要注意的是,Map集合的键不能重复,但值可以重复,每个键只能对应一个值。
HashMap<K,V>HashMap是最常用的Map实现类,其内部采用哈希表结构存储数据。由于哈希表的特点,HashMap的性能非常高,插入、删除和查找操作均为O(1)时间复杂度。为了保证键的唯一性,HashMap要求键必须重写hashCode()和equals()方法。
LinkedHashMap<K,V>LinkedHashMap是HashMap的一个子类,其内部不仅采用了哈希表结构,还引入了链表结构。链表用于存储键的顺序信息,使得LinkedHashMap能够保持元素的插入顺序。因此,LinkedHashMap既保证了高效的查找性能,又保留了元素的顺序,适用于需要有序存储键值对的场景。
public V put(K key, V value):将指定键与值添加到集合中。public V remove(Object key):删除指定键所对应的键值对,返回被删除的值。public V get(Object key):根据键获取对应的值。boolean containsKey(Object key):判断集合中是否包含指定键。public Set<K> keySet():获取集合中所有键,返回一个Set集合。public Set<Map.Entry<K,V>> entrySet():获取集合中所有键值对对象,返回一个Set集合。public class MapDemo { public static void main(String[] args) { Map map = new HashMap<>(); map.put("黄晓明", "杨颖"); map.put("文章", "马伊琍"); map.put("邓超", "孙俪"); System.out.println(map); System.out.println(map.remove("邓超")); System.out.println(map); System.out.println(map.get("黄晓明")); System.out.println(map.get("邓超")); }} Set<K> keys = map.keySet();for (K key : keys) { ... }String value = map.get(key);示例代码:
public class MapDemo01 { public static void main(String[] args) { Map map = new HashMap<>(); map.put("胡歌", "霍建华"); map.put("郭德纲", "于谦"); map.put("薛之谦", "大张伟"); Set keys = map.keySet(); for (String key : keys) { String value = map.get(key); System.out.println(key + "的CP是:" + value); } }} Map集合中的每个键值对可以看作是一个Entry对象。Entry类提供了获取键和值的方法:
public K getKey():获取键。public V getValue():获取值。获取所有Entry对象的方法:Set<Map.Entry<K,V>> entrySet = map.entrySet();
示例代码:
public class MapDemo02 { public static void main(String[] args) { Map map = new HashMap<>(); map.put("胡歌", "霍建华"); map.put("郭德纲", "于谦"); map.put("薛之谦", "大张伟"); Set > entrySet = map.entrySet(); for (Map.Entry entry : entrySet) { System.out.println(entry.getKey() + "的CP是:" + entry.getValue()); } }} 在实际应用中,除了简单的字符串键值对,Map集合还可以存储自定义对象。例如,可以将学生对象作为键,家庭住址作为值。
public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); }} public class HashMapTest { public static void main(String[] args) { Map map = new HashMap<>(); map.put(new Student("lisi", 28), "上海"); map.put(new Student("wangwu", 22), "北京"); map.put(new Student("zhaoliu", 24), "成都"); map.put(new Student("wangwu", 22), "南京"); Set keySet = map.keySet(); for (Student key : keySet) { String value = map.get(key); System.out.println(key.toString() + "....." + value); } }} LinkedHashMap存储顺序LinkedHashMap结合了哈希表和链表的优势,既保证了高效查找,又保留了元素的插入顺序。以下是LinkedHashMap的使用示例:
public class LinkedHashMapDemo { public static void main(String[] args) { LinkedHashMap map = new LinkedHashMap<>(); map.put("邓超", "孙俪"); map.put("李晨", "范冰冰"); map.put("刘德华", "朱丽倩"); Set > entrySet = map.entrySet(); for (Map.Entry entry : entrySet) { System.out.println(entry.getKey() + " " + entry.getValue()); } }} 需求:统计一个字符串中每个字符的出现次数。
实现步骤:
HashMap集合,键为字符,值为出现次数。代码实现:
public class MapTest { public static void main(String[] args) { System.out.println("请录入一个字符串:"); String line = new Scanner(System.in).nextLine(); findChar(line); } private static void findChar(String line) { HashMap map = new HashMap<>(); for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (!map.containsKey(c)) { map.put(c, 1); } else { Integer count = map.get(c); map.put(c, count + 1); } } System.out.println(map); }} 转载地址:http://avux.baihongyu.com/