将以下计算机(Computen)类对象按照要求装入Map集合,具体要求如下:
计算机编号计算机品名销售价格
coo1
惠普笔记本 4200
coo2
联想台式机 3800
Coo3
戴尔笔记本 3650
1)以计算机编号为Key,计算机对象为valke,将以上计算机对象信息存入TeeMap中。
(2)创建Comparator比较器,使得存入TreeMap集合中的计算机对象按照计算机编号的降序进行排序。
(3)将价格四千元以上的计算机,价格打九五折进行销售(修改对应计算机价格)。
(4)迭代输出各Map集合中各对象的key和value值。
示例代码如下:
public class Computen {
private String no;
private String name;
private Double price;
public Computen(String no, String name, Double price) {
this.no = no;
this.name = name;
this.price = price;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
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 "Computen{" +
"no='" + no + '\'' +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
public class Test {
public static void main(String[] args) throws SQLException, IOException {
Computen computen = new Computen("coo1", "惠普笔记本", 4200d);
Computen computen2 = new Computen("coo2", "联想台式机", 3800d);
Computen computen3 = new Computen("Coo3", "戴尔笔记本", 3650d);
TreeMap<String, Computen> treeMap = new TreeMap<>(Comparator.reverseOrder());
treeMap.put(computen.getName(), computen);
treeMap.put(computen2.getName(), computen2);
treeMap.put(computen3.getName(), computen3);
treeMap.values().stream().filter(item -> item.getPrice() > 4000).forEach(item -> item.setPrice(item.getPrice() * 0.95));
for (Map.Entry<String, Computen> entry : treeMap.entrySet()) {
System.out.printf("key:%s, value:%s\n", entry.getKey(), entry.getValue());
}
}
}
如有帮助,请采纳。
用HashMap,key就是coo1,创建类,里面包含名称和价格,放到value