HashMap的使用方法及注意事项
2013-09-25 19:56
971 查看
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的使用方法及注意事项
- iPhone SDK中多线程的使用方法以及注意事项
- 嵌入式开发中网络性能测试工具iperf的使用方法和注意事项
- cygwin跨平台移植 gcc+vc联合使用的方法和注意事项
- 安卓中使用ArrayAdapter中的remove方法注意事项
- jquery animate()方法使用的注意事项
- TreeSet的使用方法和注意事项
- Android使用Parcelable传递对象方法及注意事项
- 方法执行[置顶] onPause()和onStop()的使用方法及注意事项
- 关于PHP面向对象编程中使用范围解析操作符访问非静态方法的注意事项
- 类方法使用注意事项
- PHP使用静态方法的几个注意事项
- C++中typename关键字的使用方法和注意事项
- Java构造器使用方法及注意事项
- Lambda表达式Contains方法(等价于SQL语句中的like)使用注意事项
- EGOImageView的使用方法及注意事项
- PRTG流量监控基本安装和使用方法及注意事项
- java.util.List接口的方法subList()的使用注意事项
- cookie的使用方法和注意事项
- 为什么用group by 以及使用的方法和注意事项