62 设计有setAll功能的哈希表
Table of Contents

ac

import java.util.*;


class ValueIncludeTime{
    int value;
    long time;

    public int getValue() {
        return value;
    }

    public long getTime() {
        return time;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public ValueIncludeTime(int value, long time) {
        this.value = value;
        this.time = time;
    }
}

class SetAllHashMap{
    HashMap<Integer, ValueIncludeTime> mp;
    ValueIncludeTime setAllValue;
    static int time = -1;

    public SetAllHashMap() {
        this.mp = new HashMap<>();
        this.setAllValue = new ValueIncludeTime(-1,time);
    }

    public boolean containsKey(Integer key) {
        return mp.containsKey(key);
    }

    public Integer get(Integer key){
        if(containsKey(key)){
            // 对比 setAllValue 的时间戳
            ValueIncludeTime valueIncludeTime = mp.get(key);
            if(valueIncludeTime.getTime() > setAllValue.getTime()){
                return valueIncludeTime.getValue();
            }
            return setAllValue.getValue();
        }
        return -1;
    }

    public void put(Integer key, Integer value){
//        long time = System.currentTimeMillis();
        time ++;
        long newTime = time;
        ValueIncludeTime valueIncludeTime = new ValueIncludeTime(value, newTime);
        mp.put(key, valueIncludeTime);
    }

    public void setAll(int value){
//        long time = System.currentTimeMillis();
        time ++;
        long newTime = time;
        this.setAllValue.setTime(newTime);
        this.setAllValue.setValue(value);
    }
}

public class Main {

    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        SetAllHashMap hashMap = new SetAllHashMap();
        int n = sc.nextInt();
        for(int i=0;i<n;i++){
            int op = sc.nextInt();
            if(op == 1){
                // put
                int key = sc.nextInt();
                int value = sc.nextInt();
                hashMap.put(key, value);
            }
            if(op == 2){
                // get
                int key = sc.nextInt();
                System.out.println(hashMap.get(key));
            }
            if(op == 3){
                // setAll
                int value = sc.nextInt();
                hashMap.setAll(value);
            }
        }
    }

}
/*
6
1 1 2
2 1
2 2
3 4
2 1
2 2

2
-1
4
-1
 */