This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Bi-directional Map in Java?
How can I retrieve key of Map by index when keys are not numeric and unordered ?
For example :
Map<String, Integer> test = new TreeMap<String, Integer>();
test.put("a", 1);
test.put("b", 2);
test.put("z", 3);
test.put("m", 4);
I want to get z if I have index 2 or a if I have index 0.
I know I can do dirty loop with increment to get it but is there another smart way to do it ?
What makes this a bit confusing is whether you're referring to the index based on the order the item is added, or based on the natural ordering of the key (eg: alphabetical)
You can obtain the list of may keys using map.keySet() but there's no guarantee the key set will be in the order which you add it in
You can use TreeMap instead of HashMap if you want to keep your data in some kind of ordering (eg: alphabetical ordering). If you prefer other way of ordering you can implement your own comparator
Related
This question already has answers here:
java HashMap sorting <String,Integer> . How to sort it? [duplicate]
(4 answers)
Sort a Map<Key, Value> by values
(64 answers)
Closed 1 year ago.
I have a HashMap<String, Integer> list and I want to sort it by integers and be able to get a key in a certain order (for example: get the 3rd, or 5th key).
How do I go by doing this?
You can't sort a HashMap. The order of entries within a HashMap is kinda random.
You could use myMap.keySet() to extract the keys (in random order) and put them in a new ordered collection (= List or SortedSet), for example: new ArrayList<>(myMap.keySet());. And then sort this List.
Or you use a SortedMap with a custom Comparator instead of a HashMap.
This question already has answers here:
Bidirectional Map
(9 answers)
Closed 3 years ago.
Let's suppose we want to associate to some set of distinct values a key, and suppose the mapping is injective. I want to be able to do things, and do them as quickly as possible: get the key of a given value and get the value corresponding to a give key. However, if possible I'd like to do key retrieval in O(1).
There are multiple ways to store key, value pairs in Java, for example with dictionaries or hashmaps. But neither is perfect: for example neither structure has a method for retrieving a key from a given value. My values are positive integer pairs. It might just be better to store the keys in a matrix so I can retrieve them immediately.
For my needs, what is the best option?
If the keys are unique and the values are unique, that you can just create an inversed hashmap, but otherwise I would just loop trough the HashMap and when the value equals return the key.
I would advocate using BidiMap by apache collections.
This question already has answers here:
Java LinkedHashMap replace key
(2 answers)
Closed 5 years ago.
Here would be a LinkedHashMap, imagine it had many values inside of it.
LinkedHashMap map = new LinkedHashMap<>();
My question is, if one of the existing values was, for example, at index 4 and the same key was .put into the map, will it simply replace the value and remain at 4? Or, will it add onto the end?
The documentation of LinkedHashMap explicitly points this out:
This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (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.)
So the value is replaced, but the iteration order will remain the same. For your example, the entry with key 4 will remain at the same position for the iteration.
This question already has answers here:
How to sort a HashMap in Java [duplicate]
(17 answers)
Closed 6 years ago.
I have a small HashMap (less than 100 entries) that contains a unique object (of my design) as the key and a Double as the value.
I need to retrieve n number of objects that have the lowest values.
So say my HashMap looked like this and I wanted the lowest 3.
Object, 4.0
Object, 5.0
Object, 2.0
Object, 12.0
Object, 10.0
Object, 3.0
I would want to fetch the first, third, and last entries as those have the lowest values.
I know there are methods such as Collections.min which I could run on the HashMap but I need more than just the lowest value and I need to know the key it corresponds to as well. Research has also led me to come across Selection Algorithms but I am confused and not quite sure how to use these. I apologise if a question of this sort has been asked before, I searched for a long time and could not find anything. Thanks pre-emptively for your help.
List<Entry<Key, Double>> lowestThreeEntries = map.entrySet()
.stream()
.sorted(Comparator.comparing(Entry::getValue))
.limit(3)
.collect(Collectors.toList());
Put all then entries of your map inside an array. Then, implement a Comparator<Map.Entry<Key,Value>> which compares the map entries according to the value they hold.
And finally, sort the entries of the map according to your shiny comparator. (You can use Arrays.sort)
Consider inverting your map and using a 'MultiMap' (effectively a more user friendly implementation of Map<Key, List<Value>>), the Guava TreeMultiMap should do exactly what you're after.
http://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/TreeMultimap.html
Edit - Added example (using String in place of the custom object for brevity)
TreeMultimap<Double, String> map = TreeMultimap.<Double, String>create();
map.put(4.0, "a");
map.put(5.0, "b");
map.put(2.0, "c");
map.put(12.0, "d");
map.put(10.0, "e");
map.put(3.0, "f");
Iterator<String> iterator = map.values().iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
This will print out the strings in ascending order according to the double value (c, f, a, b, e, d)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
ordered map implementation
I am using the hashmap to store the values as follows -
Map<String,String> propertyMap=new HashMap<String,String>();
propertyMap.put("Document Type", dataobject.get("dDocType"));
propertyMap.put("Document Title", dataobject.get("dDocTitle"));
propertyMap.put("Revision Label", dataobject.get("dRevLabel"));
propertyMap.put("Security Group", dataobject.get("dSecurityGroup"));
After that i am getting the hashmap key and value in a List
documentProperties = new ArrayList(propertyMap.entrySet());
But when i iterate over the List i am not getting the key and values in the order i have put it into the map..
Is there is anyway by which i can get the values in the Order i am putting it into the map.
Thanks
I believe what you are looking for is LinkedHashMap.
This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
By the why, why the need for a separate ArrayList? You can directly iterate over Map.entrySet:
for (final Map.Entry<String, String> entry : propertyMap.entrySet()) {
...
}
propertyMap.entrySet() you get results as Set. Set is un-ordered.
new ArrayList(propertyMap.entrySet()); constructs a list with the order you have in Set (which may not be the order you have put into map).
If you are looking for order map, you may use LinkedHashMap
Here is interesting discussion on this topic.
You have to use LinkedHashMap. For more details Please check this question and answer given by Michael
Try this..
for(Map.Entry<String, String> m : map.entrySet()){
// Use m.getKey() to get the Key.
// Use m.getValue() to get the Value.
}