首页 > 编程 > Java > 正文

Java 学习——Map

2019-11-08 00:08:39
字体:
来源:转载
供稿:网友

前言

     

    最近正在做的高校云平台项目中接触Map比较多,关于map的使用不是很熟悉,所以在此将map的几个方法再次学习下。

 

Map与Collection

 

    提到Map集合接口就不能不提到Collection集合接口,map和Collection都是集合接口,Collection中包含了我们经常用的list和set子接口;而Map是与Collection处于平级的地位;Collection中存储的是一组对象,而Map存储的是一个键值对(key/value). 

 

Map

 

    在Map对象中,Key是唯一的,不可重复的。null也可以作为key,但这样的key只能有一个;但是可以有一个或多个key所对应的value都是null。

 

    当我们想判断map中是否存在某个key时,可以用方法containsKey()来判断,同样想判断是否存在value时用方法containsValue()来判断;代码如下:

 

[java] view plain copy 在CODE上查看代码片<PRe name="code" class="java">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");          if (map.containsKey("a")) {              System.out.println("Key=Ture");              if (map.containsValue("1")) {                  System.out.println("value=Ture");              }          }            }  

执行结果是:

Key=Ture%20 value=Ture

 

 %20 %20Map中提供了一些常用的方法来取出Map中的数据,用的比较多的比如:entrySet()方法,;entrySet()的返回值是个Set集合,此集合的类型为Map.Entry。Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry<K,V>。它表示Map中的一个实体(一个key-value对)。接口中有getKey(),getValue方法,代码如下:

 

[java] view%20plain copy 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());      }       }  执行的结果如下:

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 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);          }      }  执行的结果如下:

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 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);      }    }  代码执行结果如下:

map.values()=[null,1,%202,%203]

value=null

value=1

value=2

value=3

 

自己做的一个利用map进行hql语句拼接的小例子:

[java] view%20plain copy 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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表