一、List集合遍历 (有序、可重复)
(1)iterator迭代器遍历(推荐)
原因:iterator对象遍历不同的List类型,返回的iterator对象实现不同,访问效率较高
Listarray = new ArrayList (); array.add("奥沙利文"); array.add("丁俊晖"); array.add("傅家俊"); System.out.println("-------------迭代器-----------------------------"); Iterator it = array.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
(2)for循环遍历(不推荐)
原因:针对与List接口下的类访问效率不同
for (int i = 0; i < array.size(); i++) {
System.out.println(str.get(i));
}
(3)增强for循环遍历(for each循环)
原因:只要实现Iterable接口的集合类都可以直接用for each循环来遍历
for (String str : array) {
System.out.println(str);
}
二、Set集合遍历(无序、唯一)
(1)iterator迭代器遍历(推荐)
Setset = new HashSet (); set.add("小明"); set.add("小冯"); set.add("小李"); Iterator it = set.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
(2)增强for循环遍历(for each循环)
for(String s:set){
System.out.println(s);
}
三、Map集合遍历
以HashMap为例
底层:数组+链表+红黑树
key:Keyset() (set:无序、唯一)
value:values()(Collection集合)
(1)、增强for循环遍历(for each)
HashMapmap = new HashMap (); map.put("马云", 23); map.put("马化腾", 22); map.put("肖总", 3); map.put("田总", 2); Set s = map.keySet(); for (String a : s) { System.out.println(a + "=" + map.get(a)); }
(2)、增强for结合(Map.Entry)
Set> a = map.entrySet(); for (Entry s1 : a) { String key = s1.getKey(); Integer value = s1.getValue(); System.out.println(key + "=" + value); }
(3)、迭代器遍历(Interator)
Set> set = map.entrySet(); Iterator > iterable = set.iterator(); while (iterable.hasNext()) { Entry e = iterable.next(); System.out.println(e.getKey() + "=" + e.getValue()); }
四、Queue集合遍历
1.Collection(List、Queue、Set)
2.遵循先入先出(FIFO:First In First Out)
3.LinkedList集合(无界)和ArrayBlockingQueue(有界)都实现了Queue接口
4.只能进行添加元素至尾部、从队列头部取元素两个操作
5.将元素添加到队尾:offer()/add()
将元素取出并从队列中删除:remove()/poll()
将元素取出并从队列中但不删除:element()/peek()
(1)判空操作遍历
Queueq = new LinkedList (); q.offer("A"); q.offer("B"); q.offer("C"); q.offer("D"); while (!q.isEmpty()) { System.out.println(q.poll()); }
(2)q.peek()==null (取完后为null)
while (q.peek() != null) {
System.out.println(q.poll());
}
(3)迭代器遍历
Iteratoriterator = q.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
五、Deque集合遍历
(1)两端可取元素,可添加元素
(2)继承Queue
public interface Deque
extends Queue {
}
Dequee = new LinkedList (); e.offer("A"); e.offer("B"); e.offer("C"); e.offer("F"); e.offer("D"); System.out.println(e); //方式一 //从头到尾遍历 while (e.peekFirst() != null) { System.out.println(e.pollFirst()); } // 从尾到头遍历 while (e.peekLast() != null) { System.out.println(e.pollLast()); } //方式二 // 判空操作 // 头->尾 while (!e.isEmpty()) { System.out.println(e.pollFirst()); } // 尾->头 while (!e.isEmpty()) { System.out.println(e.pollLast()); } //方式三 // 迭代器遍历 Iterator i = e.iterator(); while (i.hasNext()) { System.out.println(i.next()); }
六、Stack栈的遍历
后进先出Last in First out (LIFO)
把元素压栈:push()
把栈顶的元素弹出:pop()
显示栈顶的元素:peek()
Stacks = new Stack (); s.push("A"); s.push("B"); s.push("F"); s.push("D"); // 迭代器遍历 Iterator i = s.iterator(); while (i.hasNext()) { System.out.println(i.next()); } // 判空操作 while (!s.isEmpty()) { System.out.println(s.pop()); } // 增强for循环遍历 for (String s1 : s) { System.out.println(); } //不健全会抛异常(EmptyStackException) while (s.peek() != null) { System.out.println(s.pop()); }



