This question already has answers here:
How can I sort Map values by key in Java?
(17 answers)
Closed 6 years ago.
Lets say, I have a map as Map<String, String> data = new HashMap<String, String>();
I have following keys of this map
{benefits7,benefits11,benefits1,benefits10,benefits15};
And I need it to sort these keys as following:
{benefits1,benefits7,benefits10,benefits11,benefits15}
Can anyone help please?
Can't you use SortedMap instead HashMap?
This way if you iterate over the map by using entrySet or KeySet it will return your values sorted by its key.
By default it use the natural ordering for your key's class (String in your example), and this behaviour seems enough for your requirements
Here is the SortedMap API
Can you try this ?
Map<String, String> data = new HashMap<String, String>();
Set<String> keys = data.keySet();
Set<String> sortedKeys = Collections.sort(keys);
You can use a list to sort the keys. If you need to have order in your map, then you should not use a HashMap as it doesn't care about order.
Use a linked hash map instead. So, you could have an ordered list of keys:
List<String> keys = new ArrayList<String>(data.keySet());
Collections.sort(keys);
If you want to iterate over the map according to the order of your keys, then use a java.util.LinkedHashMap, instead of a HashMap. But you would have to put elements according to the explicit order as shown above.
Related
This question already has an answer here:
Java: Get hashmap value [closed]
(1 answer)
Closed 6 years ago.
I have a hashmap which has several values and it override the same keys, is there any solution for this? this is my Hashmap
HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Jack","John");
meMap.put("Jack","Jacob");
it will override the first one.
You can use get function: meMap.get("Color3");. You can access to different methods of maps in Java here:
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
A Map is a Key - Value store, meaning that you access values in the Map by specifying its Key. So you would do:
meMap.get("Color3");
You have to use get() for single element:
Toast.makeText(context, map.get("Color3"), Toast.LENGTH_SHORT).show();
You can not have multiple values for one key in HashMap.
If you need to store multiple values for same key then you should use map insted of HashMap.
Map has combination of Key and value. If you want a value, you have to call it's key.
System.out.println(meMap.get("Color3"));
Resource Link:
Class HashMap
From your comment,it seems you want to store multiple values for a single key.In that case you can store your values in a List and then add that list to the map,as shown below:
HashMap<String, List<String>> meMap=new HashMap<String, List<String>>();
List<String> lst=new ArrayList<String>();//List to store colors
lst.add("Red");
lst.add("Blue");
lst.add("Green");
lst.add("White");
//add the key and list to map
meMap.put("Color",lst);
//to get the value at 3rd position,use get(2),as index starts from 0.
System.out.println(meMap.get("Color").get(2));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Hashmap: How to get key from value?
Bi-directional Map in Java?
I want a key value data structure to use for Android App. I can use Map<K,V>, but in Map I can't get key for particular Value.
Is there any good Java data structure using which I can retrieve key by value and vice-versa.
You can use Map with entrySet and Map.Entry class to iterate and get both keys and values even if you don't know any of the keys in the Map.
Map <Integer,String> myMap = new HashMap<Integer,String>();
Iterator<Entry<Integer, String>> iterator = myMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer,String> pairs = (Map.Entry<Integer,String>)iterator.next();
String value = pairs.getValue();
Integer key = pairs.getKey();
System.out.println(key +"--->"+value);
}
Use Google Guava BiMap
How to use Guava Libraris in Android is here
The best data structure would be Java HashMaps check this link you can use the get(Object key) method
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.
}
I searched the web(and Stackoverflow) a lot but couldn't exactly found what I was looking for. Also couldn't really understand the concept.
As a Java assignment I have a treeMap storing some values in it.
static Map<String, Double> customers = new TreeMap<String, Double>();
Naturally it stores them ascending by it's String values. But I want it to store it's values ascending by it's Double values. How can I achieve this by a comparator? Details would be much appreciated.
Also again in this assignment I've another, separate treeMap which stores different values. Again declared like below:
static Map<String, Double> customers = new TreeMap<String, Double>();
It's okay for it to store it's values ascending by String. I'm using it like this to print out it's values but I also need to print it's values in ascending order sorted by Double. So how can I re-sort a treeMap. Again, details would be much appreciated.
You cannot re-sort a TreeMap. It has a fixed iteration order, determined at TreeMap construct-time. If you want a different sort order, use a different data structure.
I'm using it like this to print out it's values but I also need to print it's values in ascending order sorted by Double.
So don't worry about sorting the TreeMap. Create a List<Double> of the values in the map, and sort those.
List<Double> values = new ArrayList<Double>(customers.values());
Collections.sort(values);
System.out.println(values);
Make another map:
Map<Double, String> values = new TreeMap<Double, String>();
and always add to both maps (e.g. via a helper method). Unless there are memory constraints, you will get your customers maps sorted by customer and values sorted by whatever the Double value represents.
If you can have duplicate values, you can make a list (or a set, depending on the requirements) of customers instead:
Map<Double, List<String>> values = new TreeMap<Double, List<String>>();
This question already has answers here:
Sorting hashmap based on keys
(9 answers)
Closed 3 years ago.
I trying to sort this HashMap based on date in keys
My Hash map:
Map<Date, ArrayList> m = new HashMap<Date, ArrayList>();
Use a TreeMap instead of HashMap. As Date already implements Comparable, it will be sorted automatically on insertion.
Map<Date, ArrayList> m = new TreeMap<Date, ArrayList>();
Alternatively, if you have an existing HashMap and want to create a TreeMap based on it, pass it to the constructor:
Map<Date, ArrayList> sortedMap = new TreeMap<Date, ArrayList>(m);
See also:
Java tutorials - Map implementations
Java tutorials - Object ordering
Use TreeMap instead of HashMap to store the data,it will be sorted automatically.