一、List集合遍历(有序、可重复)
(1)iterator迭代器遍历(推荐)
原因:iterator对象遍历不同的List类型,返回的iterator对象实现不同,访问效率较高
List<String> array = new ArrayList<String>();array.add("奥沙利文");array.add("丁俊晖");array.add("傅家俊");System.out.println("-------------迭代器-----------------------------");Iterator<String> 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迭代器遍历(推荐)
Set<String> set = new HashSet<String>();set.add("小明");set.add("小冯");set.add("小李");Iterator<String> 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)
HashMap<String, Integer> map = new HashMap<String, Integer>();map.put("马云", 23);map.put("马化腾", 22);map.put("肖总", 3);map.put("田总", 2);Set<String> s = map.keySet();for (String a : s) {System.out.println(a + "=" + map.get(a));}
(2)、增强for结合(Map.Entry)
Set<Entry<String, Integer>> a = map.entrySet();for (Entry<String, Integer> s1 : a) {String key = s1.getKey();Integer value = s1.getValue();System.out.println(key + "=" + value);}
(3)、迭代器遍历(Interator)
Set<Entry<String, Integer>> set = map.entrySet();Iterator<Entry<String, Integer>> iterable = set.iterator();while (iterable.hasNext()) {Entry<String, Integer> 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)判空操作遍历
Queue<String> q = new LinkedList<String>();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)迭代器遍历
Iterator<String> iterator = q.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}
五、Deque集合遍历
(1)两端可取元素,可添加元素
(2)继承Queue
public interface Deque<E> extends Queue<E> {
}
Deque<String> e = new LinkedList<String>();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<String> i = e.iterator();while (i.hasNext()) {System.out.println(i.next());}
六、Stack栈的遍历
后进先出Last in First out (LIFO)
把元素压栈:push()
把栈顶的元素弹出:pop()
显示栈顶的元素:peek()
Stack<String> s = new Stack<String>();s.push("A");s.push("B");s.push("F");s.push("D");// 迭代器遍历Iterator<String> 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());}