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
  • 相关阅读:
    Ubuntu14.04LTS系统QQ的安装:pidgin-lwqq
    Ubuntu14.04LTS系统输入法的安装
    Linux系统安装及初始化(ubuntu14.04)
    创建RAID并永久挂载RAID
    磁盘管理和磁盘配额
    用户账号组账号概述
    安装及管理程序
    目录和文件管理
    Vi编辑器的工作模式
    Linux命令及使用方法
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13887654.html
Copyright © 2011-2022 走看看