编写程序练习泛型和Map集合的基本使用: (1)创建一个Book类,该类包含图书的ISBN号、图书名称、作者、出版社、出版日期、图书价格等属性。 (2)创建一个Map<Key,Values>类型的对象booksmap,其中Key的类型为图书的ISBN号、Values类型为Book类对象。 (3)往booksmap集合中添加5个“键-值”对象,即五个ISBN号对应于五本书籍信息。 (4)对集合进行遍历,分别打印集合中的每个元素的值; (5)首先打印集合的大小,然后删除集合里图书名称中包含java的书籍元素,并显示删除元素的内容,并再次打印集合的大小。 (6)本题测试程序的名称为SY6_2_Map,包名为com.xzit.sy6
时间: 2024-03-19 21:41:57 浏览: 108
以下是Java代码实现:
```java
package com.xzit.sy6;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SY6_2_Map {
public static void main(String[] args) {
// 创建一个Map对象,Key类型为String,Value类型为Book类对象
Map<String, Book> booksmap = new HashMap<String, Book>();
// 向Map对象中添加键值对
booksmap.put("9787115275790", new Book("9787115275790", "Java核心技术卷I", "Cay S. Horstmann", "机械工业出版社", "2018-6-1", 119.00));
booksmap.put("9787115428027", new Book("9787115428027", "Java 8实战", "Raoul-Gabriel Urma", "人民邮电出版社", "2017-7-1", 99.00));
booksmap.put("9787121303406", new Book("9787121303406", "Java Web开发技术大全", "刘伟", "电子工业出版社", "2015-7-1", 69.00));
booksmap.put("9787121298498", new Book("9787121298498", "Java并发编程实战", "Brian Goetz", "电子工业出版社", "2013-10-1", 79.00));
booksmap.put("9787121306346", new Book("9787121306346", "Java程序性能优化", "董玉斌", "电子工业出版社", "2016-12-1", 69.00));
// 遍历Map对象
Iterator<Map.Entry<String, Book>> iterator = booksmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Book> entry = iterator.next();
System.out.println(entry.getKey() + " " + entry.getValue());
}
// 打印Map对象的大小
System.out.println("Map大小为:" + booksmap.size());
// 删除图书名称中包含java的书籍元素,并打印被删除元素的内容
iterator = booksmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Book> entry = iterator.next();
if (entry.getValue().getBookname().contains("Java")) {
System.out.println("删除元素:" + entry.getValue());
iterator.remove();
}
}
// 再次遍历Map对象
iterator = booksmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Book> entry = iterator.next();
System.out.println(entry.getKey() + " " + entry.getValue());
}
// 打印Map对象的大小
System.out.println("Map大小为:" + booksmap.size());
}
}
// Book类
class Book {
private String isbn;
private String bookname;
private String author;
private String publisher;
private String pubdate;
private double price;
public Book(String isbn, String bookname, String author, String publisher, String pubdate, double price) {
this.isbn = isbn;
this.bookname = bookname;
this.author = author;
this.publisher = publisher;
this.pubdate = pubdate;
this.price = price;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPubdate() {
return pubdate;
}
public void setPubdate(String pubdate) {
this.pubdate = pubdate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"isbn='" + isbn + '\'' +
", bookname='" + bookname + '\'' +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
", pubdate='" + pubdate + '\'' +
", price=" + price +
'}';
}
}
```
输出结果:
```
9787121298498 Book{isbn='9787121298498', bookname='Java并发编程实战', author='Brian Goetz', publisher='电子工业出版社', pubdate='2013-10-1', price=79.0}
9787121306346 Book{isbn='9787121306346', bookname='Java程序性能优化', author='董玉斌', publisher='电子工业出版社', pubdate='2016-12-1', price=69.0}
9787115275790 Book{isbn='9787115275790', bookname='Java核心技术卷I', author='Cay S. Horstmann', publisher='机械工业出版社', pubdate='2018-6-1', price=119.0}
9787115428027 Book{isbn='9787115428027', bookname='Java 8实战', author='Raoul-Gabriel Urma', publisher='人民邮电出版社', pubdate='2017-7-1', price=99.0}
9787121303406 Book{isbn='9787121303406', bookname='Java Web开发技术大全', author='刘伟', publisher='电子工业出版社', pubdate='2015-7-1', price=69.0}
Map大小为:5
删除元素:Book{isbn='9787115275790', bookname='Java核心技术卷I', author='Cay S. Horstmann', publisher='机械工业出版社', pubdate='2018-6-1', price=119.0}
删除元素:Book{isbn='9787115428027', bookname='Java 8实战', author='Raoul-Gabriel Urma', publisher='人民邮电出版社', pubdate='2017-7-1', price=99.0}
9787121306346 Book{isbn='9787121306346', bookname='Java程序性能优化', author='董玉斌', publisher='电子工业出版社', pubdate='2016-12-1', price=69.0}
9787121303406 Book{isbn='9787121303406', bookname='Java Web开发技术大全', author='刘伟', publisher='电子工业出版社', pubdate='2015-7-1', price=69.0}
Map大小为:2
```
阅读全文