值對的 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