[英]Mapping in java
我在Java中有一個地圖代碼,這是我的以下代碼。
Map<String, String> map = new HashMap<String, String>();
map.put("type", "One");
map.put("name", "Two");
map.put("setup", "Three");
System.out.println("Map Values Before: ");
Set<String> keys = map.keySet();
for (Iterator<String> i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = (String) map.get(key);
Map<String, String> map = new HashMap<String, String>();
}
在這里我能夠得到輸出。我的問題是我需要將每個鍵值匹配到單獨的字符串以在我的代碼中進一步使用。如何獲得每個鍵值在單獨的字符串中。請幫助我。
這種情況使您無法遍歷地圖鍵集。 但是,根據您的需要,Guava具有雙向地圖實現,在您的情況下可能會有用。
for(String key:map.keySet())
System.out.println("key: "+key+" value: "+map.get(key));
猜測一下您的意思,您想填充與鍵對應的變量
String type; // gets the value "One"
String name; // gets the value "Two"
String name; // gets the value "Three"
現在,我假設像
type = map.get("type");
是不可接受的,還是您肯定不會問這個問題,因此您需要某種編程方式來迭代屬性集?
在這種情況下,請查看反射API。
請確認這是您要解決的問題,並查看API,如果您還有其他問題需要跟進...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.