zoukankan      html  css  js  c++  java
  • MapTest java 核心编程

    简介

    类似c++map

    code

    /*
     * @Author: your name
     * @Date: 2020-10-27 21:15:06
     * @LastEditTime: 2020-10-27 21:19:36
     * @LastEditors: Please set LastEditors
     * @Description: In User Settings Edit
     * @FilePath: /java/Employee.java
     */
    
    public class Employee implements Comparable<Employee> {
    	private String name;
    	private double salary;
    	
    	public Employee(String name, double salary) {
    		this.name = name;
    		this.salary = salary;
    	}
        
        public Employee(String name) {
    		this.name = name;
    		this.salary = -1;
    	}
        public String toString(){
            return "[name=" + name +  "]";
        }
    	public String getName(){
    		return name;
    	}
    
    	public double getSalary() {
    		return salary;
    	}
    	
    	public void raiseSalary(double byPercent) {
    		double raise = salary * byPercent / 100;
    		salary += raise;
    	}
    	
    	@Override
    	public int compareTo(Employee other) {
    		// TODO Auto-generated method stub
    		return Double.compare(salary,  other.salary);
    	}
    }
    
    
    /*
     * @Author: your name
     * @Date: 2020-10-27 21:08:20
     * @LastEditTime: 2020-10-27 21:18:24
     * @LastEditors: Please set LastEditors
     * @Description: In User Settings Edit
     * @FilePath: /java/MapTest.java
     */
    import java.util.*;
    
    public class MapTest {
        public static void main(String[] args) {
            Map<String, Employee> staff = new HashMap<>();
            staff.put("144-25-5464", new Employee("Amy Lee"));
            staff.put("567-24-2546", new Employee("Harry Hacker"));
            staff.put("157-62-7935", new Employee("Gary Cooper"));
            staff.put("456-62-5527", new Employee("Frrancesca Curz"));
            
            System.out.println(staff);
            staff.remove("567-24-2546");
            staff.put("456-62-5527", new Employee("Francesca Miller"));
            System.out.println(staff.get("157-62-7935"));
            staff.forEach((k, v)->System.out.println("key=" + k + ", value=" + v));
        }
    }
    
    

    result

    {157-62-7935=[name=Gary Cooper], 144-25-5464=[name=Amy Lee], 456-62-5527=[name=Frrancesca Curz], 567-24-2546=[name=Harry Hacker]}
    [name=Gary Cooper]
    key=157-62-7935, value=[name=Gary Cooper]
    key=144-25-5464, value=[name=Amy Lee]
    key=456-62-5527, value=[name=Francesca Miller]
    

    发现确实完成了元素的删除和替换

    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    取消 Vue 中格式编译警告
    Vue 中提示报错 handlers[i].call is not a function解决方法
    PhpStorm 配置链接远程虚拟机
    Java 类提供了自定义的构造方法,那么类的默认构造不会被调用
    2019.9.30极限测试 04.JAVA语言课堂测试试卷-极限测试
    程序员修炼之道+从小工到专家 9月份读后感
    2019.9.23 作业2
    2019.9.23 作业1
    原码,补码,反码区分
    9.16日上课总结
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13887654.html
Copyright © 2011-2022 走看看