I have a use case when I want to fill records in a data structure from several threads, so it should be thread safe and after reaching a certain size it starts discarding old records. And I also want to iterate over the data structure in the same order as Insertion.
So, I decided to use it Guava Cachehere, but, to my surprise, the Guava method asMap()does not return elements in any particular order.
private final Cache<Integer, Integer> cache =
CacheBuilder.newBuilder().maximumSize(10)
.removalListener(
RemovalListeners.asynchronous(new CustomListener(), executorService)
).build();
cache.put(1, 1);
cache.put(2, 2);
cache.put(3, 3);
cache.put(4, 4);
cache.put(5, 5);
cache.put(6, 6);
for (Entry<Integer, Integer> entry : cache.asMap().entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
Conclusion:
2=2
6=6
1=1
4=4
3=3
5=5
, , , , , , ?
. Java 7 Java 8.
- , :
1=1
2=2
3=3
4=4
5=5
6=6
user5447339