速学堂(java)第九章编程题答案(自写)
1. 使用List和Map存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key。
package cn.sxt.jiu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 使用List和Map存放多个图书信息,遍历并输出。
* 其中商品属性:编号,名称,单价,出版社;使用
* 商品编号作为Map中的key。
* @author ASUS
*
*/
public class lianxi01 {
public static void main(String[] args) {
listStorage();//将图书信息用List存储
System.out.println("------------------------------------");//分隔线
mapStorage();//将图书信息用Map进行存储
}
//将图书信息用List存储
public static void listStorage(){
System.out.println("采用List存放图书信息如下:");
//创建List容器
List<Book>book = new ArrayList<>();
book.add(new Book(1001,"百年孤独",58,"人民出版社"));
book.add(new Book(1002,"城市发展史",86,"清华出版社"));
book.add(new Book(1003,"美国大城市的死与生",75,"中国人民大学出版社"));
//用增强for循环进行遍历输出
for(Book temp:book){
System.out.println(temp);
}
}
//将图书信息用Map进行存储
public static void mapStorage(){
System.out.println("采用Map存放图书信息如下:");
//创建Map容器,将商品编号作为Map中的key
Map<Integer,Book> book = new HashMap<>();
book.put(1001, new Book(1001,"百年孤独",58,"人民出版社"));
book.put(1002, new Book(1002,"城市发展史",86,"清华出版社"));
book.put(1003, new Book(1003,"美国大城市的死与生",75,"中国人民大学出版社"));
//将book中的键值返回到keyset集合中
Set<Integer>keyset = book.keySet();
//使用增强for循环进行遍历输出
for(Integer key:keyset){
System.out.println(key+"--------"+book.get(key));
}
}
}
//图书类
class Book{
private int id; //编号
private String name; //名称
private double price;//单价
private String press;//出版社
//构造器
public Book(int id, String name, double price, String press) {
super();
this.id = id;
this.name = name;
this.price = price;
this.press = press;
}
//get,set方法
private int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
private String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
private double getPrice() {
return price;
}
private void setPrice(double price) {
this.price = price;
}
private String getPress() {
return press;
}
private void setPress(String press) {
this.press = press;
}
//重写toString方法
public String toString() {
return "id:"+id+" name:"+name+" price:"+price+" press:"+press;
}
}
运行结果截图:
2. 使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;要求向其中添加多个相同的商品,验证集合中元素的唯一性。
提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )。 向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较 规则。
package cn.sxt.jiu;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* 使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;
* 要求向其中添加多个相同的商品,验证集合中元素的唯一性。
* 提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )。
* 向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较 规则。
* @author ASUS
*
*/
public class lianxi02 {
public static void main(String[] args) {
System.out.println("图书信息用HashSet存储如下:");
hashsetStorage();//图书信息用HashSet存储
System.out.println("---------------------------------------------------");
System.out.println("图书信息TreeSet存储如下:");
treesetStorage();//图书信息用TreeSet存储
}
//图书信息用HashSet存储
public static void hashsetStorage(){
Set<Book1>set1 = new HashSet<>();
set1.add(new Book1(1001,"百年孤独",58,"人民出版社"));
set1.add(new Book1(1002,"城市发展史",86,"清华出版社"));
//重写了equals方法和HashCode方法,id相同第二个无法加入
set1.add(new Book1(1003,"美国大城市的死与生",75,"中国人民大学出版社"));
set1.add(new Book1(1003,"大城市的死与生",77,"大学出版社"));
//使用增强for循环遍历输出
for(Book1 temp:set1){
System.out.println(temp);
}
}
//图书信息用TreeSet存储
public static void treesetStorage(){
Set<Book1>set2 = new TreeSet();
set2.add(new Book1(1001,"百年孤独",58,"人民出版社"));
set2.add(new Book1(1002,"城市发展史",86,"清华出版社"));
//根据自定义比较,id相同,价格相同,第二个无法加入
set2.add(new Book1(1003,"美国大城市的死与生",75,"中国人民大学出版社"));
set2.add(new Book1(1003,"人间失格",75,"人民大学出版社"));
//id相同,价格不同,无影响
set2.add(new Book1(1005,"瞬变",100,"外国语出版社"));
set2.add(new Book1(1005,"瞬变",99,"外国语出版社"));
//id不同,价格相同,无影响
set2.add(new Book1(1006,"Java",100,"计算机出版社"));
set2.add(new Book1(1007,"Java",100,"计算机出版社"));
//使用增强for循环进行遍历输出
for(Book1 temp:set2){
System.out.println(temp);
}
}
}
class Book1 implements Comparable<Book1>{
int id; //编号
String name; //书名
double price; //价格
String press; //出版社
//构造器
public Book1(int id, String name, double price, String press) {
super();
this.id = id;
this.name = name;
this.price = price;
this.press = press;
}
//自定义比较
public int compareTo(Book1 o) {
//传入对象(o)的价格小于被比较对象的价格,return 1
if(this.price>o.price){
return 1;
}//传入对象(o)的价格小于被比较对象的价格,return -1
else if(this.price<o.price){
return -1;
}else{
//传入对象(o)的价格等于被比较对象的价格,则比较两者id
if(this.id>o.id){
return 1;
}else if(this.id<o.id){
return -1;
}else{
return 0;
}
}
}
//重写toString方法
public String toString() {
return "id:"+id+" name:"+name+" price:"+price+" press:"+press;
}
//重写hashCode方法
public int hashCode() {
return this.id;
}
//重写equals方法
public boolean equals(Object obj) {
//判断o是否是Book1对象
if(obj instanceof Book1){
//将obj从Object强制转化为Book1
Book1 b=(Book1)obj;
//判断id是否相等
if(b.id==this.id){
//相等返回true
return true;
}else{
//不等返回false
return false;
}
}else{
//o不是Book1对象返回false
return false;
}
}
}
运行结果截图:
3. 实现List和Map数据的转换。具体要求如下:
功能1:定义方法public void listToMap( ){ }将List中Student元素封装到Map
1) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List;
2) 遍历List,输出每个Student信息;
3) 将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value;
4) 遍历Map,输出每个Entry的key和value。
功能2:定义方法public void mapToList( ){ }将Map中Student映射信息封装到List
1) 创建实体类StudentEntry,可以存储Map中每个Entry的信息;
2) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息,并使用Student的id属性作为key,存入Map;
3) 创建List对象,每个元素类型是StudentEntry;
4) 将Map中每个Entry信息放入List对象。
package cn.sxt.jiu;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* 实现List和Map数据的转换。具体要求如下:
*
* 功能1:定义方法public void listToMap( ){ }将List中Student元素封装到Map中
* 1) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List;
* 2) 遍历List,输出每个Student信息;
* 3) 将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value;
* 4) 遍历Map,输出每个Entry的key和value。
*
* 功能2:定义方法public void mapToList( ){ }将Map中Student映射信息封装到List
* 1) 创建实体类StudentEntry,可以存储Map中每个Entry的信息;
* 2) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息,并使用Student的id属性作为key,存入Map;
* 3) 创建List对象,每个元素类型是StudentEntry;
* 4) 将Map中每个Entry信息放入List对象。
* @author ASUS
*
*/
public class lianxi03 {
public static void main(String[] args) {
lianxi03 lx = new lianxi03();
lx.listToMap();
lx.mapToList();
}
public void listToMap(){
System.out.println("====================================");
System.out.println("现在调用的是listToMap方法(list存储在map中)");
Student s1 = new Student(1001,"张三",20,"男");
Student s2 = new Student(1002,"李四",21,"女");
Student s3 = new Student(1003,"王五",18,"男");
//创建list容器
List<Student>list = new ArrayList<>();
//将学生信息存入list中
list.add(0, s1);//将s1放入第一个位置
list.add(1, s2);//将s2放入第二个位置
list.add(2, s3);//将s3放入第三个位置
//使用增强for循环,遍历list,输出每个Student信息
System.out.println("遍历list,输出每个Student信息如下:");
for(Student s:list){
System.out.println(s);
}
System.out.println("------------------------------------");
//创建map对象
Map<Integer,Student>map = new TreeMap<>();
//将List中数据放入Map,使用Student的id属性作为key,使用list中的Student对象信息作为value
map.put(s1.id, list.get(0));
map.put(s2.id, list.get(1));
map.put(s3.id, list.get(2));
////将map中的键值返回到keyset集合中
Set<Integer>keyset = map.keySet();
System.out.println("遍历Map,输出每个Entry的key和value如下:");
//使用增强for循环,遍历Map,输出每个Entry的key和value
for(Integer key:keyset){
System.out.println(key+"-----"+map.get(key));
}
}
public void mapToList(){
System.out.println("====================================");
System.out.println("现在调用的是mapToList方法(map存储在list中)");
StudentEntry s1 = new StudentEntry(1001,"张三",20,"男");
StudentEntry s2 = new StudentEntry(1002,"李四",21,"女");
StudentEntry s3 = new StudentEntry(1003,"王五",18,"男");
//创建map容器
Map<Integer,StudentEntry>map1 = new TreeMap<>();
//将学生信息存入map1中
map1.put(s1.id, s1);
Map<Integer,StudentEntry>map2 = new TreeMap<>();
map2.put(s2.id, s2);
Map<Integer,StudentEntry>map3 = new TreeMap<>();
map3.put(s3.id, s3);
//创建list对象,每个元素类型是StudentEntry
List<Map<Integer,StudentEntry>>list1 = new ArrayList();
//将Map中每个Entry信息放入List对象
list1.add(map1);
list1.add(map2);
list1.add(map3);
//使用增强for循环遍历输出
for(Map<Integer,StudentEntry>e:list1){
for(Integer key:e.keySet()){
System.out.println(key+"--"+e.get(key));
}
}
}
}
//Student学生类
class Student {
int id; //学号
String name; //名字
int age; //年龄
String sex; //性别
public Student() {
}
//构造器
public Student(int id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
//重写toString方法
public String toString() {
return "id:"+id+" name:"+name+" age:"+age+" sex:"+sex;
}
}
//StudentEntry继承了Student类,就拥有了Student类的成员属性
class StudentEntry extends Student{
public StudentEntry() {
}
//父类的构造方法不能继承,所以要重新写
public StudentEntry(int id, String name, int age, String sex) {
super(id, name, age, sex);
}
}
运行结果截图: