Need a Java map/table with multiple keys to one value. Value is commonly altered

前端 未结 5 1283
旧巷少年郎
旧巷少年郎 2021-01-04 10:13

What I need is a collection which allows multiple keys to access a single object.

I need to apply frequent alterations to this object.

It also must be effici

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 10:44

    this may do what you want:

    import java.util.*;
    class Value {
        public String toString() {
            return x.toString();
        }
        Integer x=0;
    }
    public class Main {
        public static void main(String[] arguments) {
            Map m=new HashMap();
            final Value v=new Value();
            m.put(1,v);
            m.put(2,v);
            System.out.println(m.get(1));
            System.out.println(m.get(2));
            v.x=42;
            System.out.println(m.get(1));
            System.out.println(m.get(2));
        }
    

提交回复
热议问题