C# Java HashMap equivalent(C# Java HashMap 等价物)
本文介绍了C# Java HashMap 等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从 Java 世界进入 C# 世界是否存在等效的 HashMap?如果没有,你会推荐什么?
Coming from a Java world into a C# one is there a HashMap equivalent? If not what would you recommend?
推荐答案
Dictionary 可能是最接近的.System.Collections.Generic.Dictionary 实现 System.Collections.Generic.IDictionary 接口(类似于 Java 的 Map 接口).
您应该注意的一些显着差异:
Some notable differences that you should be aware of:
- 添加/获取项目
- Java 的 HashMap 具有用于设置/获取项目的
put和get方法myMap.put(key, value)MyObject 值 = myMap.get(key)
- Adding/Getting items
- Java's HashMap has the
putandgetmethods for setting/getting itemsmyMap.put(key, value)MyObject value = myMap.get(key)
myDictionary[key] = valueMyObject 值 = myDictionary[key]
- Java 的
HashMap允许空键 如果您尝试添加空键, - .NET 的
Dictionary会抛出ArgumentNullException
- Java's
HashMapallows null keys - .NET's
Dictionarythrows anArgumentNullExceptionif you try to add a null key
- Java 的
HashMap会将现有值替换为新值. 如果您使用 - .NET 的
Dictionary将用新值替换现有值.如果您使用Add方法,它会改为抛出ArgumentException.
[]索引,- Java's
HashMapwill replace the existing value with the new one. - .NET's
Dictionarywill replace the existing value with the new one if you use[]indexing. If you use theAddmethod, it will instead throw anArgumentException.
- Java 的
HashMap将返回 null. - .NET 的
Dictionary将抛出KeyNotFoundException.您可以使用TryGetValue方法而不是[]索引以避免这种情况:MyObject 值 = null;if (!myDictionary.TryGetValue(key, out value)) {/* 键不存在 *
- Java's HashMap has the
- Java 的 HashMap 具有用于设置/获取项目的
编程基础网
本文标题为:C# Java HashMap 等价物
基础教程推荐
猜你喜欢
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
