How to sort HashMap based on Date? [duplicate] - java

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.

Related

Java sort Map by Value descending and then by Key ascending [duplicate]

This question already has answers here:
Sort Map by value in java [duplicate]
(3 answers)
Sort a Map<Key, Value> by values
(64 answers)
How to sort HashMap entries values by multiple properties Java8 [duplicate]
(3 answers)
Closed 3 months ago.
I have a LinkedHashMap<String, Integer> that I need to order first by the Value descending, and then by the Key ascending. I don't know how to make the second condition work - I tried .thenComparing and everything I managed to see as a suggestion on the Internet but with no success.
playersSkillsPoints.get(user).entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.forEach(e -> {System.out.printf("- %s <::> %d%n", e.getKey(), e.getValue());
I tried:
.sorted(Map.Entry.comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()))
but got:
Cannot resolve method 'thenComparing(Comparator<Entry<K, V>>)'
Any suggestions would be appreciated!
You're already on the right track but probably just face issues with the generic type inference. If you help the compiler a little it should work, e.g. try this:
...sorted(Map.Entry.<String, Integer>comparingByValue().reversed()
.thenComparing(Map.Entry.comparingByKey()))
Alternatively create the comparators first, then combine them:
Comparator<Entry<String, Integer>> valueComparator = Map.Entry.comparingByValue();
Comparator<Entry<String, Integer>> keyComparator = Map.Entry.comparingByKey();
...sorted(valueComparator.reversed().thenComparing(keyComparator))

Sort a hashmap and get a key in an order (2nd key, 3rd, etc.) [duplicate]

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.

Need to sort alphanumeric keys of a map [duplicate]

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.

Alternative for hashmap [duplicate]

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));

Best Java Datastructure to store key Value Pair [duplicate]

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

Categories