Map介面是雙列集合,它的每個元素都包含一個鍵物件key和值物件Value,鍵和值物件之間存在一個對應關係,稱為映射,從Map集合中存取元素時,只要指定了Key,就能找到對應的Value。
HashMap集合 HashMap集合是Map介面的實作類,它用於儲存鍵值映射關係,但必須確保不出現重複的鍵。void put(Object key,Object value)//將指定的值與對應中的指定鍵關聯
Object get(Object key) //傳回指定鍵所對應的值,如果此對應不包含該鍵的對應關係,則傳回null
boolean containsKey(Object key)//如果此對應包含指定鍵的對應關係,則傳回true
boolean containsValue(Object value)//如果此時對應將一個或多個鍵對應到指定值,則傳回true
Set keySet()//傳回此對應中所包含的值的Set Set## Collection< ;V>values()//傳回此對應中所包含的值的Collection檢視
Set>entrySet()//傳回此對應所包含的對應關係的Set檢視
package 集合类; import java.util.HashMap; import java.util.Map; public class Long { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); System.out.println("1:"+map.get("1")); System.out.println("2:"+map.get("2")); System.out.println("3:"+map.get("3")); } }
package 集合类; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Li { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Set keySet= (Set) map.keySet(); Iterator it= keySet.iterator; while(it.hasNext()){ Object key=it.next(); Object value=map.get( key); System.out.println(key+":"+value); } } }
package 集合类; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Xiao { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Set entrySet= (Set) map.entrySet(); Iterator it= entrySet.iterator; while(it.hasNext()){ Map.Entry entry=(Map.Entry) (it.next()); Object key=entry.getKey(); Object value=entry.getValue(); System.out.println(key+":"+value); } } }
package 集合类; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class LiXiao { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Collection values=map.values(); Iterator it=values.iterator(); while(it.hasnext()){ Object value=it.next(); System.out.println(value): } } }
import java.util.Map; public class Long { public static void main(String[] args) { Map map=new LinkedHashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Set keySet= (Set) map.keySet(); Iterator it= keySet.iterator; while(it.hasNext()){ Object key=it.next(); Object value=map.get( key); System.out.println(key+":"+value); } } }
package 集合类; import java.util.Enumeration; import java.util.Properties; public class Xiao { public static void main(String[] args) { Properties p=new Properties(); p.setProperty("Backgroup-color","red"); p.setProperty("Font-size","14px"); p.setProperty("Language","chinese"); Enumeration names=p.propertyNames(); while(names.hasMoreElements()){ String key=(String) names.nextElement(); String value=p.getProperty(key); System.out.println(key+"="+value); } } }
以上是Java Map集合使用實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!