集合应用

//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));
	}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值