提到Map集合接口就不能不提到Collection集合接口,map和Collection都是集合接口,Collection中包含了我們經常用的list和set子接口;而Map是與Collection處于平級的地位;Collection中存儲的是一組對象,而Map存儲的是一個鍵值對(key/value).
在Map對象中,Key是唯一的,不可重復的。null也可以作為key,但這樣的key只能有一個;但是可以有一個或多個key所對應的value都是null。
當我們想判斷map中是否存在某個key時,可以用方法containsKey()來判斷,同樣想判斷是否存在value時用方法containsValue()來判斷;代碼如下:
public static void main(String[] args) { Map< Serializable, Serializable > map = new HashMap< Serializable, Serializable >(); map.put(null,null); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); Set<Entry<Serializable, Serializable>> entrySet= map.entrySet(); System.out.println("entrySet="+entrySet); for (Entry key : entrySet) { System.out.println("key.getKey="+key.getKey()+" key.getValue()="+ key.getValue()); } } 執(zhí)行的結果如下:
entrySet=[null=null,%20a=1,%20b=2,%20c=3]
key.getKey=null%20 key.getValue()=null
key.getKey=a%20 key.getValue()=1
key.getKey=b%20 key.getValue()=2
key.getKey=c%20 key.getValue()=3
%20 %20接下來要說的是keySet方法,keySet()方法返回值是Map中key值的集合,然后可以通過get(key)遍歷來獲取value值,代碼如下:
[java] view%20plain copy ![在CODE上查看代碼片]()
public static void main(String[] args) { Map< Serializable, Serializable > map = new HashMap< Serializable, Serializable >(); map.put(null,null); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); Set keySet= map.keySet(); System.out.println("keySet="+keySet); for (Iterator iterator = keySet.iterator(); iterator.hasNext();) { Object key = (Object) iterator.next(); Object value = map.get(key); System.out.println("key = "+key+ " value="+value); } } 執(zhí)行的結果如下:
keySet=[null,%20a,%20b,c]
key%20=%20null%20 value=null
key%20=%20a%20 value=1
key%20=%20b%20 value=2
key%20=%20c%20 value=3
%20 %20最后要說的是,map還有一個values()方法,values()方法返回值是Map中value值的集合,通過遍歷可以取出value的值,代碼如下:
[java] view%20plain copy ![在CODE上查看代碼片]()
public static void main(String[] args) { Map<Serializable, Serializable> map = new HashMap<Serializable, Serializable>(); map.put(null, null); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); Collection c = map.values(); System.out.println("map.values()=" + map.values()); for (Iterator iterator = c.iterator(); iterator.hasNext();) { Object value = (Object) iterator.next(); System.out.println("value="+value); } } 代碼執(zhí)行結果如下:map.values()=[null,1,%202,%203]
value=null
value=1
value=2
value=3
自己做的一個利用map進行hql語句拼接的小例子:
[java] view%20plain copy ![在CODE上查看代碼片]()
public class HqlMap { public static void main(String[] args) { Map<Serializable, Serializable> map = new HashMap<Serializable, Serializable>(); map.put("isDelete", 0); map.put("roomTypeName", "test"); Map<Serializable, Serializable> map1 = new HashMap<Serializable, Serializable>(); StringBuilder hqlBuilder = new StringBuilder(); hqlBuilder.append(" from Build "); String hql = "" ; if (map.isEmpty()) { hql=hqlBuilder.toString(); }else { hqlBuilder.append(" where "); Set keySet = map.keySet(); for (Iterator iterator = keySet.iterator(); iterator.hasNext();) { Object key = (Object) iterator.next(); hqlBuilder.append(key + "=:" + key + " and "); } //去掉最后的一個and int lastIndex = hqlBuilder.lastIndexOf("and"); if (lastIndex > -1) { hql = hqlBuilder.substring(0, lastIndex) + hqlBuilder.substring(lastIndex + 3, hqlBuilder.length()); } } System.out.println(hql); } } ![]()
小結
本文主要介紹了Map集合中entrySet()方法與keySet()、value()方法的使用,其中前兩者取出的都是key和value的映射關系,只有最后的values取出的是集合中所以的值,沒有鍵,也就沒有了對應的映射關系。
http://blog.csdn.net/zwk626542417/article/details/42290625