linkedhashmap

Use LinkedHashMap to implement LRU cache

假装没事ソ 提交于 2019-11-28 16:31:19
问题 I was trying to implement a LRU cache using LinkedHashMap. In the documentation of LinkedHashMap (http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html), it says: Note that insertion order is not affected if a key is re-inserted into the map. But when I do the following puts public class LRUCache<K, V> extends LinkedHashMap<K, V> { private int size; public static void main(String[] args) { LRUCache<Integer, Integer> cache = LRUCache.newInstance(2); cache.put(1, 1); cache.put(2

How to add element at specific index/position in LinkedHashMap?

对着背影说爱祢 提交于 2019-11-28 08:52:33
I have an ordered LinkedHashMap and i want to add element at specific index , say at first place or last place in the map. How can i add element in LinkedHashMap at an specific position? Even if I could add an element to FIRST or LAST position in LinkedHashMap would help! MasterCassim You can not change the order. It is insert-order (by default) or access-order with this constructor: public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode. Parameters:

Does entrySet() in a LinkedHashMap also guarantee order?

笑着哭i 提交于 2019-11-28 06:40:24
I am using a linkedHashMap to guarantee order when someone tries to access it. However, when it comes time to iterate over it, does using entrySet() to return key/value pairs guarantee order as well? No changes will be made while iterating. EDIT: Also, are there any adverse effects from iterating through the map by iterating through its keys and calling get? Michael Myers According to the Javadocs , yes. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the

What is the difference between LRU and LFU

对着背影说爱祢 提交于 2019-11-28 03:39:41
What is the difference between LRU and LFU cache implementations? I know that LRU can be implemented using LinkedHashMap . But how to implement LFU cache? Zorayr Let's consider a constant stream of cache requests with a cache capacity of 3, see below: A, B, C, A, A, A, A, A, A, A, A, A, A, A, B, C, D If we just consider a Least Recently Used (LRU) cache with a HashMap + doubly linked list implementation with O(1) eviction time and O(1) load time, we would have the following elements cached while processing the caching requests as mentioned above. [A] [A, B] [A, B, C] [B, C, A] <- a stream of

Sending LinkedHashMap to intent

China☆狼群 提交于 2019-11-28 02:07:54
I want send LinkedHashMap to another Intent. But I don't known what method for extras is allowable. Bundle extras = getIntent().getExtras(); LinkedHashMap<Integer, String[]> listItems = extras.get(LIST_TXT); David Wasser You cannot reliably send a LinkedHashMap as an Intent extra. When you call putExtra() with a LinkedHashMap , Android sees that the object implements the Map interface, so it serializes the name/value pairs into the extras Bundle in the Intent . When you want to extract it on the other side, what you get is a HashMap , not a LinkedHashMap . Unfortunately, this HashMap that you

How to keep the order of elements in hashtable

╄→尐↘猪︶ㄣ 提交于 2019-11-27 23:29:43
I have a hashtable . values() method returns values in some order different from the order in which i am inserted.How can i get the values in the same order as i inserted?Using LinkedHashmap is an alternative but it is not synchronized. cletus Use a LinkedHashMap . Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map

How to traverse Linked Hash Map in reverse? [duplicate]

泄露秘密 提交于 2019-11-27 21:22:37
Possible Duplicate: Iterating through a LinkedHashMap in reverse order How to traverse Linked Hash Map in a reverse order? Is there any predefined method in map to do that? I'm creating it as follows: LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer,String>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); List<Entry<Integer,String>> list = new ArrayList<>(map.entries()); for( int i = list.size() -1; i >= 0 ; i --){ Entry<Integer,String> entry = list.get(i); } Not really pretty and at the cost of a copy of the entry set, which if your map has a significant number of

Java : Cartesian Product of a List of Lists

北慕城南 提交于 2019-11-27 19:28:57
I have a problem that is really kind of a general programming question, but my implementation is in Java, so I will provide my examples that way I have a class like this: public class Foo { LinkedHashMap<String, Vector<String>> dataStructure; public Foo(LinkedHashMap<String, Vector<String>> dataStructure){ this.dataStructure = dataStructure; } public String[][] allUniqueCombinations(){ //this is what I need to do } } I need to generate a nested array from my LinkedHashMap that represents every unique combination of all values in the LHM. for example, if my LHM looks like this (pseudocode, but

Convert from LinkedHashMap to Json String

非 Y 不嫁゛ 提交于 2019-11-27 16:25:59
问题 I'm Workin with Mongo using Jongo, when I do a query I receive a LinkedHashMap as result. Iterator one = (Iterator) friends.find(query).projection("{_id:0}").as(Object.class); while (one.hasNext()) { LinkedHashMap data= new LinkedHashMap(); data= (LinkedHashMap) one.next(); String content=data.toString(); } the problem is that if the json is {"user":"something"} content will be {user=something}, it is not a json is only toString method from HashMap. How I can get the original JSON?. I don't

How to iterate through LinkedHashMap with lists as values

a 夏天 提交于 2019-11-27 10:06:54
问题 I have following LinkedHashMap declaration. LinkedHashMap<String, ArrayList<String>> test1 my point is how can i iterate through this hash map. I want to do this following, for each key get the corresponding arraylist and print the values of the arraylist one by one against the key. I tried this but get only returns string, String key = iterator.next().toString(); ArrayList<String> value = (ArrayList<String> )test1.get(key) 回答1: for (Map.Entry<String, ArrayList<String>> entry : test1.entrySet