Java LinkedHashMap获取第一个元素和最后一个元素 深藏阁楼爱情的钟 2022-04-08 09:59 429阅读 0赞 ## [Java LinkedHashMap获取第一个元素和最后一个元素 ][Java_LinkedHashMap] 作者是 [在线疯狂][Link 1] 发布于 2016年10月27日 在 [Java][]. ## 获取LinkedHashMap中的头部元素(最早添加的元素): 时间复杂度O(1) public <K, V> Entry<K, V> getHead(LinkedHashMap<K, V> map) { return map.entrySet().iterator().next(); } * 1 * 2 * 3 获取LinkedHashMap中的末尾元素(最近添加的元素): 时间复杂度O(n) public <K, V> Entry<K, V> getTail(LinkedHashMap<K, V> map) { Iterator<Entry<K, V>> iterator = map.entrySet().iterator(); Entry<K, V> tail = null; while (iterator.hasNext()) { tail = iterator.next(); } return tail; } * 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 通过反射获取LinkedHashMap中的末尾元素: 时间复杂度O(1),访问tail属性 public <K, V> Entry<K, V> getTailByReflection(LinkedHashMap<K, V> map) throws NoSuchFieldException, IllegalAccessException { Field tail = map.getClass().getDeclaredField("tail"); tail.setAccessible(true); return (Entry<K, V>) tail.get(map); } * 1 * 2 * 3 * 4 * 5 * 6 测试代码: import static org.junit.Assert.assertEquals; import java.lang.reflect.Field; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map.Entry; import org.junit.Before; import org.junit.Test; public class TestLinkedHashMap { private LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); private String letters[] = { "a", "b", "c", "d", "e" }; @Before public void init() { for (int i = 0; i < letters.length; i++) { map.put(letters[i], i + 1); } } @Test public void testGetHead() { assertEquals(getHead(map).getKey(), "a"); assertEquals(getHead(map).getValue(), Integer.valueOf(1)); } @Test public void testGetTail() { assertEquals(getTail(map).getKey(), "e"); assertEquals(getTail(map).getValue(), Integer.valueOf(5)); } @Test public void testGetTailByReflection() throws NoSuchFieldException, IllegalAccessException { assertEquals(getTailByReflection(map).getKey(), "e"); assertEquals(getTailByReflection(map).getValue(), Integer.valueOf(5)); } public <K, V> Entry<K, V> getHead(LinkedHashMap<K, V> map) { return map.entrySet().iterator().next(); } public <K, V> Entry<K, V> getTail(LinkedHashMap<K, V> map) { Iterator<Entry<K, V>> iterator = map.entrySet().iterator(); Entry<K, V> tail = null; while (iterator.hasNext()) { tail = iterator.next(); } return tail; } @SuppressWarnings("unchecked") public <K, V> Entry<K, V> getTailByReflection(LinkedHashMap<K, V> map) throws NoSuchFieldException, IllegalAccessException { Field tail = map.getClass().getDeclaredField("tail"); tail.setAccessible(true); return (Entry<K, V>) tail.get(map); } } * 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 * 25 * 26 * 27 * 28 * 29 * 30 * 31 * 32 * 33 * 34 * 35 * 36 * 37 * 38 * 39 * 40 * 41 * 42 * 43 * 44 * 45 * 46 * 47 * 48 * 49 * 50 * 51 * 52 * 53 * 54 * 55 * 56 * 57 * 58 * 59 * 60 * 61 本文链接:[http://bookshadow.com/weblog/2016/10/27/java-linked-hash-map-get-first-and-get-last/][Java_LinkedHashMap] 请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。 如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看[支付宝二维码][Link 2] [Java_LinkedHashMap]: http://bookshadow.com/weblog/2016/10/27/java-linked-hash-map-get-first-and-get-last/ [Link 1]: http://bookshadow.com/weblog/authors/%E5%9C%A8%E7%BA%BF%E7%96%AF%E7%8B%82/ [Java]: http://bookshadow.com/weblog/categories/java/ [Link 2]: http://bookshadow.com/weblog/2016/10/27/java-linked-hash-map-get-first-and-get-last/#
还没有评论,来说两句吧...