求Java大神帮忙写一段代码 不要写的太复杂 我是初学者

求Java大神帮忙写一段代码 不要写的太复杂 我是初学者 : 创建一个图书类 Book类 其属性有书号id 书名name 作者author 和出版社press 再声明三个book对象并赋值 然后依次用Set ,List ,Map 集合起来实现对book对象数据的存储 并输出相应的图书信息

import java.util.ArrayList;import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class Book implements Comparable<Book>{ int id;
String name;
String author;
String press;

public Book(int id, String name, String author, String press) {
super();
this.id = id;
this.name = name;
this.author = author;
this.press = press;
}

public String toString() { return "Book [author=" + author + ", id=" + id + ", name=" + name
+ ", press=" + press + "]";
}

public int compareTo(Book o) {
if(this.id>o.id){
return 1;
}
if(this.id<o.id){
return -1;
}
return 0;
}

public static void main(String[] args) {
Book b1=new Book(001, "java", "吴教授", "新华出版社");
Book b2=new Book(002, "java程序设计", "张教授", "延边出版社");
Book b3=new Book(003, "java自学指导", "刘教授", "大地出版社");

//采用set方式储存,这样的话需要实现Comparable接口
Set<Book> s=new TreeSet<Book>();
s.add(b1);
s.add(b2);
s.add(b3);

//采用list方式储存
ArrayList<Book> al=new ArrayList<Book>();
al.add(b1);
al.add(b2);
al.add(b3);

//采用map方式储存
Map<Integer, Book> m=new TreeMap<Integer, Book>();
m.put(b1.id, b1);
m.put(b2.id, b2);
m.put(b3.id, b3);

//遍历List
System.out.println("List 输出:");
for (int i = 0; i < al.size(); i++) {
System.out.println(al.get(i));
}
System.out.println();

//采用迭代遍历set
System.out.println("set 输出:");
Iterator<Book> i=s.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
System.out.println();

//读取map里的值
System.out.println("map 输出:");
System.out.println(m.get(b1.id));
System.out.println(m.get(b2.id));
System.out.println(m.get(b3.id));

}
}

这样看得懂吧。
温馨提示:答案为网友推荐,仅供参考
相似回答