HashMap是Java中最常用的Map接口的实现之一。它提供了一种快速的查找方式,并且插入和删除操作也比较高效。HashMap的内部实现是基于哈希表,它将键值对映射到一个固定长度的数组中。当需要查找一个特定的键值对时,首先需要计算哈希码,然后定位到对应的桶(bucket),最后在桶中查找对应的值。
一、HashMap简介
HashMap是Java中最常用的Map接口的实现之一。它允许存储键值对,并且用Key来取得Value,Key在HashMap中必须是独一无二的,Value则可以重复。Key和Value都可以为null。
HashMap的实现方法是采用哈希表,它是将Key映射到一个固定长度的数组中,这个数组就是哈希表的主空间。当需要查找一个特定的键值对时,首先需要计算哈希码,然后定位到对应的桶(bucket),最后在桶中查找对应的值。因为哈希码的范围往往大于数组的长度,所以可能会出现不同的键值对具有相同的哈希码的情况,这就是哈希冲突,HashMap使用链表来解决这个问题,即如果两个键值对具有相同的哈希码,就将它们存储在同一个桶中的链表中。
二、HashMap的常用操作
1. 创建HashMap
创建一个HashMap可以通过默认构造函数或者带有初始容量和负载因子的构造函数实现。如果不指定初始容量和负载因子,则使用默认值(初始容量为16,负载因子为0.75)。
Map<String,Integer> hashMap = new HashMap<>(); Map<String,Integer> hashMap = new HashMap<>(20); Map<String,Integer> hashMap = new HashMap<>(20, 0.8f);
2. 添加键值对
在HashMap中添加键值对可以使用put()方法。如果新的键值对的键已经存在,则会用新的值覆盖原有的值。
Map<String,Integer> hashMap = new HashMap<>(); hashMap.put("apple", 10); hashMap.put("orange", 20); hashMap.put("banana", 30);
3. 获取值
使用get()方法可以获取HashMap中指定键对应的值,如果键不存在,就返回null。
Map<String,Integer> hashMap = new HashMap<>(); hashMap.put("apple", 10); Integer value = hashMap.get("apple"); System.out.println(value); // 输出:10
4. 移除键值对
可以使用remove()方法来移除HashMap中指定键对应的键值对,如果键不存在,就不会有任何影响。
Map<String,Integer> hashMap = new HashMap<>(); hashMap.put("apple", 10); hashMap.put("orange", 20); hashMap.put("banana", 30); hashMap.remove("orange");
5. 判断键是否存在
可以使用containsKey()方法来判断HashMap中是否包含指定的键。
Map<String,Integer> hashMap = new HashMap<>(); hashMap.put("apple", 10); boolean contains = hashMap.containsKey("apple"); System.out.println(contains); // 输出:true
三、HashMap的使用
1. 存储学生成绩
以下是一个示例代码,用HashMap存储学生成绩。
Map<String,Double> scoreMap = new HashMap<>(); scoreMap.put("张三", 80.5); scoreMap.put("李四", 90.0); scoreMap.put("王五", 70.5); scoreMap.put("赵六", 95.0); System.out.println("张三的成绩是" + scoreMap.get("张三"));
输出结果为:
张三的成绩是80.5
2. 统计单词出现的次数
以下是一个示例代码,用HashMap统计一段英文文本中每个单词出现的次数。
String text = "Good morning. Have a good class. Have a good visit. Have fun!"; String[] words = text.toLowerCase().split("[^a-zA-Z]+"); Map<String,Integer> wordCountMap = new HashMap<>(); for (String word : words) { if (wordCountMap.containsKey(word)) { wordCountMap.put(word, wordCountMap.get(word) + 1); } else { wordCountMap.put(word, 1); } } for (Map.Entry<String,Integer> entry : wordCountMap.entrySet()) { System.out.println(entry.getKey() + "出现了" + entry.getValue() + "次"); }
输出结果为:
a出现了3次 class出现了1次 fun出现了1次 good出现了3次 have出现了3次 morning出现了1次 visit出现了1次
3. 实现LRU缓存
以下是一个示例代码,用HashMap实现一个LRU(least recently used)缓存,其中最多存储10个键值对,并且当缓存满时,清除最近最少使用的键值对。
class LRUMap<K,V> extends LinkedHashMap<K,V> { private final int maxSize; public LRUMap(int maxSize) { super(maxSize + 1, 1.0f, true); this.maxSize = maxSize; } @Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > maxSize; } } public static void main(String[] args) { Map<Integer,Integer> map = new LRUMap<>(10); for (int i = 0; i < 20; i++) { map.put(i, i); System.out.println("插入键" + i + ",当前map:" + map); } }
输出结果为:
插入键0,当前map:{0=0} 插入键1,当前map:{0=0, 1=1} 插入键2,当前map:{0=0, 1=1, 2=2} 插入键3,当前map:{0=0, 1=1, 2=2, 3=3} 插入键4,当前map:{0=0, 1=1, 2=2, 3=3, 4=4} 插入键5,当前map:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5} 插入键6,当前map:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6} 插入键7,当前map:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7} 插入键8,当前map:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8} 插入键9,当前map:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9} 插入键10,当前map:{1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10} 插入键11,当前map:{2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11} 插入键12,当前map:{3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11, 12=12} 插入键13,当前map:{4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11, 12=12, 13=13} 插入键14,当前map:{5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11, 12=12, 13=13, 14=14} 插入键15,当前map:{6=6, 7=7, 8=8, 9=9, 10=10, 11=11, 12=12, 13=13, 14=14, 15=15} 插入键16,当前map:{7=7, 8=8, 9=9, 10=10, 11=11, 12=12, 13=13, 14=14, 15=15, 16=16} 插入键17,当前map:{8=8, 9=9, 10=10, 11=11, 12=12, 13=13, 14=14, 15=15, 16=16, 17=17} 插入键18,当前map:{9=9, 10=10, 11=11, 12=12, 13=13, 14=14, 15=15, 16=16, 17=17, 18=18} 插入键19,当前map:{10=10, 11=11, 12=12, 13=13, 14=14, 15=15, 16=16, 17=17, 18=18, 19=19}
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/192884.html