один из способов сделать это:
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);
}
Выход:
Первый ключ: один
Последний ключ: три
конечно isEmpty
никогда ложно Это дело.
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);
}
}
Выход:
Первый ключ: один
Последний ключ: один
Вы имеете в виду ключ или значение? –
http://stackoverflow.com/questions/1936462/java-linkedhashmap-get-first-or-last-entry – Matthias
взгляните на него. http://stackoverflow.com/questions/1936462/java-linkedhashmap-get-first-or-last-entry –