![]() Next, lets’ take a look at an example to make it more clear what access order is. This sort of a LinkedHashMap fits great for building LRU (least recently used) caches. We access entries in a map by using methods: put, putIfAbsent, get, getOrDefault, compute, computeIfAbsent, computeIfPresent and also merge methods. When you set accessOrder to true the entries in the map will be ordered from the least-recently accessed to most-recently accessed. To put it simply, it lets you define a different order for the entries in the map – the order how entries are accessed in the map. This is another feature of LinkedHashMap that differs from the HashMap. You might wonder what the accessOrder parameter is you saw in the LinkedHashMap constructor definition. LinkedHashMap has been available since JDK 1.4. To achieve a thread safe map you can wrap it with Collections.synchronizedMap method: Map map =Ĭollections.synchronizedMap(new LinkedHashMap()) To point out, LinkedHashMap is not synchronized. Public LinkedHashMap(int initialCapacity, * IllegalArgumentException if the initial capacity is negative * access-order, false for insertion-order * accessOrder the ordering mode - true for * specified initial capacity, load factor and ordering mode. * Constructs an empty LinkedHashMap instance with the If you want to, you can also override the load factor and initial capacity by using another constructor: /** * with the default initial capacity (16) and load factor (0.75). * Constructs an empty insertion-ordered LinkedHashMap instance #Linked hashmap code#Following code is taken from the LinkedHashMap class: /** When creating a LinkedHashMap in Java its’ initial capacity is 16 and the load factor is 0.75. #Linked hashmap full#You can see the full list of available methods in its’ documentation and some example code about HashMap here. As LinkedHashMap class extends HashMap, most of their functionality is the same. This is possible because LinkedHashMap takes advantage of doubly linked list under the hood by keeping references on the previous and next entry. Its’ keys remain in the same order as they were inserted into the map. Java LinkedHashMap is an ordered version of HashMap. It would be better if you already know what a HashMap is before continuing to read this blog post. Also, we’ll talk about what is the difference between the LinkedHashMap and HashMap in Java. (A key k is * reinserted into a map m if m.put(k, v) is invoked when * m.containsKey(k) would return true immediately prior to * the invocation.) * * This implementation spares its clients from the unspecified, generally * chaotic ordering provided by HashMap} (and Hashtable}), * without incurring the increased cost associated with TreeMap}.In this blog post we’ll cover what is Java LinkedHashMap and when to use it. Note that insertion order is not affected * if a key is re-inserted into the map. This linked list defines the iteration ordering, * which is normally the order in which keys were inserted into the map * (insertion-order). This implementation differs from * HashMap in that it maintains a doubly-linked list running through * all of its entries. */ package java.util import import import import java.io.IOException /** * Hash table and linked list implementation of the Map interface, * with predictable iteration order. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit if you need additional information or have any * questions. #Linked hashmap software#* * You should have received a copy of the GNU General Public License version * 2 along with this work if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is free software you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. Line source /* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |