linkedhashmap

How to keep the order of elements in hashtable

六眼飞鱼酱① 提交于 2019-11-26 21:28:42
问题 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. 回答1: 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

Java LinkedHashMap get first or last entry

只愿长相守 提交于 2019-11-26 16:00:59
I have used LinkedHashMap because it is important the order in which keys entered in the map. But now I want to get the value of key in the first place (the first entered entry) or the last. Should there be a method like first() and last() or something like that? Do I need to have an iterator to just get the first key entry? That is why I used LinkedHashMap ! Thanks! skaffman The semantics of LinkedHashMap are still those of a Map, rather than that of a LinkedList . It retains insertion order, yes, but that's an implementation detail, rather than an aspect of its interface. The quickest way to

How get value from LinkedHashMap based on index not on key? [duplicate]

梦想的初衷 提交于 2019-11-26 13:28:30
问题 This question already has answers here : How to get position of key/value in LinkedHashMap using its key (5 answers) Closed 5 years ago . I have LinkedHashMap<String, List<String>> hMap; I want to get List<String> by position not on key. I don't want to use iterate. Is there any other way to get Value based on index ? 回答1: You can't get the value of the Map based on index, Map s just don't work that way. A workaround would be to create a new list from your values and get the value based on

Is the order guaranteed for the return of keys and values from a LinkedHashMap object?

邮差的信 提交于 2019-11-26 12:49:18
I know LinkedHashMap has a predictable iteration order (insertion order). Does the Set returned by LinkedHashMap.keySet() and the Collection returned by LinkedHashMap.values() also maintain this order? The Map interface provides three collection views , which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the

LinkedHashMap in .NET

跟風遠走 提交于 2019-11-26 09:53:15
问题 I wonder if there is a counterpart to java.util.LinkedHashMap in .NET? (ie. the elements are (re)ordered automatically if I access an element. (boolean accessOrder) ). 回答1: A bit of Googling seems to show that there is no built in C# equivalent for LinkedHashMap, but there are some third party options available. 回答2: Just to clarify a bit for readers: LinkedHashMap only behaves that way when built with one particular constructor overload. Normally the elements are maintained in insert order.

Sorting LinkedHashMap

余生长醉 提交于 2019-11-26 08:56:23
问题 How can I sort a LinkedHashMap based on its values given that the LinkedHashMap contains of String and Integer. So I need to sort it based on the Values which are Integers. Thanks a lot 回答1: This is now quite a bit easier with Java 8 streams: you don't need the intermediate map to sort: map.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .forEach(entry -> ... ); 回答2: List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(map.entrySet()); Collections

Is the order guaranteed for the return of keys and values from a LinkedHashMap object?

徘徊边缘 提交于 2019-11-26 02:50:30
问题 I know LinkedHashMap has a predictable iteration order (insertion order). Does the Set returned by LinkedHashMap.keySet() and the Collection returned by LinkedHashMap.values() also maintain this order? 回答1: The Map interface provides three collection views , which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some