I have this Java Map:
Can you tell me how I can get the 6-th element of the Map?
private static final Map cache = new HashMap<
HashMaps do not preserve ordering:
LinkedHashMap which guarantees a predictable iteration order.
Example
public class Users
{
private String Id;
public String getId()
{
return Id;
}
public void setId(String id)
{
Id = id;
}
}
Users user;
LinkedHashMap linkedHashMap = new LinkedHashMap();
for (int i = 0; i < 3; i++)
{
user = new Users();
user.setId("value"+i);
linkedHashMap.put("key"+i,user);
}
/* Get by position */
int pos = 1;
Users value = (new ArrayList(linkedHashMap.values())).get(pos);
System.out.println(value.getId());