m0_disjjebwoo 2021-12-16 14:40 采纳率: 100%
浏览 48
已结题

Java map集合的问题

将以下计算机(Computen)类对象按照要求装入Map集合,具体要求如下:
计算机编号计算机品名销售价格
coo1
惠普笔记本 4200
coo2
联想台式机 3800
Coo3
戴尔笔记本 3650
1)以计算机编号为Key,计算机对象为valke,将以上计算机对象信息存入TeeMap中。
(2)创建Comparator比较器,使得存入TreeMap集合中的计算机对象按照计算机编号的降序进行排序。
(3)将价格四千元以上的计算机,价格打九五折进行销售(修改对应计算机价格)。
(4)迭代输出各Map集合中各对象的key和value值。

  • 写回答

2条回答 默认 最新

  • 大鹏cool Java领域优质创作者 2021-12-16 15:13
    关注

    示例代码如下:

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

    如有帮助,请采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月24日
  • 已采纳回答 12月16日
  • 提问应符合社区要求 12月16日
  • 创建了问题 12月16日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?