我有一个像这样的loadingcache类:
LoadingCache<Integer, List<Parent>> parents
其中Parent
是包含id
,description
,home
和nickname
我想在parents
对象
我尝试过这样的事情:
parents.keys()
但我找不到LoodingCache
对象
答案 0 :(得分:2)
您可以改用方法asMap()
:
/**
* Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to
* the map directly affect the cache.
*/
ConcurrentMap<K, V> asMap();
答案 1 :(得分:0)
这个怎么样?:
Set<Integer> s = parents.asMap().keySet();
Iterator<Integer> it = s.iterator();
while (it.hasNext()) {
int n = it.next();
try {
List<Parent> list = parents.get(n);
for (Parent l : list) {
System.out.println(String.format(
"id = %s, description = %s, home = %s",
l.getId(), l.getDescription(), l.getHome())); // you can do more than printing to standard output
}
} catch (ExecutionException e) {
LOG.error(e.getMessage(), e); // you can do whatever you want
}
}
LoadingCache有一个名为asMap
的方法get方法抛出三个异常,它们是: ExecutionException UncheckedExecutionException 和ExecutionError
答案 2 :(得分:0)
如果您只想打印所有Parent
个对象,这非常简单。
for (List<Parent> parentsGroup : parents.asMap().values()) {
for (Parent parent : parentsGroup) {
System.out.println(parent); // or whatever
}
}