1. 使用List和Map存放多个图书信息,遍历并输出.其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestStoreBookInfo {
public static void main(String[] args) {
List<Object> bookList1 = new ArrayList<>();
bookList1.add("朝花夕拾");
bookList1.add(10001);
bookList1.add(21.30);
bookList1.add("天津人民出版社");
List<Object> bookList2 = new ArrayList<>();
bookList2.add("人生如逆旅");
bookList2.add(10002);
bookList2.add(30.90);
bookList2.add("四川人民出版社");
List<Object> bookList3 = new ArrayList<>();
bookList3.add("身份的焦虑");
bookList3.add(10003);
bookList3.add(30.80);
bookList3.add("上海译文出版社");
List<Object> bookList4 = new ArrayList<>();
bookList4.add("面纱");
bookList4.add(10004);
bookList4.add(9.90);
bookList4.add("开明出版社");
Map<Integer,List<Object>> bookMap = new HashMap<>();
bookMap.put(1001, bookList1);
bookMap.put(1002, bookList2);
bookMap.put(1003, bookList3);
bookMap.put(1004, bookList4);
for(Integer key:bookMap.keySet()) {
List<Object> booklist = bookMap.get(key);
for(Object bookinfo:booklist) {
System.out.print(bookinfo + " ");
}
System.out.println();
}
}
}
2. 使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社
要求向其中添加多个相同的商品,验证集合中元素的唯一性.
提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals().
向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较规则.
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class WorkTest {
public static void main(String[] args) {
// 自定义通过id来判断是否是同一个商品
// 通过自定义hashCode和equals方法实现
Set<Book> bookSet = new HashSet<>();
bookSet.add(new Book("朝花夕拾",10001,21.30,"天津人民出版社"));
bookSet.add(new Book("人生如逆旅",10001,30.90,"四川人民出版社"));
bookSet.add(new Book("身份的焦虑",10003,30.80,"上海译文出版社"));
bookSet.add(new Book("身份的焦虑",10002,30.80,"上海译文出版社"));
for(Book bb:bookSet) {
System.out.println(bb);
}
System.out.println("-----------------");
// 通过价格降序排列
// 通过Comparable接口
Set<Book> bookSet2 = new TreeSet<>();
bookSet2.add(new Book("朝花夕拾",10001,21.30,"天津人民出版社"));
bookSet2.add(new Book("人生如逆旅",10002,30.90,"四川人民出版社"));
bookSet2.add(new Book("身份的焦虑",10003,9.90,"上海译文出版社"));
bookSet2.add(new Book("面纱",10004,9.90,"开明出版社"));
for(Book bb:bookSet2) {
System.out.println(bb);
}
}
}
class Book implements Comparable<Book>{
private String BookName;
private int Id;
private double price;
private String PulishHouse;
public boolean equals(Object obj) {
// 判断进行比较的对象是不是Book类的对象
if (obj instanceof Book) {
// 如果是,则将此对象强制转为Book类的对象,并进一步进行判断
Book newBook = (Book) obj;
// 判断传入的对象与此对象的ID是否相同,相同则认为是同一本书
if(this.Id!=newBook.getId()) {
// 不相同返回false
return false;
}else {
return true;
}
}else {
return false;
}
}
public int hashCode() {
//hashCode方法返回的是int类型,并且保证返回值要与Book对象的属性有关
return Id;
}
public String toString() {
return "ID:"+ Id + " BookName:" + BookName + " price:" + price + " publishHouse:" + PulishHouse;
}
@Override
public int compareTo(Book o) {
// 先通过价格来比较,逆序
if(this.price>o.getPrice()) {
return -1;
}else if(this.price<o.getPrice()) {
return 1;
}else {
// 价格相等时,用Id比较,逆序
if(this.Id>o.getId()) {
return -1;
}else if(this.Id<o.getId()) {
return 1;
}else {
// 都相同时,返回0
return 0;
}
}
}
public Book() {
}
public Book(String bookName, int id, double price, String pulishHouse) {
super();
BookName = bookName;
Id = id;
this.price = price;
PulishHouse = pulishHouse;
}
public String getBookName() {
return BookName;
}
public void setBookName(String bookName) {
BookName = bookName;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPulishHouse() {
return PulishHouse;
}
public void setPulishHouse(String pulishHouse) {
PulishHouse = pulishHouse;
}
}
3. 实现List和Map数据的转换.具体要求如下:
功能1:定义方法public static void listToMap(){}将List中Student元素封装到Map中
1) 使用构造方法Student(int id,String name,int age,String sex,int age, int score)创建多个学生信息并加入List;
2) 遍历List,输出每个Student信息;
3) 将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value;
4) 遍历Map,输出每个Entry的key和value.
功能2:定义方法public static void mapToList(){}将Map中Student映射信息封装到List
1) 创建实体类StudentEntry,可以存储Map中每个Entry的信息;
2) 使用构造方法Student(int id,String name,int age,String sex,int age)创建多个学生信息,
并使用Student的id属性作为key,存入Map;
3) 创建List对象,每个元素类型是StudentEntry;
4) 将Map中每个Entry信息放入List对象.
功能3:说明Comparable接口的作用,并通过分数来对学生进行排序.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
public class OtherTrans {
public static void main(String[] args) {
Students stu1 = new Students(10001,"小王",19,"男",120);
Students stu2 = new Students(10002,"小刘",20,"女",99);
Students stu3 = new Students(10003,"小芯",18,"女",103);
List<Students> stuList = new ArrayList<>();
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
listToMap(stuList);
System.out.println("########################");
// Map转List
Map<Integer,Students> stuMap = new HashMap<>();
stuMap.put(stu1.getId(), stu1);
stuMap.put(stu2.getId(), stu2);
stuMap.put(stu3.getId(), stu3);
mapToList(stuMap);
// 按分数排序(Comparable接口实现)
Set<Students> stuSet = new TreeSet<>();
stuSet.add(stu1);
stuSet.add(stu2);
stuSet.add(stu3);
System.out.println(stuSet);
}
public static void mapToList(Map<Integer,Students> stuMap){
// 创建元素类型为Entry的List
List<Entry<Integer,Students>> stulist = new ArrayList<>();
// 将Map中每个Entry信息放入List对象.
for(Entry<Integer,Students> stuEntry:stuMap.entrySet()) {
stulist.add(stuEntry);
}
// 遍历List并输出
for(Entry<Integer,Students> ss:stulist) {
System.out.println(ss);
}
}
public static void listToMap(List<Students> stu){
Map<Integer,Students> stuMap = new HashMap<>();
// 遍历输出,并添加到map中
for(Students ss:stu) {
System.out.println(ss);
stuMap.put(ss.getId(), ss);
}
// 输出key,value
for(Integer key:stuMap.keySet()) {
System.out.println(key+"--------"+stuMap.get(key));
}
}
}
class Students implements Comparable<Students>{
private int id;
private String name;
private int age;
private String sex;
private int score;
public int compareTo(Students o) {
if(this.score>o.getScore()) {
return -1;
}else if(this.score<o.getScore()) {
return 1;
}else {
if(this.id>o.getId()) {
return -1;
}else if(this.id<o.getId()) {
return 1;
}else {
return 0;
}
}
}
public String toString() {
return "id:"+id+" 姓名:"+name+" 年龄:"+age+" 性别:"+sex+" 分数:"+score;
}
public Students() {
}
public Students(int id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Students(int id, String name, int age, String sex, int score) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Comparable接口的作用:
TreeSet底层实际是用TreeMap实现的,内部维持了一个简化版的TreeMap,通过key来存储Set的元素.
TreeSet内部需要对存储的元素进行排序,因此,我们对应的类需要实现Comparable接口.这样,
才能根据compareTo()方法比较对象之间的大小,才能进行内部排序.