linkedhashmap

How is the implementation of LinkedHashMap different from HashMap?

て烟熏妆下的殇ゞ 提交于 2019-11-30 04:14:44
If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap? What are all the extra overhead LinkedHashMap has when compared to HashMap in Java? LinkedHashMap will take more memory. Each entry in a normal HashMap just has the key and the value. Each LinkedHashMap entry has those references and references to the next and previous entries. There's also a little bit more housekeeping to do, although that's usually irrelevant. If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap? You should not confuse complexity with performance.

Use LinkedHashMap to implement LRU cache

岁酱吖の 提交于 2019-11-29 20:29:12
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, 2); cache.put(1, 1); cache.put(3, 3); System.out.println(cache); } private LRUCache(int size) {

Converting a collection to Map by sorting it using java 8 streams

一世执手 提交于 2019-11-29 11:38:50
问题 I have a list that I need to custom sort and then convert to a map with its Id vs. name map. Here is my code: Map<Long, String> map = new LinkedHashMap<>(); list.stream().sorted(Comparator.comparing(Building::getName)).forEach(b-> map.put(b.getId(), b.getName())); I think this will do the job but I wonder if I can avoid creating LinkedHashMap here and use fancy functional programming to do the job in one line. 回答1: You have Collectors.toMap for that purpose : Map<Long, String> map = list

Building ordered JSON String from LinkedHashMap

末鹿安然 提交于 2019-11-29 09:28:14
问题 I had a need for having Key/Value pairs in the order of my insertion, so I opted to use LinkedHashMap over HashMap . But I need to convert the LinkedHashMap into a JSON String where the order in the LinkedHashMap is maintained in the string. But currently I'm achieving it by: First converting the LinkedHashMap into JSON. Then converting the JSON into a string. import java.util.LinkedHashMap; import java.util.Map; import org.json.JSONObject; public class cdf { public static void main(String[]

How to sort a LinkedHashMap by value in decreasing order in java stream?

旧城冷巷雨未停 提交于 2019-11-29 05:56:27
To sort it int ascending order I can use: myMap.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); How can I do it in decreasing order? Since Java 1.8 java.util.Comparator.reversed() myMap.entrySet().stream() .sorted(Map.Entry.comparingByValue().reversed()) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); To sort in reverse order, pass Comparator.reverseOrder() as parameter to comparingByValue . To get a LinkedHashMap , you must specifically request one with the 4-argument toMap() . If you don't specify what kind of

Convert from LinkedHashMap to Json String

女生的网名这么多〃 提交于 2019-11-29 02:16:20
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 have a class to map the response, and it is not a solution create a map class, that is why I use a

How is the implementation of LinkedHashMap different from HashMap?

走远了吗. 提交于 2019-11-29 02:13:03
问题 If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap? What are all the extra overhead LinkedHashMap has when compared to HashMap in Java? 回答1: LinkedHashMap will take more memory. Each entry in a normal HashMap just has the key and the value. Each LinkedHashMap entry has those references and references to the next and previous entries. There's also a little bit more housekeeping to do, although that's usually irrelevant. 回答2: If LinkedHashMap's time

java collections - keyset() vs entrySet() in map

那年仲夏 提交于 2019-11-28 18:40:55
问题 I put a string array elements is a map where elements of string array is key and frequency of word is value, e.g.: String[] args = {"if","it","is","to","be","it","is","up","me","to","delegate"}; then the map will have entries like [ if:1, it:2 .... ] Set<String> keys = m.keySet(); System.out.println("keyset of the map : "+keys); prints all keys: "if","it","is","to","be","it","is","up","me","to","delegate" Set<Map.Entry<String, Integer>> entrySet = m.entrySet(); Iterator<Map.Entry<String,

How to iterate through LinkedHashMap with lists as values

泄露秘密 提交于 2019-11-28 16:58:07
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) for (Map.Entry<String, ArrayList<String>> entry : test1.entrySet()) { String key = entry.getKey(); ArrayList<String> value = entry.getValue(); // now work with key and

How is the internal implementation of LinkedHashMap different from HashMap implementation?

五迷三道 提交于 2019-11-28 16:43:44
I read that HashMap has the following implementation: main array ↓ [Entry] → Entry → Entry ← linked-list implementation [Entry] [Entry] → Entry [Entry] [null ] So, it has an array of Entry objects. Questions: I was wondering how can an index of this array store multiple Entry objects in case of same hashCode but different objects. How is this different from LinkedHashMap implementation? Its doubly linked list implementation of map but does it maintain an array like the above and how does it store pointers to the next and previous element? So, it has an array of Entry objects. Not exactly. It