P162-163考察HashSet集合import java.util.HashSet; import java.util.Set; class Grain { private String name; public Grain(String name) { this.name name; } public String getName() { return name; } Override public boolean equals(Object o) { if (this o) return true; if (o null || getClass() ! o.getClass()) return false; Grain grain (Grain) o; return name.equals(grain.name); } Override public int hashCode() { return name.hashCode(); } public void show() { } } public class HashSetExam { public static void main(String[] args) { SetGrain grainSet new HashSet(); grainSet.add(new Grain(Wheat)); grainSet.add(new Grain(Rice)); grainSet.add(new Grain(Maize)); // 打印所有粮食 for (Grain grain : grainSet) { System.out.println(grain.getName()); } } }P163考察TreeSet集合import java.util.*; public class TestTreeSet { public static void main(String[] args) { Set ts new TreeSet(); ts.add(长粒米); ts.add(稻花香); ts.add(珍珠米); System.out.println(ts); } }P164考察Comparable接口的compareTo方法P166考察Iterator迭代器的使用import java.util.*; import com.dongsheng.farmproduce.*; class GrainView { public static void main(String[] args) { // 创建 HashSet 集合用于存放车辆 Set grainSet new HashSet(); // 创建 2 个大米对象、2 个小麦对象并加入到 HashSet 集合中 Grain r1 new Rice(长粒米,五常); Grain r2 new Rice(珍珠米,金龙鱼); Grain w1 new Wheat(烟农 999,安徽); Grain w2 new Wheat(丰德存麦 20,河南); grainSet.add(r1); grainSet.add(r2); grainSet.add(w1); grainSet.add(w2); // 使用迭代器循环输出 Iterator it grainSet.iterator(); while (it.hasNext()) { System.out.println(*** 显示集合中元素信息 ***); Object obj it.next(); if(obj instanceof Rice) { Rice rice (Rice)obj; // 调用 Rice 类的特有方法 getBrand() System.out.println(大米品牌为: rice.getBrand()); }else{ Wheat wht (Wheat) obj; // 调用 Wheat 类的特有方法 getMarket() System.out.println(小麦销售市场为: wht.getClass()); } // 调用 Grain 类方法 show() ((Grain)obj).show(); } } }P168考察增强for循环import java.util.*; public class ForEachLoop { public static void main(String[] args) { String[] names {西农 511,酒春 7 号,邯麦 19,克春 14 号}; // 传统 for 循环遍历 for(int i 0;i names.length;i){ System.out.println(names[i]); } // 增强 for 循环遍历 for(String elem:names){ System.out.println(elem); } Set wheatSet new HashSet(); wheatSet.add(西农 511); wheatSet.add(酒春 7 号); wheatSet.add(邯麦 19); wheatSet.add(克春 14 号); // 迭代器遍历 Iterator it wheatSet.iterator(); while (it.hasNext()){ System.out.println(it.next()); } // 增强 for 循环遍历 for(Object elem:wheatSet){ System.out.println((String)elem); } } }P170考察ArrayListP174、P175两块相对大一些的代码考察泛型