深圳专业网站建设免费维护送域名空间,网站建设与管期末试题,wordpress虚拟币接口,做医药中间体的外贸网站集合框架_HashMap
一、概述
HashMap 是用于存储 Key-Value 键值对的集合。 #xff08;1#xff09;HashMap 根据键的 hashCode 值存储数据#xff0c;大多数情况下可以直接定位到它的值#xff0c;所以具有很快的访问速度#xff0c;但遍历顺序不确定。 #xff08;21HashMap 根据键的 hashCode 值存储数据大多数情况下可以直接定位到它的值所以具有很快的访问速度但遍历顺序不确定。 2 HashMap 中键 key 为 null 的记录至多只允许一条值 value 为 null 的记录可以有多条。 3 HashMap 非线程安全即任一时刻允许多个线程同时写 HashMap可能会导致数据的不一致。
二、特点
HashMap的特点主要有以下几点
线程不安全HashMap不是线程安全的容器不适用于多线程环境。如果需要在多线程环境下使用Map可以考虑使用ConcurrentHashMap或者通过加锁等方式来保证线程安全。哈希表实现HashMap使用哈希表Hash Table实现它通过散列函数将键映射到哈希表的索引位置上从而实现了快速的插入、删除和查找操作。元素无序HashMap并不保证元素的顺序元素在哈希表中的位置取决于键的哈希值和哈希表的容量。键不重复HashMap中的键是不允许重复的如果添加一个键值对时发现键已经存在则会覆盖原有的值。可以存放nullHashMap中的键和值都可以存放null但是需要注意并发情况下的线程安全性。性能高效由于使用了哈希表实现HashMap的插入、删除和查找操作的时间复杂度都接近常数级别因此具有高效的性能。
总之HashMap是一种常用的数据结构其主要特点是高效、无序、键不重复但不具备线程安全性。在使用时需要根据具体应用场景进行选择和设计。
三 使用示例
HashMap 类位于 java.util 包中使用前需要引入它语法格式如下
import java.util.HashMap; // 引入 HashMap 类以下实例我们创建一个 HashMap 对象 Sites 整型Integer的 key 和字符串String类型的 value
HashMapInteger, String Sites new HashMapInteger, String();HashMap常用方法 1、put(K key, V value) 将键key/值value映射存放到Map集合中。
2、get(Object key) 返回指定键所映射的值没有该key对应的值则返回 null。
3、size() 返回Map集合中数据数量。
4、clear() 清空Map集合。
5、isEmpty() 判断Map集合中是否有数据如果没有则返回true否则返回false。
6、remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值。
7、values() 返回Map集合中所有value组成的以Collection数据类型格式数据。
8、containsKey(Object key) 判断集合中是否包含指定键包含返回 true否则返回false。
9、containsValue(Object value) 判断集合中是否包含指定值包含返回 true否则返回false。
10、keySet() 返回Map集合中所有key组成的Set集合。
11、entrySet() 将Map集合每个key-value转换为一个Entry对象并返回由所有的Entry对象组成的Set集合。
1 添加元素
1.1 HashMap 类提供了很多有用的方法添加键值对(key-value)可以使用 put() 方法:
实例
// 引入 HashMap 类
import java.util.HashMap;public class worldTest {public static void main(String[] args) {// 创建 HashMap 对象 SitesHashMapInteger, String Sites new HashMapInteger, String();// 添加键值对Sites.put(1, hello);Sites.put(2, world);Sites.put(3, hi);Sites.put(4, china);System.out.println(Sites);}
}1.2 以下实例创建一个字符串String类型的 key 和字符串String类型的 value
实例
// 引入 HashMap 类
import java.util.HashMap;public class worldTest {public static void main(String[] args) {// 创建 HashMap 对象 SitesHashMapString, String Sites new HashMapString, String();// 添加键值对Sites.put(one, hello);Sites.put(two, world);Sites.put(three, hi);Sites.put(four, china);System.out.println(Sites);}
}2 访问元素
我们可以使用 get(key) 方法来获取 key 对应的 value:
实例
// 引入 HashMap 类
import java.util.HashMap;public class worldTest {public static void main(String[] args) {// 创建 HashMap 对象 SitesHashMapInteger, String Sites new HashMapInteger, String();// 添加键值对Sites.put(1, hello);Sites.put(2, world);Sites.put(3, hi);Sites.put(4, china);System.out.println(Sites.get(3));}
}3 删除元素
3.1 我们可以使用 remove(key) 方法来删除 key 对应的键值对(key-value):
实例
// 引入 HashMap 类
import java.util.HashMap;public class worldTest {public static void main(String[] args) {// 创建 HashMap 对象 SitesHashMapInteger, String Sites new HashMapInteger, String();// 添加键值对Sites.put(1, hello);Sites.put(2, world);Sites.put(3, hi);Sites.put(4, china);Sites.remove(4);System.out.println(Sites);}
}3.2 删除所有键值对(key-value)可以使用 clear 方法
实例
// 引入 HashMap 类
import java.util.HashMap;public class worldTest {public static void main(String[] args) {// 创建 HashMap 对象 SitesHashMapInteger, String Sites new HashMapInteger, String();// 添加键值对Sites.put(1, hello);Sites.put(2, world);Sites.put(3, hi);Sites.put(4, china);Sites.clear();System.out.println(Sites);}
}4 计算大小
如果要计算 HashMap 中的元素数量可以使用 size() 方法
实例
// 引入 HashMap 类
import java.util.HashMap;public class worldTest {public static void main(String[] args) {// 创建 HashMap 对象 SitesHashMapInteger, String Sites new HashMapInteger, String();// 添加键值对Sites.put(1, hello);Sites.put(2, world);Sites.put(3, hi);Sites.put(4, china);System.out.println(Sites.size());}
}5 迭代 HashMap
可以使用 for-each 来迭代 HashMap 中的元素。
如果你只想获取 key可以使用 keySet() 方法然后可以通过 get(key) 获取对应的 value如果你只想获取 value可以使用 values() 方法。
实例
// 引入 HashMap 类
import java.util.HashMap;public class worldTest {public static void main(String[] args) {// 创建 HashMap 对象 SitesHashMapInteger, String Sites new HashMapInteger, String();// 添加键值对Sites.put(1, hello);Sites.put(2, world);Sites.put(3, hi);Sites.put(4, china);// 输出 key 和 valuefor (Integer i : Sites.keySet()) {System.out.println(key: i value: Sites.get(i));}// 返回所有 value 值for(String value: Sites.values()) {// 输出每一个valueSystem.out.print(value , );}}
}6 复制一份 hashMap
方法clone
实例
public static void main(String[] args) {HashMapString, String map new HashMap();map.put(A,1);map.put(B,2);map.put(C,3);Object clone map.clone();System.out.println(原来的map);System.out.println(复制一份clone);
}7 判断 hashMap 是否为空
方法isEmpty
实例
public static void main(String[] args) {HashMapString, String map new HashMap();HashMapString, String map1 new HashMap();map.put(A,1);map.put(B,2);map.put(C,3);boolean empty1 map.isEmpty();boolean empty2 map1.isEmpty();System.out.println(empty1);System.out.println(empty2);
}
8 替换 hashMap 中是指定的 key 对应的 value
方法replace
实例
public static void main(String[] args) {HashMapString, String map new HashMap();HashMapString, String map1 new HashMap();map.put(A,1);map.put(B,2);map.put(C,3);System.out.println(替换前----map);map.replace(A, 100);System.out.println(替换后----map);
}