I just tried to get add effect with ConcurrentHashMap<String,String> but the elements order is mixed.
Code like a…
ConcurrentHashMap<String,String>h=new ConcurrentHashMap<String,String>();
for(int i=0; i<10; i++)
{
h.put(""+Math.random(), ""+i);
}
Iterator<String> iterator=h.values().iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
… gives in console next values:
9
4
6
2
0
5
1
7
3
8
… so my question is…
is there a way for ConcurrentHashMap to get original elements order if key is some random string?
Thanks
Use
LinkedHashMapinstead it will preserve the order of insertionAnd use
See
Collections.synchronizedMap(mapINstance);