尚学堂Java第9章作业编码题参考

本人初学JAVA,自己写的答案,仅供参考。如有错误或可以优化的地方请各位留言指正。谢谢!*

1.使用List和Map存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key。

``
public class Test1 {
public static void main(String[] args) {
//创建图书对象
Books book1 = new Books(1001,“时间简史”,77,“新华出版社”);
Books book2 = new Books(1002,“三体”,66,“邮电出版社”);
Books book3 = new Books(1003,“流浪地球”,55,“中国人民出版社”);
//list集合
List list = new ArrayList();
list.add(book1);
list.add(book2);
list.add(book3);
//遍历list集合
for (Books book : list) {
System.out.println(book);
}
//map集合
HashMap<Integer,Books> map = new HashMap<Integer,Books>();
map.put(book1.getNumber(), book1);
map.put(book2.getNumber(), book2);
map.put(book3.getNumber(), book3);
//遍历map集合
Set<Entry<Integer, Books>> set = map.entrySet();
for (Entry<Integer, Books> entry : set) {
System.out.println(“编号” + entry.getKey() + “:” + entry.getValue());
}
}
}


```图书类
public class Books {
	private int number;
	private String name;
	private double price;
	private String press;
	public Books(int nums, String name, double price, String press) {
		super();
		this.number = nums;
		this.name = name;
		this.price = price;
		this.press = press;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	@Override
	public String toString() {
		return "Books [number=" + number + ", name=" + name + ", price=" + price + ", press=" + press + "]";
	}
	
	
}

2.使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;要求向其中添加多个相同的商品,验证集合中元素的唯一性。

public class Test1 {
	public static void main(String[] args) {
		//创建图书对象
		Books book1 = new Books(1001,"时间简史",77,"新华出版社");
		Books book2 = new Books(1002,"三体",66,"邮电出版社");
		Books book3 = new Books(1003,"流浪地球",55,"中国人民出版社");
		Books book4 = new Books(1003,"流浪地球",55,"中国人民出版社");
		//HashSet集合
		HashSet<Books> hs = new HashSet<Books>();
		hs.add(book1);
		hs.add(book2);
		hs.add(book3);
		hs.add(book4);
		//遍历HashSet
		for (Books books : hs) {
			System.out.println(books);
		}
		
		System.out.println("==========================");
		
		//TreeSet
		TreeSet<Books> ts = new TreeSet<Books>();
		ts.add(book1);
		ts.add(book2);
		ts.add(book3);
		ts.add(book4);
		//遍历TreeSet
		for (Books books : ts) {
			System.out.println(books);
		}
	}
}

!输出结果

在这里插入图片描述
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对象。

学生类:

public class Student {
	private int id;
	private String name;
	private int age;
	private String sex;
	public Student(int id, String name, int age, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

功能1:

public class Test2 {
	public static void main(String[] args) {
		//使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List;
		Student s1 = new Student(1,"张三",20,"男");
		Student s2 = new Student(2,"李四",22,"男");
		Student s3 = new Student(3,"王五",18,"女");
		List<Student> list = new ArrayList<Student>();
		list.add(s1);
		list.add(s2);
		list.add(s3);
		//遍历List,输出每个Student信息;
		for (Student s : list) {
			System.out.println(s);
		}
		//遍历Map,输出每个Entry的key和value。
		Map<Integer,Student> map = new HashMap<Integer,Student>();
		map = listToMap(list, map);
		Set<Entry<Integer, Student>> set = map.entrySet();
		for (Entry<Integer, Student> entry : set) {
			System.out.println("ID:" + entry.getKey() + " 学生信息:" + entry.getValue());
		}
	}
	//将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value;
	public static Map<Integer,Student> listToMap(List<Student> list,Map<Integer,Student> map) {
		for(int i = 0;i < list.size();i++) {
			map.put(list.get(i).getId(), list.get(i));
		}
		return map;
	}
}

功能2:

public class Test2 {
	public static void main(String[] args) {
		//使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List;
		Student s1 = new Student(1,"张三",20,"男");
		Student s2 = new Student(2,"李四",22,"男");
		Student s3 = new Student(3,"王五",18,"女");
		
		//使用Student的id属性作为key,存入Map;
		Map<Integer,Student> map = new HashMap<Integer,Student>();
		map.put(s1.getId(), s1);
		map.put(s2.getId(), s2);
		map.put(s3.getId(), s3);
		
		//创建List对象,每个元素类型是StudentEntry;
		List<StudentEntry> list = new ArrayList<StudentEntry>();
		list = mapToList(list, map);
		for (StudentEntry s : list) {
			System.out.println(s.toString());
		}
	}
	//将Map中每个Entry信息放入List对象。
	public static List<StudentEntry> mapToList(List<StudentEntry> list,Map<Integer,Student> map){ 
		Set<Entry<Integer, Student>> set = map.entrySet();
			list.add(new StudentEntry(map,set));
		
		return list;
	}
}

public class StudentEntry {
	public Map<Integer,Student> map = new HashMap<Integer,Student>();
	public Set<Entry<Integer, Student>> set = map.entrySet();
	public StudentEntry(Map<Integer, Student> map, Set<Entry<Integer, Student>> set) {
		super();
		this.map = map;
		this.set = set;
	}
	@Override
	public String toString() {
		return "StudentEntry [set=" + set + "]";
	}
			
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值