//type 品种 name 狗名 price 价格
//使用 ArrayList 存入 10个 狗的实例
//京巴 小京 2000
//金毛 娜美 1500
//金毛 琪琪 1600
//藏獒 多吉 3000
//泰迪 球球 800
//藏獒 憨憨 4000
//美国恶霸犬 布诺 3000
//泰迪 小雪 1000
//秋田 八公 3000
//金毛 丢丢 1400
//1. 计算所有狗的 总价格
//2. 算出每个品种狗的数量 用Map存起来
//3. 输出 金毛的数量。
使用 ArrayList 存入 10个 狗的实例
// 1. 计算所有狗的 总价格
// 2. 算出每个品种狗的数量 用Map存起来
//*************************************************
dog类
public class Dog {
private String type;
private String name;
private double price;
public Dog(String type, String name, double price) {
super();
this.type = type;
this.name = name;
this.price = price;
}
public Dog() {
super();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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;
}
@Override
public String toString() {
return “Dog:” + type + “, name=” + name + “, price=” + price + “]”;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dog other = (Dog) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
}
//**************************************************************************
import java.util.*;
public class DogTest {
public static void main(String[] args) {
test1();
}
使用 ArrayList 存入 10个 狗的实例
// 1. 计算所有狗的 总价格
// 2. 算出每个品种狗的数量 用Map存起来
public static void test1() {
List<Dog> list = new ArrayList<Dog>();
list.add(new Dog("京巴", "小京", 2000));
list.add(new Dog("金毛", "娜美", 1500));
list.add(new Dog("金毛", "琪琪", 1600));
list.add(new Dog("藏獒", "多吉", 3000));
list.add(new Dog("泰迪", "球球", 800));
list.add(new Dog("藏獒", "憨憨", 4000));
list.add(new Dog("美国恶霸犬", "布诺", 3000));
list.add(new Dog("泰迪", "小雪", 1000));
list.add(new Dog("金毛", "丢丢", 1400));
Iterator<Dog> it = list.iterator();
int sum = 0;
// 获得狗的总价
while (it.hasNext()) {
sum += (int) (it.next().getPrice());
}
System.out.println("狗的总价为:" + sum);
//创建一个set集合,放狗的品种
Set<String> set = new HashSet<String>();
for (Dog dog : list) {
set.add(dog.getType());
}
//创建一个Map集合,存放够的品种,和该品种狗的个数
Map<String, String> map = new HashMap<String, String>();
for (String type : set) {
int count=0;
for (Dog dog : list) {
if (type.equals(dog.getType())) {
count++;
}
}
map.put(type, String.valueOf(count));
}
//输出map集合的所有键值对
Set<String> key = map.keySet();
Iterator<String> itK = set.iterator();
while (itK.hasNext()) {
String str = itK.next();
System.out.println(str + ":" + map.get(str));
}
}
}