从Java5起,在Java中有了for-each循环,可以用来循环遍历collection和array.Foreach循环允许你在无需保持传统for循环中的索引,或在使用iterator /ListIterator(ArrayList中的一种迭代器实现)时无需调用while循环中的hasNext()方法就能遍历collection.for-each循环简化了任何Collection或array的遍历过程.但是使用foreach循环也有两点需要注意. 使用foreach循环的对象,必须实现了Ite…
迭代器来遍历 : entrySet() ; keySet(); values(); eg.HashMap<String,String> map = new HashMap<String,String>(); map.put("zizi", "ZZ"); map.put("lili", "LL"); map.put("cici", "CC"); map.put(&q…
Map集合的特点 1.Map集合中保存的都是键值对,键和值是一一对应的 2.一个映射不能包含重复的值 3.每个键最多只能映射到一个值上 Map接口和Collection接口的不同 Map是双列集合的根接口,Collection是单列集合的根接口 1.Map是双列的(是双列集合的根接口),Collection是单列的(是单列集合的根接口) 2.Map的键是唯一的,Collection的子接口Set是唯一的 3.Map集合的数据结构值针对键有效,跟值无关:如:TreeMap:键是用二叉树算法,Has…
遍历List集合的三种方法 List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); 方法一: 超级for循环遍历 for(String attribute : list) { System.out.println(attribute); } 方法二: 对于ArrayList来说速度比较快, 用for循环, 以size为条件遍历: for(int…
集合(续) 集合间的操作 集合提供了如取并集,删交集,判断包含子集等操作 package collection; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; /** * 集合间的操作 */ public class CollectionDemo4 { public static void main(String[] args) { // Collection c1 = new…
set的三种遍历方式,set遍历元素 list 遍历元素 http://blog.csdn.net/sunrainamazing/article/details/71577662 set遍历元素 http://blog.csdn.net/sunrainamazing/article/details/71577893 map遍历元素 http://blog.csdn.net/sunrainamazing/article/details/71580051 package sun.rain.amazi…
Map的遍历 1.通过map.entrySet遍历Key和Value Map<Integer,Integer> map = new HashMap<>(); map.put(1, 10); map.put(2, 8); for(Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + " ---- " + entry.getVal…
遍历ArrayList的三种方法 步骤 1 : 用for循环遍历 通过前面的学习,知道了可以用size()和get()分别得到大小,和获取指定位置的元素,结合for循环就可以遍历出ArrayList的内容 package collection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import charactor.Hero; public class TestCollectio…
Collections工具类之sort方法 * 使用Collections工具类对List集合进行排序 Collections.sort(List集合) * Collections.sort()方法只能对List集合进行排序. * 代码: ``` = import sun.rmi.log.LogInputStream; import java.sql.ClientInfoStatus; import java.util.*; public class CollectionsUtilTest01…
List集合在Java日常开发中是必不可少的,只要懂得运用各种各样的方法就可以大大提高我们开发的效率,适当活用各种方法才会使我们开发事半功倍. 我总结了三种List集合的遍历方式,下面一一来介绍. 首先来创建一个实体类,以供下面使用. public class News{ private int id; private String title; private String author; public News(int id, String title, String author) { s…