HashMap的使用方法及注意事项
2013-09-24 21:00
411 查看
99、Map(映射):Map 的keySet()方法会返回 key 的集合,因为 Map 的键是不能重复的,因此 keySet()方法的返回类型是 Set;而 Map 的值是可以重复的,因此 values()方法的返回类型是 Collection,可以容纳重复的元素。
100、Map注意事项:
A)
由上述代码可知键不可以重复。
B)
由上述代码可知值可以重复。
注:因此 keySet()方法的返回类型是 Set,values()方法的返回类型是 Collection。
101、关于HashSet的使用:
A)
b)
附Map.Entry的API简介:
Map.entrySet方法返回映射的 collection视图,其中的元素属于此类。获得映射项引用的唯一方法是通过此
collection视图的迭代器来实现。这些Map.Entry对象仅在迭代期间有效;更确切地讲,如果在迭代器返回项之后修改了底层映射,则某些映射项的行为是不确定的,除了通过setValue在映射项上执行操作之外。
C)通过使用HashSet判断输入值的个数
=====================关于HashSet其实运行下这些代码,参考着API是很容易理解的=============
100、Map注意事项:
A)
public class TestMap { publicstatic void main(String[] args) { HashMap map = new HashMap(); map.put("a","zhangsan"); map.put("b","lisi"); map.put("c","wangwu"); map.put("a","cuijun"); //System.out.println(map); String value = (String)map.get("a"); System.out.println(value); System.out.println("---------------------"); Stringvalue1 = (String)map.get("d"); System.out.println(value1); } }
由上述代码可知键不可以重复。
B)
public classTestMap1 { publicstatic void main(String[] args) { HashMap map = new HashMap(); String str = newString("cuijun"); map.put("a",str); map.put("b",str); //map.put("a", "cuijun"); //map.put("b", "cuijun"); System.out.println(map); } }
由上述代码可知值可以重复。
注:因此 keySet()方法的返回类型是 Set,values()方法的返回类型是 Collection。
101、关于HashSet的使用:
A)
public class TestMap2 { publicstatic void main(String[] args) { HashMap map = new HashMap(); map.put("a","aa"); map.put("b","bb"); map.put("c","cc"); map.put("d","dd"); Set keys = map.keySet();//使用keySet()方法,获取键的集合 for(Iterator it =keys.iterator(); it.hasNext();){ String key = (String)it.next(); String value = (String)map.get(key); System.out.println(key+"="+value); } } }
b)
publicclass TestMap3 { publicstatic void main(String[] args) { HashMap map = new HashMap(); map.put("a","aa"); map.put("b","bb"); map.put("c","cc"); map.put("d","dd"); Set keys = map.entrySet();//使用entrySet()方法,获取entry对象的集合 for(Iterator it =keys.iterator(); it.hasNext();){ Map.Entry maps = (Map.Entry)it.next();//Map.Entry:Entry是Map的一个内部类。具体详情请参考API。 /* * 关于内部类的简单介绍:内部类是定义在类内部的类 * public class A{ * publicclass B{} * } * A a = new A(); * A.B b = new A.B(); */ String key = (String)maps.getKey(); String value = (String)maps.getValue(); System.out.println(key+":"+value); } } }
附Map.Entry的API简介:
Map.entrySet方法返回映射的 collection视图,其中的元素属于此类。获得映射项引用的唯一方法是通过此
collection视图的迭代器来实现。这些Map.Entry对象仅在迭代期间有效;更确切地讲,如果在迭代器返回项之后修改了底层映射,则某些映射项的行为是不确定的,除了通过setValue在映射项上执行操作之外。
C)通过使用HashSet判断输入值的个数
publicclassTestMap4 { publicstatic void main(String[] args) { HashMap map = new HashMap(); for(int i = 0; i < args.length; i++){ if(map.get(args[i]) ==null){ map.put(args[i],newInteger(1)); }else{ Integer value = (Integer)map.get(args[i]); value = new Integer(value.intValue()+1); map.put(args[i],value); } } Set keys = map.keySet(); for(Iterator it =keys.iterator(); it.hasNext();){ String key = (String)it.next(); Integer value = (Integer)map.get(key); System.out.println(key+"="+value); } } }
=====================关于HashSet其实运行下这些代码,参考着API是很容易理解的=============
相关文章推荐
- HashMap的使用方法及注意事项
- 使用eclipse 开发 嵌入式Linux驱动(注意事项)(make:***没有规则可言创建目标"all".停止 的解决方法)
- java.util.List接口的方法subList()的使用注意事项
- 华为 mas V4.1D3API 使用方法及注意事项
- fgets()函数的使用方法及注意事项(笔记)
- jdom中使用setAttribute(Attribute attribute)方法设置属性的注意事项
- EGOImageView的使用方法及注意事项
- pthread-win32库编译及使用方法注意事项
- SVN代码管理使用方法和注意事项
- 使用ES5 reduce()方法的一点小注意事项
- PHP中$_FILES的使用方法及注意事项说明
- 注释的使用方法及注意事项和汇编语言的使用
- 分页解决方案 之 分页算法——Pager_SQL的详细使用方法和注意事项
- document.write方法使用注意事项
- Swift中KVO(监听)的使用方法及注意事项
- mysql中使用存储过程方法中的注意事项
- equals()方法与“==”的使用以及注意事项
- Android使用Parcelable传递对象方法及注意事项
- new与delete函数使用方法以及注意事项
- ServletContext.getRealPath() 使用方法及注意事项