public class Province {private String name;//省份名称private long area;//土地面积private boolean general;//是普通省份还是特殊省份public String getName() {return name;}public void setName(String name) {this.name = name;}public long getArea() {return area;}public void setArea(long area) {this.area = area;}public void setGeneral(boolean general) {this.general = general;}public boolean isGeneral() {return general;}
}
管理类
import java.util.LinkedHashSet;
public class ProvinceManager {//添加省份到set集合private LinkedHashSet provinceSet;public LinkedHashSet getProvinceSet() {return provinceSet;}public void setProvinceSet(LinkedHashSet provinceSet) {this.provinceSet = provinceSet;}public void addProvince(Province pro) {provinceSet.add(pro);}//根据给定省份显示省份信息public Province findProvinceByname(String name) {for (Object o : provinceSet) {if (((Province) o).getName().equals(name)) {return (Province) o;}}return null;}//显示省份信息public void displayAll(){for(Object o:provinceSet){System.out.println(((Province) o).getName()+"\t"+((Province) o).getArea()+"\t"+((Province) o).isGeneral());}}public ProvinceManager() {if (provinceSet == null) {provinceSet = new LinkedHashSet();}}
}
测试类
import java.util.Scanner;//linkhashset
public class test64 {public static void main(String[] args){Scanner in=new Scanner(System.in);System.out.println("请输入省份的信息3个");ProvinceManager pm=new ProvinceManager();for(int i=0;i<=3;i++){System.out.println("请输入第"+i+"个省份信息");System.out.println("请输入省份名称");String name=in.next();System.out.println("请输入土地面积");long area=in.nextLong();System.out.println("直辖市输入0普通输入1");int tag=in.nextInt();Province pro=new Province();pro.setName(name);pro.setArea(area);if(tag==0)pro.setGeneral(false);elsepro.setGeneral(true);pm.addProvince(pro);}System.out.println("所有省份信息如下:");pm.displayAll();//显示所有省份的信息System.out.println("请输入要查找的省份名称");String proName=in.next();Province target=pm.findProvinceByname(proName);if(target==null){System.out.println("没有找到");}else{System.out.println(target.getName()+target.getArea()+target.isGeneral());}}
}
运行结果