package com.lrq;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
*
* @author 李瑞琦
* 使用List和Map存放多个图书信息,遍历并输出。
* 其中图书属性:编号,名称,单价,出版社; 使用商品编号作为Map中的key。
*
*/
public class Test01 {
public static void main(String[] args) {
// 声明list集合
List<Book> list = new ArrayList<Book>();
//实例化并赋初值Book对象
Book book1 = new Book(101, "西游记", 10.1, "清华出版社");
Book book2 = new Book(002, "水浒传", 10.2, "北大出版社");
Book book3 = new Book(003, "红楼梦", 10.3, "人大出版社");
Book book4 = new Book(004, "三国演义", 10.4, "地质大学出版社");
//往list里面存值
list.add(book1);
list.add(book2);
list.add(book3);
list.add(book4);
//遍历集合并输出,在知道是list集合的前提下可以直接用foreach遍历,
//在不知道是list还是set的前提下可以优先考虑Iterator迭代器
for (Iterator ite = list.iterator(); ite.hasNext();) {
Book book = (Book) ite.next();
System.out.println(book);
}
//声明一个map集合
Map< Integer, Book> map = new HashMap<Integer,Book>();
//实例化并赋初值Book对象
Book book5 = new Book(101, "活着", 10.1, "清华出版社");
Book book6 = new Book(002, "骆驼祥子", 10.2, "北大出版社");
Book book7 = new Book(003, "茶馆", 10.3, "人大出版社");
Book book8 = new Book(004, "大学", 10.4, "地质大学出版社");
//往map中存放数据
map.put(1, book1);
map.put(2, book2);
map.put(3, book3);
map.put(4, book4);
//在foreach中使用entry来遍历map
for (Entry<Integer, Book> entry : map.entrySet()) {
System.out.println(entry.getValue());
}
}
}
package com.lrq;
/**
* JAVABean
* @author 图书类
*
*/
public class Book {
private int id;
private String name;
private double price;
private String pressName;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(int id, String name, double price, String pressName) {
super();
this.id = id;
this.name = name;
this.price = price;
this.pressName = pressName;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + ", pressName=" + pressName + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pressName == null) ? 0 : pressName.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pressName == null) {
if (other.pressName != null)
return false;
} else if (!pressName.equals(other.pressName))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPressName() {
return pressName;
}
public void setPressName(String pressName) {
this.pressName = pressName;
}
}
list、map、set、for-each、Iterator综合练习1
最新推荐文章于 2023-11-26 00:06:22 发布