package com.test; import java.awt.List; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * list集合遍历 set集合遍历 hashmap集合遍历 * * @author 18430 * */ public class Test { public static void main(String[] args) { Student student1 = new Student("zhangsan", 90); Student student2 = new Student("lisi", 78); Student student3 = new Student("wangwu", 56); ArrayList<Student> list = new ArrayList<Student>(); list.add(student1); list.add(student2); list.add(student3); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)); } System.out.println(); System.out.println("for循环遍历list================================================"); // Iterator iterator = list.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next()); } System.out.println(); System.out.println("迭代器遍历list================================================"); // for (Student student : list) { System.out.print(student); } System.out.println(); System.out.println("foreach遍历list================================================"); HashSet<Student> set = new HashSet<>(); set.add(student1); set.add(student2); set.add(student3); for (Student student : set) { System.out.print(student); } System.out.println(); System.out.println("foreach遍历set================================================"); Iterator iterator1 = set.iterator(); while (iterator1.hasNext()) { System.out.print(iterator1.next()); } System.out.println(); System.out.println("迭代器遍历set================================================"); HashMap map = new HashMap(); map.put("1", student1); map.put("2", student2); map.put("3", student3); Iterator iterator3 = map.keySet().iterator(); while (iterator3.hasNext()) { System.out.println(map.get(iterator3.next())); } System.out.println(); System.out.println("map集合的遍历================================================"); } }