une façon de le faire:
Map<String, Double> map = new LinkedHashMap<>();
map.put("one", 2.0);
map.put("two", 12.0);
map.put("three", 44.0);
String lKeyFirst = null;
String lKeyLast = null;
if (!map.isEmpty()){
lKeyFirst = map.keySet().iterator().next();
for(String key : map.keySet()){
lKeyLast = key;
}
System.out.println("First key: " +lKeyFirst);
System.out.println("Last key: " +lKeyLast);
}
Sortie:
Première clé: une
Dernière clé: trois
bien sûr isEmpty
est jamais faux ce cas.
EDIT:
@Test
public void testMapOneValue() {
Map<String, Double> map = new LinkedHashMap<>();
map.put("one", 2.0);
String lKeyFirst = null;
String lKeyLast = null;
if (!map.isEmpty()){
lKeyFirst = map.keySet().iterator().next();
for(String key : map.keySet()){
lKeyLast = key;
}
System.out.println("First key: " +lKeyFirst);
System.out.println("Last key: " +lKeyLast);
}
}
Sortie:
Première clé: une
Dernière touche: une
vous voulez dire la clé ou la valeur? –
http://stackoverflow.com/questions/1936462/java-linkedhashmap-get-first-or-last-entry – Matthias
jetez un coup d'œil. http://stackoverflow.com/questions/1936462/java-linkedhashmap-get-first-or-last-entry –