在我的應用程式中,我有一個 for 回圈,它從地圖中獲取資料并將其添加到一個新地圖中,該地圖將包含來自其他資料源(幾個 enumClass)的更多資料。我這樣做:
private static Map<String, String> myMapConversion() {
for (myEnunClassKeyOne key : document.keySet()) {
String keyObteined = key.toString();
String value = document.get(key);
globalConfig.put(convertKey(keyToConversion), value); // Extension function get value of myEnunClassKeyOne key
}
return globalConfig;
}
我必須對其他 enumClasses 執行相同的操作,并將資料添加到同一個 globalConfig 映射中,如下所示:
private static Map<String, String> mapConversion() {
for (myEnunClassKeyONE key : document.keySet()) {
String keyObteined = key.toString();
String value = document.get(key);
globalConfig.put(convertKey(keyToConversion), value); // Extension function get value of myEnunClassKeyOne key
}
for (myEnunClassKeyTWO key : document.keySet()) {
String keyObteined = key.toString();
String value = document.get(key);
globalConfig.put(convertKey(keyToConversion), value); // Extension function get value of myEnunClassKeyOne key
}
for (myEnunClassKeyTHREE key : document.keySet()) {
String keyObteined = key.toString();
String value = document.get(key);
globalConfig.put(convertKey(keyToConversion), value); // Extension function get value of myEnunClassKeyOne key
}
return globalConfig;
}
重復 for 回圈對我來說似乎“不雅”。你能想出一種更自動化的方法嗎?
我嘗試了幾種方法,但無法使其正常作業。它必須是 java 代碼,盡管可以接受 Kotlin 擴展函式
提前致謝
---根據答案編輯---
@Tenfour04 你很善良。
我的問題是 document1、document2 等的型別未經驗證,我不知道如何正確輸入。我包括更多細節:
enum class Document1(val currentKey: String) {
color1("blue"),
color1("red"),
color1("green"),
color1("cyan"),
color1("yellow"),
color_show("yes"),
...
}
enum class Document2(val currentKey: String) {
word1("hello"),
word1("sky"),
word1("orange"),
word1("smile"),
word1("beauty"),
word_show("yes"),
...
}
因此,當我在“地圖檔案”中包含 Document1、Document2 等時,會出現必需的型別錯誤
...
Map<?, ?>[] documents = { Document1, Document2, Document3** };
...
uj5u.com熱心網友回復:
在 Java 中,您可以創建專案串列并對其進行迭代。我假設這些實際上是三個不同的檔案。
private static Map<String, String> mapConversion() {
List<Map<?, String>> documents = List.of(document1, document2, document3);
for (Map<?, String> document : documents) {
for (Map.Entry<?, String> entry : document.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue();
globalConfig.put(convertKey(keyToConversion), value);
}
}
return globalConfig;
}
在 Kotlin 中,您可以更簡潔地使用mapKeysTo:
fun mapConversion(): MutableMap<String, String> {
listOf(document1, document2, document3).forEach {
it.mapKeysTo(globalConifg) { (key, _) -> convertKey(key.toString()) }
}
return globalConfig
}
抱歉,我錯過了 Android 標簽。在 Java 9 之前,您可以使用陣列,但我認為您必須轉換回傳值:
private static Map<String, String> mapConversion() {
Map<?, ?>[] documents = { document1, document2, document3 };
for (Map<?, ?> document : documents) {
for (Map.Entry<?, String> entry : document.entrySet()) {
String key = entry.getKey().toString();
String value = (String)entry.getValue();
globalConfig.put(convertKey(keyToConversion), value);
}
}
return globalConfig;
}
由于它們是列舉定義而不是映射,因此您需要一種方法來對它們進行通用處理。一種方法是添加一種從它們中獲取地圖的方法。像這樣在每個列舉中:
enum class Document1(val currentKey: String) {
color1("blue"),
color1("red");
companion object {
@JvmStatic val valuesMap: Map<String, Document1>() =
values().associateBy(Document1::currentKey)
}
}
然后你可以使用地圖:
Map<?, ?>[] documents = { Document1.getValuesMap(), Document2.getValuesMap(), Document3.getValuesMap() };
In this case, the map values are the various enum types, so you'll maybe have to convert them to Strings with toString() to put them in your global map.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456873.html
