Java集合的总结

来源:互联网 发布:启凡网络 编辑:程序博客网 时间:2024/05/19 19:32
//list集合的遍历List<String> list = new ArrayList<String>();list.add("1");list.add("2");list.add("3");//方法1:Iterator it_list = list.iterator();while (it_list.hasNext()){    String result = it_list.next().toString();    System.out.println(result);}//方法2:for (int i=0; i<list.size(); i++){    String result = list.get(i);    System.out.println(result);}//方法3:for (String result:list) {    System.out.println(result);}
//set集合遍历Set<String> set = new HashSet<String>();set.add("1");set.add("2");set.add("3");//方法1:Iterator<String> it_set = set.iterator();while (it_set.hasNext()){    String result = it_set.next();    System.out.println(result);}//方法2:for (String result:set) {    System.out.println(result);}
//map遍历Map<String, String> map = new HashMap<String, String>();map.put("1","a");map.put("2","b");map.put("3","c");//方法1:for (String key : map.keySet()) {    System.out.println("key= "+ key + " and value= " + map.get(key));}//方法2:Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();while (it.hasNext()) {    Map.Entry<String, String> entry = it.next();    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}//方法3:推荐,尤其是容量大时for (Map.Entry<String, String> entry : map.entrySet()) {    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}
图例说明:
类图中,实线边框的是实现类,比如ArrayList,LinkedList,HashMap等,折线边框的是抽象类,比如AbstractCollection,AbstractList,AbstractMap等,而点线边框的是接口,比如Collection,Iterator,List等。
集合之间的比较:
0 0
原创粉丝点击