值对的 Java 集合? (元组?) - A Java collection of value pairs? (tuples?)
问题:
I like how Java has a Map where you can define the types of each entry in the map, for example <String, Integer>
.我喜欢 Java 有一个 Map ,您可以在其中定义地图中每个条目的类型,例如<String, Integer>
。
What I'm looking for is a type of collection where each element in the collection is a pair of values.我正在寻找的是一种集合,其中集合中的每个元素都是一对值。 Each value in the pair can have its own type (like the String and Integer example above), which is defined at declaration time.该对中的每个值都可以有自己的类型(如上面的 String 和 Integer 示例),这是在声明时定义的。
The collection will maintain its given order and will not treat one of the values as a unique key (as in a map).该集合将保持其给定的顺序,并且不会将其中一个值视为唯一键(如在地图中)。
Essentially I want to be able to define an ARRAY of type <String,Integer>
or any other 2 types.本质上,我希望能够定义<String,Integer>
类型或任何其他 2 种类型的 ARRAY。
I realize that I can make a class with nothing but the 2 variables in it, but that seems overly verbose.我意识到我可以创建一个只有 2 个变量的类,但这似乎过于冗长。
I also realize that I could use a 2D array, but because of the different types I need to use, I'd have to make them arrays of OBJECT, and then I'd have to cast all the time.我也意识到我可以使用 2D 数组,但由于我需要使用不同的类型,我必须将它们设为 OBJECT 数组,然后我必须一直进行转换。
I only need to store pairs in the collection, so I only need two values per entry.我只需要在集合中存储对,所以每个条目只需要两个值。 Does something like this exist without going the class route?如果不走课堂路线,是否存在这样的事情? Thanks!谢谢!
解决方案:
参考一: http://stackoom.com/question/2BZz参考二: A Java collection of value pairs? (tuples?)
- 用 JavaScript 计算昨天的日期 - Calculate the date yesterday in JavaScript
- 允许保留 2 位小数 - Allow 2 decimal places in
- 在 Android 上防止屏幕旋转 - Prevent screen rotation on Android
- 如何更新 RecyclerView 适配器数据 - How to update RecyclerView Adapter Data
- 日历重复/重复事件 - 最佳存储方法 - Calendar Recurring/Repeating Events - Best Storage Method
- (13:权限被拒绝)连接到上游时:[nginx] - (13: Permission denied) while connecting to upstream:[nginx]
- 如何使用 git 检查 GitHub 拉取请求? - How can I check out a GitHub pull request with git?
- 正则表达式:删除包含“帮助”等的行 - Regex: Remove lines containing “help”, etc
- >>>和>>之间的区别 - Difference between >>> and >>
- JavaScript 中反引号 (`) 的用法 - Usage of the backtick character (`) in JavaScript
- C 模板typedef - C template typedef
- 仅添加非空白更改 - Add only non-whitespace changes
- Python中exit()和sys.exit()之间的区别 - Difference between exit() and sys.exit() in Python
- Git:仅列出“未跟踪”的文件(还有自定义命令) - Git: list only “untracked” files (also, custom commands)
- 谷歌使用什么数据库? - What database does Google use?
- 在CLR中投放与使用'as'关键字 - Casting vs using the 'as' keyword in the CLR
- 值对的 Java 集合? (元组?) - A Java collection of value pairs? (tuples?)
- 'git 责备' 有什么作用? - What does 'git blame' do?
- 蚂蚁警告:“'includeantruntime'未设置” - ant warning: “'includeantruntime' was not set”
- 如何在不等待的情况下安全地调用 C# 中的异步方法 - How to safely call an async method in C# without await