hashMap 转HashTable 有空直接抛异常 代码
时间: 2023-12-22 16:05:31 浏览: 27
可以使用以下代码将 HashMap 转换为 HashTable,如果 Map 中存在空值,则会抛出 NullPointerException 异常:
```
Map<K, V> map = new HashMap<>();
// 将 HashMap 转换为 HashTable
Hashtable<K, V> table = new Hashtable<>(map);
```
需要注意的是,Hashtable 不允许 null 值,如果 HashMap 中存在 null 值,转换时会抛出 NullPointerException 异常。如果需要将 HashMap 中的 null 值转换为非 null 值,可以在转换前进行遍历和处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)