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()方法比較物件之間的大小,才能進行內部排序.