Which Java collection should I use to implement a thread-safe cache?

后端 未结 5 1532
后悔当初
后悔当初 2021-02-20 14:58

I\'m looking to implement a simple cache without doing too much work (naturally). It seems to me that one of the standard Java collections ought to suffice, with a little extra

5条回答
  •  执笔经年
    2021-02-20 15:39

    For synchronization, the Collections framework provides a synchronized map:

    Map myMap = Collections.synchronizedMap(new HashMap());
    

    You could then wrap this, or handle the LRU logic in a cache object.

提交回复
热议问题